⚡️ Speed up function _assertion_supported by 71%
#76
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.
📄 71% (0.71x) speedup for
_assertion_supportedinsrc/_pytest/config/__init__.py⏱️ Runtime :
409 microseconds→240 microseconds(best of196runs)📝 Explanation and details
The optimization replaces a try-except block that tests assertion functionality with a direct return of the
__debug__built-in variable, achieving a 70% speedup.Key Changes:
AssertionErrorwithassert Falseand catches it to determine if assertions are enabled. This involves Python's exception machinery (try/except/raise/catch).__debug__, a built-in boolean that Python automatically sets toFalsewhen running with optimization flags (-Oor-OO) andTrueotherwise.Why This is Faster:
The line profiler shows the original code spends significant time across multiple operations:
assert False(triggering the exception)except AssertionError(exception handling)return True(returning from except block)The optimized version eliminates all exception overhead, reducing execution from 4 lines with exception handling to a single variable lookup.
Impact on Workloads:
Based on the function reference, this is called from
_warn_about_missing_assertion()which checks if Python is running with optimization flags that disable assertions. The optimization is particularly beneficial for:Test Case Performance:
All test cases show consistent 70-130% improvements, with the optimization being most effective for bulk operations (list comprehensions, multiple calls) where the exception overhead compounds.
The optimization maintains identical functionality since
__debug__is the canonical way to check if assertions are enabled in Python.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__lsdxkww/tmprsp118bb/test_concolic_coverage.py::test__assertion_supportedTo edit these changes
git checkout codeflash/optimize-_assertion_supported-mi9p60upand push.