Importing open_meteo takes about 4.5 seconds since 1.0.0

オープン
#1,416 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
28/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
performance

調査の方向性

Start by reading the shared model base class and forecast.py, where import profiling shows most of the delay. The proposed lazy_compilation approach is measured, but it currently fails with real freezegun until the reported mashumaro fix is released. After that dependency fix is available, check parsing under freeze_time and compare import time and parsing results with the existing behavior.

索引モデルが issue の本文から書いたものです。

説明

Description

Update (3 December): all numbers below are re-measured on an idle machine with the same versions and settings; the first ones were about 2.5× higher, most likely because the machine was busy. The ratios and the proposal are unchanged.

Since 1.0.0, import open_meteo takes about 4.5 seconds on our machine. Almost all of it is mashumaro building the from_dict/to_dict code of every model when the classes are created: the models grew from about 630 lines in 0.4.0 to about 3,700 in 1.0.1, and forecast.py alone takes about 3.1 s.

In Home Assistant this shows at every start: integrations are imported one after another, so others wait behind it.

Measured (python 3.14, mashumaro 3.23, the Home Assistant 2026.12.0 image, 1 CPU, python -X importtime -c "import open_meteo", 3 runs each):

import
0.4.0 1.2-1.3 s
1.0.1 4.6-4.7 s
1.0.1 with lazy_compilation (below) 0.7-0.9 s

cProfile of the import: 55 calls to mashumaro's __init_subclass__ account for nearly all of it; about 60 % builds the unpack code and 40 % the pack code.

A possible fix: mashumaro's lazy_compilation, set once on a shared base class of the models, so a model's code is built when it is first used. I tried it locally:

  • The test suite passes unchanged (147 passed, 23 snapshots), including test_parsing_with_patched_datetime, and so do Home Assistant's open_meteo tests (16 passed).
  • Every fixture parses to the same result as with 1.0.1 (to_dict compared for all 36 fixtures that map to a model).
  • The cost moves to the first parse of a model: about 0.6 s for the first Forecast (Home Assistant's forecast.json test fixture) instead of under 10 ms, then about 1 ms. Only the models a caller actually uses are built. For an async caller that first parse runs on the event loop; a caller can warm up in an executor by parsing a small response once.

One catch: with real freezegun, a first parse inside freeze_time fails (UnserializableField: Field "time" of type datetime in CurrentForecast is not serializable), where it works today. That is a mashumaro bug in lazy compilation, which I reported as Fatal1ty/mashumaro#391 with a fix ready. test_parsing_with_patched_datetime still passes with the patch, so it does not catch this case. So a PR here would wait for a mashumaro release with that fix.

Would you accept a PR for this once that is out? Or would you rather keep building at import (for example to keep the first parse fast) and look for another way?

Version

1.0.1

Additional information

This analysis and the patch were made with Claude Opus 5.5 (claude-opus-5-5), an AI model; I reviewed them myself. Careful review and measurements on other machines are very welcome.

主要言語
Python
スター
32
フォーク
10
PR マージ指標
30日以内にマージされた PR はありません

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

frenck/python-open-meteo のほかの issue

frenck/python-open-meteo の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。