Skip to content

perf: lazily import our modules and external libraries#624

Merged
lars-reimann merged 22 commits intomainfrom
lazy-imports-in-init-files
Apr 17, 2024
Merged

perf: lazily import our modules and external libraries#624
lars-reimann merged 22 commits intomainfrom
lazy-imports-in-init-files

Conversation

@lars-reimann
Copy link
Copy Markdown
Member

@lars-reimann lars-reimann commented Apr 17, 2024

Summary of Changes

We depend on several large libraries that take a while to load. Previously, importing Table would already import almost all of them, leading to horrendous startup times:

(Measure-Command { python -c "from safeds.data.tabular.containers import Table" }).TotalSeconds

➡️ 3.5068337 (seconds to import Table on main)

Now, we lazily import our own modules in the __init__.py files, and we lazily import external libraries. The latter part is quite ugly, since each function must now contain their external imports at the start. There is no better solution, however, and the improvements are huge:

(Measure-Command { python -c "from safeds.data.tabular.containers import Table" }).TotalSeconds

➡️ 0.1683219 (seconds to import Table in this branch)

We still have to pay the cost for an import once we first import a module, but at least this no longer has to happen fully upfront.

@lars-reimann lars-reimann requested a review from a team as a code owner April 17, 2024 13:21
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 17, 2024

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Elapsed time
✅ PYTHON black 53 0 0 1.92s
✅ PYTHON mypy 53 0 3.34s
✅ PYTHON ruff 53 0 0 0.25s
✅ REPOSITORY git_diff yes no 0.21s

See detailed report in MegaLinter reports
Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff

MegaLinter is graciously provided by OX Security

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e856cd5) to head (b828945).

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #624    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           62        62            
  Lines         4634      4748   +114     
==========================================
+ Hits          4634      4748   +114     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

# Conflicts:
#	src/safeds/data/tabular/containers/_tagged_table.py
#	src/safeds/exceptions/__init__.py
#	src/safeds/ml/nn/__init__.py
#	src/safeds/ml/nn/_model.py
@lars-reimann lars-reimann force-pushed the lazy-imports-in-init-files branch from 924df2c to 99879f6 Compare April 17, 2024 14:09
@lars-reimann lars-reimann force-pushed the lazy-imports-in-init-files branch from 99879f6 to 509bc02 Compare April 17, 2024 14:19
@lars-reimann lars-reimann marked this pull request as ready for review April 17, 2024 14:30
@lars-reimann
Copy link
Copy Markdown
Member Author

lars-reimann commented Apr 17, 2024

@Safe-DS/library For future additions, please move all imports of external libraries into the functions that need them, so they get evaluated lazily. It's hideous, but startup is much faster, as explained in the opening post.

@lars-reimann lars-reimann merged commit 20fc313 into main Apr 17, 2024
@lars-reimann lars-reimann deleted the lazy-imports-in-init-files branch April 17, 2024 14:32
lars-reimann pushed a commit that referenced this pull request Apr 17, 2024
## [0.21.0](v0.20.0...v0.21.0) (2024-04-17)

### Features

* add ARIMA model ([#577](#577)) ([8b9c7a9](8b9c7a9)), closes [#570](#570)
* Add ImageList class ([#534](#534)) ([3cb74a2](3cb74a2)), closes [#528](#528) [#599](#599) [#600](#600)
* more hash, sizeof and eq implementations ([#609](#609)) ([2bc0b0a](2bc0b0a))

### Performance Improvements

* Add special case to `Table.add_rows` to increase performance ([#608](#608)) ([ffb8304](ffb8304)), closes [#606](#606)
* improve performance of model & forward layer ([#616](#616)) ([e856cd5](e856cd5)), closes [#610](#610)
* lazily import our modules and external libraries ([#624](#624)) ([20fc313](20fc313))
* treat Tables specially when calling add_rows ([#606](#606)) ([e555b85](e555b85)), closes [#575](#575)
@lars-reimann
Copy link
Copy Markdown
Member Author

🎉 This PR is included in version 0.21.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Included in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants