⚡️ Speed up function check_session_id_signature by 33%
#121
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.
📄 33% (0.33x) speedup for
check_session_id_signatureinsrc/bokeh/util/token.py⏱️ Runtime :
470 microseconds→354 microseconds(best of50runs)📝 Explanation and details
The optimized code achieves a 32% speedup through three key optimizations that reduce function call overhead and unnecessary work:
1. Conditional Secret Key Processing
The original code always called
_ensure_bytes(secret_key)at the function start, even for unsigned sessions (the majority case). The optimized version only processes the secret key whensigned=True, eliminating this overhead for ~83% of calls based on the test patterns.2. String Encoding Optimization
Replaced
codecs.encode(string, 'utf-8')withstring.encode('utf-8')in both_ensure_bytesand_signature. The.encode()method is a direct C-level operation that's significantly faster than the more genericcodecs.encode()function call.3. Inlined Base64 Encoding
The original code called
_base64_encode(signer.digest())which had additional overhead from function calls and usedcodecs.decode(). The optimized version inlines this logic using directbase64.urlsafe_b64encode()and.decode('ascii'), eliminating the extra function call and codec overhead.Performance Impact by Use Case:
Hot Path Context:
Given that
check_session_id_signatureis called fromcheck_token_signature(which validates both tokens and their contained session IDs), this optimization directly benefits Bokeh server authentication workflows where session validation occurs frequently during client-server interactions.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_sstvtaha/tmpu1rg1kdi/test_concolic_coverage.py::test_check_session_id_signature_2codeflash_concolic_sstvtaha/tmpu1rg1kdi/test_concolic_coverage.py::test_check_session_id_signature_3To edit these changes
git checkout codeflash/optimize-check_session_id_signature-mhw6whvaand push.