⚡️ Speed up method AsyncMemory._should_use_agent_memory_extraction by 123%
#7
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.
📄 123% (1.23x) speedup for
AsyncMemory._should_use_agent_memory_extractioninmem0/memory/main.py⏱️ Runtime :
7.89 microseconds→3.53 microseconds(best of30runs)📝 Explanation and details
The optimization focuses on improving the performance of the
_should_use_agent_memory_extractionmethod through early exit logic and avoiding unnecessary computation.Key Optimization:
Early Return Pattern: Instead of computing both conditions (
has_agent_idandhas_assistant_messages) regardless of the first condition's result, the optimized version immediately returnsFalseifagent_idis missing, avoiding the expensiveany()operation entirely.Efficient Loop Structure: When
agent_idis present, the code uses a simpleforloop that returnsTrueimmediately upon finding the first assistant message, rather than theany()function which creates a generator expression and potentially scans all messages.Performance Impact:
The line profiler shows the optimization eliminates the expensive
any()call (which took 66.4% of the original execution time at 9764ns). In the optimized version, whenagent_idis None (common case), the function exits early after just two operations, reducing total runtime from 14.7µs to 6.8µs.Minor Optimization:
["faiss", "qdrant"]to("faiss", "qdrant")for slightly better performance in theinoperator.This optimization is particularly effective for scenarios where
agent_idis frequently missing from metadata, as it completely avoids the message scanning loop. Even whenagent_idis present, the loop-based approach is more efficient thanany()for early termination cases.✅ 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_memory_main_AsyncMemory__should_use_agent_memory_extractiontest_pytest_testsvector_storestest_opensearch_py_testsvector_storestest_upstash_vector_py_testsllmstest_l__replay_test_0.py::test_mem0_memory_main_AsyncMemory__should_use_agent_memory_extractionTo edit these changes
git checkout codeflash/optimize-AsyncMemory._should_use_agent_memory_extraction-mhk2kz7tand push.