Garden-AI / garden

https://garden-ai.readthedocs.io
MIT License
20 stars 4 forks source link

speed up your cold imports 1.6x with One Weird Trick #390

Closed OwenPriceSkelly closed 9 months ago

OwenPriceSkelly commented 9 months ago

Overview

😤 Scientists HATE him 😤! see how this One Weird Trick can save you countless (~4.5) seconds whenever you import garden_ai*

* for the first time ** in a new, completely isolated virtual environment *** only noticeable on apple silicon

Discussion

Turns out that nearly all of the ~10-15s cold start delay we've noticed in our study halls can be traced to our big dependencies with C extension modules: pydantic, which is the majority of the slowdown but too difficult to lazy import everywhere; and nbconvert, which was trivial to lazy import since it's only used once in containers.py. Pretty consistent 60/40 split in terms of total contribution to import time on my machine.

Testing

I have a little profiling script that sets up a new environment, installs a package, and then profiles how long that package & its dependencies take to import with python -X importtime -c 'import garden_ai'. I also used tuna to visualize the import profiles, but I think snakeviz can handle that too. Manually running stuff like python -c 'import pydantic' && python -X importtime -c 'import garden_ai' helped sanity check, too


📚 Documentation preview 📚: https://garden-ai--390.org.readthedocs.build/en/390/

codecov-commenter commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (0e0f698) 76.04% compared to head (eb52ac1) 76.04%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #390 +/- ## ======================================= Coverage 76.04% 76.04% ======================================= Files 44 44 Lines 2542 2542 ======================================= Hits 1933 1933 Misses 609 609 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

OwenPriceSkelly commented 9 months ago

LGTM! If you want to save your profiling script in a side folder or something that could be cool

sure! maybe not worth adding to the repo but I copied it to a gist here