⚡️ Speed up function is_platform_arm by 15%
#407
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.
📄 15% (0.15x) speedup for
is_platform_arminpandas/compat/__init__.py⏱️ Runtime :
859 microseconds→744 microseconds(best of141runs)📝 Explanation and details
The optimization eliminates redundant calls to
platform.machine()by storing its result in a variablemand reusing it. In the original code,platform.machine()is called twice - once for theincheck and potentially again for thestartswithcheck if the first condition fails. The optimized version calls it only once and stores the result.Key Performance Impact:
platform.machine()is a system-level function that queries the operating system for architecture information, making it relatively expensiveplatform.machine()up to twice per function invocation, while the optimized version always calls it exactly onceTest Results Analysis:
The optimization shows consistent 5-25% speedups across different scenarios:
The line profiler confirms this - the original version shows 6,795 total hits across both lines (4,534 + 2,261), indicating
platform.machine()was called multiple times per function call in some scenarios. The optimized version shows only 2,273 hits on the assignment line, proving the single-call optimization works as intended.This optimization is particularly valuable for code that calls
is_platform_arm()frequently, as each avoided system call compounds the performance benefit.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_platform_arm-mj9vmuvband push.