⚡️ Speed up method Search.select_all by 107%
#125
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.
📄 107% (1.07x) speedup for
Search.select_allinchromadb/execution/expression/plan.py⏱️ Runtime :
1.98 milliseconds→956 microseconds(best of65runs)📝 Explanation and details
The optimization pre-computes the expensive
Selectobject creation at module-level as_PREDEFINED_SELECT_ALL, eliminating the need to recreate it on everyselect_all()call.Key changes:
_PREDEFINED_SELECT_ALL = Select(keys={Key.DOCUMENT, Key.EMBEDDING, Key.METADATA, Key.SCORE})is created once when the module loads instead of every timeselect_all()is calledselect_all()method now directly uses the cached object instead of constructing a new oneWhy this creates a 107% speedup:
The line profiler shows the original code spent 48.3% of its time (3.95ms out of 8.18ms) creating the
Selectobject with the set of fourKeyobjects. Set construction andKeyobject creation are expensive operations in Python. By moving this to module load time, eachselect_all()call now only performs the much cheaperSearchconstructor call.Test case performance patterns:
test_select_all_performance_large_loopshow dramatic improvements (108% speedup) whenselect_all()is called repeatedlySearchconfiguration, indicating the optimization benefits any usage patternThe optimization is particularly effective because
select_all()always returns the same set of keys, making it a perfect candidate for pre-computation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_p_g0hne0/tmp71_sap_k/test_concolic_coverage.py::test_Search_select_allTo edit these changes
git checkout codeflash/optimize-Search.select_all-mh7jr5q1and push.