⚡️ Speed up method ApproxMapping._repr_compare by 6%
#83
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.
📄 6% (0.06x) speedup for
ApproxMapping._repr_compareinsrc/_pytest/python_api.py⏱️ Runtime :
11.4 milliseconds→10.7 milliseconds(best of155runs)📝 Explanation and details
The optimized code achieves a 6% speedup through several micro-optimizations that reduce function call overhead and improve memory access patterns:
Key Optimizations:
Eliminated redundant list conversion in
_compare_approx: Added type check to avoid convertingmessage_datato list when it's already a list, saving unnecessary allocation.Optimized column width calculation: Replaced expensive
max()function calls with simple comparison branches (if l0 > max0), reducing function call overhead in the tight loop that processes message formatting.Cached method references: Stored
explanation.appendas a local variable and pre-formatted the string template to avoid repeated attribute lookups during message formatting.Improved type checking: Replaced
isinstance(x, Decimal)withtype(x) is Decimalfor faster type detection, avoiding MRO traversal.Reduced attribute lookups in hot path: In
_repr_compare, cachedself.rel,self.abs,self.nan_okas local variables and used direct__setitem__calls instead of dictionary assignment.Optimized difference calculations: Eliminated nested
max()calls by using temporary variables and direct comparisons formax_abs_diffandmax_rel_diffupdates.Streamlined message data construction: Used method references (
__getitem__) to avoid repeated attribute lookups when building the final message data.Performance Impact:
The optimizations show strongest gains for larger datasets with many mismatches - test cases with 300-1000 elements and high mismatch rates see 9-10% improvements, while smaller datasets see modest gains or slight regressions due to the overhead of additional type checks. The optimizations are particularly effective when processing large comparison results where the formatting and string operations dominate runtime.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-ApproxMapping._repr_compare-mi9t0d69and push.