Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce import time - draft #8831

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

perf: reduce import time - draft #8831

wants to merge 3 commits into from

Conversation

anakin87
Copy link
Member

@anakin87 anakin87 commented Feb 7, 2025

Motivation

Motivated by recent issues (#8649, #8650, and #8704), I investigated the import times of Haystack.

I used some raw scripts (attached in this draft PR) and commands like
python -X importtime -c "import haystack" to analyze cumulative import times of packages.

Findings

  • Our LazyImport context manager behaves like a DeferImportError context manager. If a package you import is available, it gets imported immediately. If not, the import error is caught and only raised later when calling .check(). Maybe this is something known, but it was surprising to me.
  • As a result the more libraries installed, the slower the imports (since more packages get loaded).
  • Our telemetry surprisingly imports heavy dependencies like torch and transformers.

An exploratory solution

I explored low-effort ways to improve this:

  • remove heavy imports from telemetry
  • make modules import in __init__.py of packages actually lazy
    • I only did this for the utils package, that triggered the torch import

Results

Tested on Ubuntu 22.04, Python 3.10.12, averaged over 30 runs.

I focus on User time and System time, as they capture actual CPU time, irrespective of parallelism and system load (reference).

Dependencies

  • Minimal = pip install haystack-ai
  • Test = All dependencies required for tests (pyproject)
Haystack Dependencies User Time Sys Time Total Time
Current (35788a2) Minimal 2.114s 0.031s 2.145s
Current (35788a2) Test 3.236s 0.185s 3.420s
This PR Minimal 2.098s 0.036s 2.134s
This PR Test 2.265s 0.050s 2.316s

Next Steps

I believe that these results can encourage discussion.

  • Should we rename LazyImport or transform it into a truly lazy importer (difficult if we want to keep its API unchanged)?
  • Do we want to use real lazy imports in the __init__.py of packages?

@github-actions github-actions bot added the type:documentation Improvements on the docs label Feb 7, 2025
@anakin87 anakin87 added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Feb 7, 2025
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 13204178773

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 6 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 92.284%

Files with Coverage Reduction New Missed Lines %
utils/requests_utils.py 6 0.0%
Totals Coverage Status
Change from base Build 13203068476: -0.02%
Covered Lines: 9125
Relevant Lines: 9888

💛 - Coveralls

@anakin87 anakin87 requested a review from julian-risch February 7, 2025 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-for-release-notes PRs with this flag won't be included in the release notes. type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants