⚡️ Speed up function parse_warning_filter by 239%
#77
+40
−48
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.
📄 239% (2.39x) speedup for
parse_warning_filterinsrc/_pytest/config/__init__.py⏱️ Runtime :
6.66 milliseconds→1.97 milliseconds(best of67runs)📝 Explanation and details
The optimized code achieves a 238% speedup through several key micro-optimizations that reduce function call overhead and improve memory access patterns:
Key Optimizations:
Module Import Hoisting: Moving
import builtins as _builtinsto module level eliminates repeated import overhead in_resolve_warning_category, which is called frequently during warning filter parsing.String Concatenation over dedent(): Replaced
dedent()calls with direct string concatenation for error templates, avoiding the overhead of text processing functions that analyze indentation.List Comprehension Optimization: Changed
parts.append("")loop toparts += [""] * (5 - nparts)for more efficient list extension when padding to 5 elements.Tuple Construction in import: Replaced
[klass]with(klass,)in the__import__call, avoiding temporary list creation.Reduced Function Call Overhead: Cached
len(parts)asnpartsto avoid repeatedlen()calls, and streamlined variable assignments.Performance Impact:
The function references show this optimization is highly impactful because
parse_warning_filteris called in hot paths:@pytest.mark.filterwarningsdecoratorsTest Case Performance:
The optimizations are particularly effective for pytest's usage pattern where the same warning filters are parsed repeatedly across many test runs, making the reduced per-call overhead compound significantly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-parse_warning_filter-mi9plvitand push.