⚡️ Speed up function _get_tokenizer_config_size by 9%
#5
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.
📄 9% (0.09x) speedup for
_get_tokenizer_config_sizeinsrc/cohere/manually_maintained/tokenizers.py⏱️ Runtime :
139 microseconds→128 microseconds(best of328runs)📝 Explanation and details
The optimized code replaces a for-loop with direct header access using Python's
oroperator for short-circuit evaluation.Key changes:
Eliminated the for-loop: Instead of iterating through
["x-goog-stored-content-length", "Content-Length"]and callinghead_response.headers.get()on each iteration, the code now usesheaders.get("x-goog-stored-content-length") or headers.get("Content-Length").Cached headers reference: Added
headers = head_response.headersto avoid repeated attribute access.Why this is faster:
.get()calls in most cases (74 hits in profiler), while the optimized version makes at most 2 calls but often just 1 due to short-circuit evaluation.Performance characteristics:
The optimization shows consistent 6-17% improvements across all test cases, with the best gains on edge cases like invalid values (14.3% faster) and large numbers (17.2% faster). The speedup is most pronounced when the first header (
x-goog-stored-content-length) is present, as it avoids the second.get()call entirely due to short-circuit evaluation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_tokenizer_config_size-mgzlyzrqand push.