⚡️ Speed up function _dtype_to_init_repr_struct by 45%
#112
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.
📄 45% (0.45x) speedup for
_dtype_to_init_repr_structinpy-polars/src/polars/datatypes/_utils.py⏱️ Runtime :
830 microseconds→574 microseconds(best of241runs)📝 Explanation and details
The optimization achieves a 44% speedup through two key performance improvements:
1. Type-based dispatch optimization:
isinstance(dtype, List)checks with direct type comparisonstype(dtype) is Listisinstance()performs2. Struct processing efficiency:
dict(dtype)conversion by callingdtype.items()directly when availableappend()instead of a list comprehension, which is more memory-efficient for complex nested structuresPerformance characteristics:
dtype_to_init_reprare frequentThe test results show consistent performance gains across various scenarios, from simple single-field structs to deeply nested structures with 1000+ fields, indicating the optimizations scale well with complexity.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_dtype_to_init_repr_struct-mgyu9mnuand push.