⚡️ Speed up function make_id by 39%#158
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization achieves a **39% speedup** by eliminating expensive repeated imports inside the `make_id()` function. **Key optimizations:** - **Cached import at module level**: The `from ..core.types import ID` statement was moved from inside both functions to the top of the module (aliased as `_CoreID`). The line profiler shows this import was consuming **17.3%** of the original function's runtime. - **Added missing module-level variables**: The `_simple_id` counter and `_simple_id_lock` were properly defined at module scope, which were missing in the original code but required for functionality. **Why this works:** Python's import mechanism has overhead for resolving module paths and namespaces. The original code performed `from ..core.types import ID` on every function call - with 82 hits in the profile, this accumulated significant cost. Moving the import to module initialization time means it only executes once when the module loads, not on every function call. **Impact on existing workloads:** Based on the function references, `make_id()` is called frequently in hot paths: - **Document callbacks**: Used for generating IDs for periodic, timeout, and next-tick callbacks in Bokeh server sessions - **Serialization**: Called during byte encoding operations, which could be frequent during data visualization rendering The test results show consistent **34-47% improvements** across all scenarios, with the optimization being particularly effective for: - Batch operations (1000 ID generations) - Simple ID mode (most common usage pattern) - Mixed workloads switching between simple and UUID modes This optimization maintains all thread safety guarantees and functional behavior while significantly reducing per-call overhead in performance-critical visualization workflows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 39% (0.39x) speedup for
make_idinsrc/bokeh/util/serialization.py⏱️ Runtime :
538 microseconds→387 microseconds(best of46runs)📝 Explanation and details
The optimization achieves a 39% speedup by eliminating expensive repeated imports inside the
make_id()function.Key optimizations:
from ..core.types import IDstatement was moved from inside both functions to the top of the module (aliased as_CoreID). The line profiler shows this import was consuming 17.3% of the original function's runtime._simple_idcounter and_simple_id_lockwere properly defined at module scope, which were missing in the original code but required for functionality.Why this works:
Python's import mechanism has overhead for resolving module paths and namespaces. The original code performed
from ..core.types import IDon every function call - with 82 hits in the profile, this accumulated significant cost. Moving the import to module initialization time means it only executes once when the module loads, not on every function call.Impact on existing workloads:
Based on the function references,
make_id()is called frequently in hot paths:The test results show consistent 34-47% improvements across all scenarios, with the optimization being particularly effective for:
This optimization maintains all thread safety guarantees and functional behavior while significantly reducing per-call overhead in performance-critical visualization workflows.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_adding_next_tick_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_adding_periodic_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_adding_timeout_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_next_tick_does_not_run_if_removed_immediatelyunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_next_tick_runsunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_periodic_does_not_run_if_removed_immediatelyunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_periodic_runsunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_remove_all_callbacksunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_removing_next_tick_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_removing_periodic_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_removing_timeout_twiceunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_same_callback_as_all_three_typesunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_timeout_does_not_run_if_removed_immediatelyunit/bokeh/server/test_callbacks__server.py::TestCallbackGroup.test_timeout_runsunit/bokeh/util/test_util__serialization.py::Test_make_id.test_defaultunit/bokeh/util/test_util__serialization.py::Test_make_id.test_simple_ids_nounit/bokeh/util/test_util__serialization.py::Test_make_id.test_simple_ids_yes🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-make_id-mhwvs2osand push.