⚡️ Speed up function parse_datetime by 18%
#48
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.
📄 18% (0.18x) speedup for
parse_datetimeinsrc/openai/_utils/_compat.py⏱️ Runtime :
30.6 milliseconds→25.9 milliseconds(best of58runs)📝 Explanation and details
The optimized code achieves an 18% speedup by eliminating expensive dictionary operations in the datetime parsing path. The key optimization is replacing the original approach of creating a dictionary via
match.groupdict()and then iterating over it with{k: int(v) for k, v in kw.items() if v is not None}, with direct field extraction and conversion.Specific optimizations:
Direct field extraction: Instead of
kw = match.groupdict()followed by dictionary comprehension, the code directly accessesgd['year'],gd['month'], etc., eliminating the intermediate dictionary creation and iteration overhead.Conditional microsecond processing: The microsecond padding logic (
ljust(6, "0")) now only executes when microseconds are actually present, avoiding unnecessary string operations in cases without microseconds.Inline integer conversions: Fields are converted to integers immediately upon extraction (
int(gd['year'])) rather than through a dictionary comprehension, reducing function call overhead.Eliminated type annotation overhead: Removed the
Dict[str, Union[None, int, timezone]]type annotation for the intermediate dictionary since it's no longer needed.Why this is faster:
Dictionary operations in Python have significant overhead - creating dictionaries, iterating with
.items(), and key lookups are all expensive. The optimization eliminates these entirely for the common datetime parsing case. The test results show consistent 15-25% improvements for ISO string parsing cases, which represent the most common use pattern where these dictionary operations were the bottleneck.The optimizations are most effective for ISO string inputs (the majority of test cases showing 15-27% improvements), while having minimal impact on numeric timestamp inputs that bypass this parsing logic entirely.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-parse_datetime-mhe3n0mkand push.