⚡️ Speed up method ContextManager.compress_messages by 19%
#3
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.
📄 19% (0.19x) speedup for
ContextManager.compress_messagesinsrc/utils/context_manager.py⏱️ Runtime :
12.5 milliseconds→10.5 milliseconds(best of109runs)📝 Explanation and details
The optimized code achieves a 19% speedup through several key performance optimizations:
1. Eliminated redundant token counting in
count_tokens()sum()generator expression and local variable cachingself._count_message_tokensas a local variable to avoid repeated attribute lookups in the hot loop2. Avoided duplicate computation in
compress_messages()token_count = self.count_tokens(messages)to compute once and reuseself.count_tokens(messages)twice - once inis_over_limit()and again in the logging statement3. Micro-optimizations in
_compress_messages()self._count_message_tokensandself._truncate_message_contentas local variablesmessages[len(prefix_messages):]with direct indexing usingprefix_countappend()+ singlereverse()instead of repeated list concatenation[item] + list4. Performance characteristics by test case:
The optimizations are most effective for large message lists where token counting dominates runtime, making this ideal for production scenarios with extensive conversation histories.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_0_gkn0tr/tmp4i8q2xt7/test_concolic_coverage.py::test_ContextManager_compress_messagescodeflash_concolic_0_gkn0tr/tmp4i8q2xt7/test_concolic_coverage.py::test_ContextManager_compress_messages_2codeflash_concolic_0_gkn0tr/tmp4i8q2xt7/test_concolic_coverage.py::test_ContextManager_compress_messages_3To edit these changes
git checkout codeflash/optimize-ContextManager.compress_messages-mguzugc1and push.