⚡️ Speed up function get_frontend_download_info by 3,899%
#35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 3,899% (38.99x) speedup for
get_frontend_download_infoincognee/api/v1/ui/ui.py⏱️ Runtime :
25.5 milliseconds→637 microseconds(best of255runs)📝 Explanation and details
The key optimization is adding
@lru_cache(maxsize=1)to theget_cognee_version()function. This provides massive performance gains because:What was optimized:
functools.lru_cache(maxsize=1)decorator to cache the result ofget_cognee_version()pyproject_pathvariable)Why this creates a 3898% speedup:
The profiler shows
get_cognee_version()was called 2043+ times, with each call performing expensive file system operations:Path(__file__).parent.parentpyproject.tomlWith caching, these expensive operations happen only once. Subsequent calls return the cached result in ~1μs instead of ~41μs per call.
Performance characteristics:
Test case benefits:
All test cases show 3500-4500% improvements because they involve repeated calls to
get_frontend_download_info(), which internally calls the now-cachedget_cognee_version(). The optimization is especially effective for:✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_frontend_download_info-mh1b6lbeand push.