⚡️ Speed up method Time.validate by 28%
#150
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.
📄 28% (0.28x) speedup for
Time.validateinsrc/bokeh/core/property/datetime.py⏱️ Runtime :
4.31 milliseconds→3.38 milliseconds(best of79runs)📝 Explanation and details
The optimized code achieves a 27% speedup by making two key changes to the
Time.validate()method:1. Removed superclass call overhead: The original code calls
super().validate(value, detail), but the baseProperty.validate()method is just apassstatement (confirmed by line profiler showing it takes 60% of total time). Removing this eliminates unnecessary method call overhead.2. Hoisted attribute lookup: The optimized code extracts
datetime.time.fromisoformatinto a local variablefromisoformatbefore the try block. This avoids repeatedly resolving the attribute accessdatetime.time.fromisoformaton each validation call, reducing attribute lookup overhead.The line profiler shows the superclass call (
super().validate(value, detail)) consumed 60.2% of the original runtime but is completely eliminated in the optimized version. The attribute hoisting provides additional micro-optimization benefits.Performance gains vary by input type:
The
validate()method is likely called frequently during data validation workflows, making these optimizations valuable for applications processing large datasets with time values. The changes maintain identical functionality and error handling behavior while significantly reducing validation overhead.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Time.validate-mhwr7qmuand push.