Importing open_meteo takes about 4.5 seconds since 1.0.0

Aperta
#1,416 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
28/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
32
Fork
10
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di frenck/python-open-meteo

Tutte le issue di frenck/python-open-meteo

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.