⚡️ Speed up function check_allowlist by 10%
#83
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.
📄 10% (0.10x) speedup for
check_allowlistinsrc/bokeh/server/util.py⏱️ Runtime :
10.3 milliseconds→9.39 milliseconds(best of163runs)📝 Explanation and details
The optimized code achieves a 9% speedup through several key optimizations:
1. Early exit for empty allowlist: Added a fast path that immediately returns
Falsefor empty allowlists, avoiding unnecessary string operations. This provides massive gains (318% faster) for empty allowlist cases.2. Eliminated redundant string creation: The original code unconditionally modified the
hostparameter by adding:80, creating a new string object. The optimized version only createshost_with_portwhen needed, reducing memory allocations.3. Replaced generator expression with explicit loop: Changed from
any(match_host(host, pattern) for pattern in allowlist)to a regular for-loop with early return. This eliminates generator overhead and allows for immediate termination when a match is found.4. Cached function reference: Stored
match_hostin a local variablematchto avoid repeated attribute lookups during the loop iteration.5. Added fast path for universal wildcards: In
match_host, added early detection for patterns like'*'and'*:*'that match everything, avoiding expensive string splitting and comparison operations.6. Micro-optimizations in matching logic: Reordered conditions in the port matching logic to check the wildcard case (
p == '*') before exact match (h == p), as wildcards are common in allowlists.These optimizations are particularly effective for:
The optimizations maintain exact functional compatibility while reducing both computational overhead and memory allocations.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/server/test_util.py::test_check_allowlist_accepts_all_on_starunit/bokeh/server/test_util.py::test_check_allowlist_accepts_implicit_port_80unit/bokeh/server/test_util.py::test_check_allowlist_accepts_name_port_matchunit/bokeh/server/test_util.py::test_check_allowlist_rejects_name_mismatchunit/bokeh/server/test_util.py::test_check_allowlist_rejects_port_mismatch🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_5f34sbte/tmpbv_bfu_7/test_concolic_coverage.py::test_check_allowlistcodeflash_concolic_5f34sbte/tmpbv_bfu_7/test_concolic_coverage.py::test_check_allowlist_2To edit these changes
git checkout codeflash/optimize-check_allowlist-mhbl4xefand push.