⚡️ Speed up function convert_date_to_datetime by 41%
#61
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.
📄 41% (0.41x) speedup for
convert_date_to_datetimeinsrc/bokeh/util/serialization.py⏱️ Runtime :
4.38 milliseconds→3.11 milliseconds(best of133runs)📝 Explanation and details
The optimized version achieves a 40% speedup by eliminating the expensive
timetuple()call and slice operation from the original implementation.Key optimization:
dt.datetime(*obj.timetuple()[:6], tzinfo=dt.timezone.utc)creates a fullstruct_timetuple (9 elements), slices it to get the first 6 elements, then unpacks them into the datetime constructor.obj.year,obj.month,obj.dayattributes and constructs the datetime with only the needed components.Why this is faster:
The
timetuple()method performs unnecessary work by computing all time components (including hour, minute, second, weekday, yearday, dst flag) when only year/month/day are needed. The slice operation[:6]and tuple unpacking*add additional overhead.Additional improvement:
Added an
isinstance(obj, dt.datetime)check to handle datetime objects more efficiently by usingreplace(tzinfo=dt.timezone.utc)instead of reconstructing them entirely.Performance characteristics:
✅ 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-mhb2v3xkand push.