⚡️ Speed up function make_id by 34%
#62
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.
📄 34% (0.34x) speedup for
make_idinsrc/bokeh/util/serialization.py⏱️ Runtime :
580 microseconds→433 microseconds(best of37runs)📝 Explanation and details
The optimization achieves a 33% speedup by eliminating expensive dynamic imports and function call overhead:
Key Changes:
Removed redundant dynamic imports: The original code imported
IDinside both functions on every call (from ..core.types import ID). The optimized version uses the already-importedIDat module level, eliminating ~16% of execution time based on line profiler results.Inlined
make_globally_unique_id()logic: Instead of calling a separate function for UUID generation, the optimized code directly executesID(str(uuid.uuid4()))withinmake_id(), avoiding function call overhead and another dynamic import.Added missing global variables: Moved
_simple_idand_simple_id_lockdefinitions to module level (they were missing in the original), ensuring proper initialization.Why This Works:
from ..core.types import ID) took 15.9% of total time in the originalPerformance Benefits:
The test results show consistent 25-47% improvements across different scenarios:
This optimization is particularly effective for high-frequency ID generation workloads where
make_id()is called repeatedly, as it eliminates per-call import overhead while preserving all original functionality and thread safety.✅ 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-mhb33dboand push.