⚡️ Speed up function _replace_numpy_floats by 107%
#2
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.
📄 107% (1.07x) speedup for
_replace_numpy_floatsinspacy/language.py⏱️ Runtime :
6.11 milliseconds→2.95 milliseconds(best of250runs)📝 Explanation and details
The optimized version achieves a 107% speedup by replacing the generic
thinc.util.convert_recursivefunction with a specialized, direct recursive implementation.Key optimizations:
Eliminates external dependency overhead: The original code relies on
convert_recursive, which adds significant function call overhead and generic dispatch logic. The profiler shows this accounts for 99.8% of execution time (32.6ms out of 32.6ms total).Direct type checking and conversion: Instead of passing lambda functions to a generic recursive utility, the optimized version performs direct
isinstance()checks and conversions inline, eliminating multiple function call layers.Specialized data structure handling: The new implementation explicitly handles the common container types (dict, list, tuple) with optimized comprehensions rather than going through a generic conversion framework.
Performance characteristics:
The optimization is particularly effective for large-scale data processing scenarios, which appear common based on the test cases, making this a valuable improvement for any workload that processes substantial amounts of nested data containing numpy floats.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_replace_numpy_floats-mhlhxqhuand push.