⚡️ Speed up function _signature by 23%
#124
Open
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.
📄 23% (0.23x) speedup for
_signatureinsrc/bokeh/util/token.py⏱️ Runtime :
4.82 milliseconds→3.91 milliseconds(best of125runs)📝 Explanation and details
The optimization achieves a 23% speedup by replacing higher-level
codecsmodule functions with direct string/bytes method calls, eliminating unnecessary function lookups and indirection.Key optimizations applied:
Direct string encoding: Replaced
codecs.encode(secret_key, 'utf-8')withsecret_key.encode('utf-8')in_ensure_bytes(). This eliminates the overhead of looking up thecodecs.encodefunction and its internal dispatch logic.Direct bytes decoding: Replaced
codecs.decode(base64.urlsafe_b64encode(...), 'ascii')withbase64.urlsafe_b64encode(...).decode('ascii')in_base64_encode(). This removes an extra layer of function indirection.Streamlined type handling: In
_base64_encode(), the input conversion is now a single inline conditional expression instead of calling_ensure_bytes(), reducing function call overhead.Consistent direct encoding: In
_signature(), replacedcodecs.encode(base_id, "utf-8")withbase_id.encode('utf-8')for consistency.Why this matters for performance:
codecsmodule functions are generic and handle many encoding types, adding dispatch overheadImpact on workloads:
Based on the function references,
_signature()is called during:The test results show 16-48% improvements across various input types, with the optimization being particularly effective for ASCII strings (48% faster) and moderate improvements for Unicode/large inputs (30-40% faster). This makes the optimization valuable for typical web application workloads where session management happens frequently.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/util/test_token.py::TestSessionId.test_signature🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_sstvtaha/tmpoxdl72nn/test_concolic_coverage.py::test__signature_2To edit these changes
git checkout codeflash/optimize-_signature-mhw7idzvand push.