⚡️ Speed up function convert_date_to_datetime by 52%#157
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up function convert_date_to_datetime by 52%#157codeflash-ai[bot] wants to merge 1 commit into
convert_date_to_datetime by 52%#157codeflash-ai[bot] wants to merge 1 commit into
Conversation
The optimization replaces the inefficient `obj.timetuple()[:6]` tuple construction and unpacking with direct attribute access (`obj.year`, `obj.month`, `obj.day`). Additionally, it moves the `DT_EPOCH` constant definition to module level to avoid recalculating it on every function call. **Key changes:** - **Eliminated tuple creation overhead**: The original code calls `timetuple()` which creates a 9-element tuple, then slices it to 6 elements, then unpacks with `*`. The optimized version directly accesses the three needed attributes. - **Moved constant to module level**: `DT_EPOCH` is now calculated once at import time rather than being undefined in the original (though it appears to be imported from elsewhere). **Why it's faster:** - `timetuple()` is a relatively expensive method that constructs a full time.struct_time object with 9 fields - Tuple slicing and unpacking add additional overhead - Direct attribute access on date objects is much faster as it simply returns the stored integer values - The line profiler shows a 38% reduction in per-hit time (1625ns → 1001ns) **Impact on workloads:** Based on the function references, this function is called from: 1. **Property transformation in hot path**: Used in `bokeh/core/property/datetime.py` during property value transformation, which happens frequently during plot creation and updates 2. **General datetime serialization**: Called from `convert_datetime_type()` for all date objects being serialized **Test case performance:** The optimization shows consistent 50-75% speedups across all test scenarios: - Basic conversions: 50-75% faster - Edge cases (min/max dates): 49-59% faster - Large scale operations (1000+ dates): 50-52% faster This optimization is particularly beneficial for applications that process many date objects, such as time series visualization in Bokeh, where this function could be called thousands of times during data preparation.
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.
📄 52% (0.52x) speedup for
convert_date_to_datetimeinsrc/bokeh/util/serialization.py⏱️ Runtime :
3.99 milliseconds→2.63 milliseconds(best of143runs)📝 Explanation and details
The optimization replaces the inefficient
obj.timetuple()[:6]tuple construction and unpacking with direct attribute access (obj.year,obj.month,obj.day). Additionally, it moves theDT_EPOCHconstant definition to module level to avoid recalculating it on every function call.Key changes:
timetuple()which creates a 9-element tuple, then slices it to 6 elements, then unpacks with*. The optimized version directly accesses the three needed attributes.DT_EPOCHis now calculated once at import time rather than being undefined in the original (though it appears to be imported from elsewhere).Why it's faster:
timetuple()is a relatively expensive method that constructs a full time.struct_time object with 9 fieldsImpact on workloads:
Based on the function references, this function is called from:
bokeh/core/property/datetime.pyduring property value transformation, which happens frequently during plot creation and updatesconvert_datetime_type()for all date objects being serializedTest case performance:
The optimization shows consistent 50-75% speedups across all test scenarios:
This optimization is particularly beneficial for applications that process many date objects, such as time series visualization in Bokeh, where this function could be called thousands of times during data preparation.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/core/property/test_datetime.py::Test_Datetime.test_transform_dateunit/bokeh/core/property/test_datetime.py::Test_Datetime.test_transform_strunit/bokeh/models/widgets/test_slider.py::TestDateRangeSlider.test_value_as_date_when_set_as_timestampunit/bokeh/models/widgets/test_slider.py::TestDateRangeSlider.test_value_as_date_when_set_mixedunit/bokeh/models/widgets/test_slider.py::TestDateSlider.test_value_and_value_throttledunit/bokeh/models/widgets/test_slider.py::TestDateSlider.test_value_as_date_when_set_as_timestamp🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-convert_date_to_datetime-mhwvekitand push.