⚡️ Speed up function _compute_datetime_types by 10%
#60
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
_compute_datetime_typesinsrc/bokeh/util/serialization.py⏱️ Runtime :
172 microseconds→157 microseconds(best of243runs)📝 Explanation and details
The optimization achieves a 9% speedup by making two key changes to the
_compute_datetime_types()function:1. Set Literal Construction Optimization:
The original code creates an empty set and uses multiple
.add()calls to populate it. The optimized version constructs the set directly using a set literal with all types included upfront. This eliminates the overhead of multiple method calls and intermediate set resizing operations.2. Import Statement Reorganization:
While the pandas import remains local to the function (preserving lazy loading behavior), the other imports (
datetime,numpy) are moved to module level. This reduces the function's execution overhead slightly, though the primary benefit comes from the set construction change.Why This Works:
.add()method calls reduces function call overhead@lru_cache(None)decorator ensures this optimization only needs to run once per process in productionTest Case Performance:
The optimization shows consistent 10-20% improvements across most test cases, with particularly strong gains (up to 54%) in cache-miss scenarios. The optimization is most effective for:
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_compute_datetime_types-mhb2n3bdand push.