⚡️ Speed up function _normalize_numbers by 10%
#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.
📄 10% (0.10x) speedup for
_normalize_numbersinskyvern/forge/sdk/core/security.py⏱️ Runtime :
706 microseconds→640 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 10% speedup by making two key changes to type checking and control flow:
What was optimized:
isinstance()withtype() is- Changed fromisinstance(x, float)totype(x) is floatfor exact type matchingifstatements toif/elif/elsechainWhy this is faster:
type(x) is floatis significantly faster thanisinstance(x, float)because it performs a direct identity comparison rather than traversing the inheritance hierarchyelif/elsestructure reduces redundant condition checking - once a type is matched, subsequent conditions are skipped entirelyPerformance benefits by test case:
Impact on workloads:
The function is called by
_normalize_json_dumps()for JSON serialization, making this optimization valuable for:The optimization maintains identical behavior while providing consistent performance gains across all test scenarios, with particularly strong benefits for large or deeply nested data structures.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_normalize_numbers-mirczof4and push.