⚡️ Speed up method ValkeyDB._build_search_query by 43%
#10
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.
📄 43% (0.43x) speedup for
ValkeyDB._build_search_queryinmem0/vector_stores/valkey.py⏱️ Runtime :
14.7 microseconds→10.3 microseconds(best of52runs)📝 Explanation and details
The optimization achieves a 42% speedup by eliminating a redundant dictionary traversal in the
_build_search_querymethod.Key optimization:
Removed the
any(value is not None for key, value in filters.items())check from the initial condition. This was causing the method to iterate through all filter items twice - once to check if any non-None values exist, and again to build the filter parts.Simplified the early exit condition to just
if not filters:instead of the compound condition, reducing the conditional logic overhead.Why this is faster:
The original code performed an O(n) traversal of the filters dictionary with the
any()generator expression, followed by another O(n) traversal in the main loop. The optimized version eliminates the first traversal, reducing the time complexity from 2×O(n) to O(n) for the common case where filters contain valid values.Performance characteristics:
Based on the line profiler results, the optimization is most effective when filters are present and contain valid data (the common use case). The time spent in the initial condition check dropped from 42.8% to 15% of total execution time. This optimization particularly benefits scenarios with moderate to large filter dictionaries, as the savings scale linearly with the number of filter key-value pairs.
✅ Correctness verification report:
⏪ Replay Tests and Runtime
test_pytest_testsconfigstest_prompts_py_testsvector_storestest_weaviate_py_testsllmstest_deepseek_py_test__replay_test_0.py::test_mem0_vector_stores_valkey_ValkeyDB__build_search_queryTo edit these changes
git checkout codeflash/optimize-ValkeyDB._build_search_query-mhl2xmniand push.