⚡️ Speed up function isinteractive by 24%
#248
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.
📄 24% (0.24x) speedup for
isinteractiveinlib/matplotlib/pyplot.py⏱️ Runtime :
2.46 milliseconds→1.99 milliseconds(best of96runs)📝 Explanation and details
The optimization achieves a 23% speedup through two key changes that eliminate redundant operations:
What was optimized:
Environment variable caching in
matplotlib/__init__.py: Instead of callingos.environ.get('MPLBACKEND')twice, the result is cached in_mplbackendand reused, eliminating one dictionary lookup.Direct rcParams access in
matplotlib/pyplot.py: Theisinteractive()function now directly imports and usesrcParams['interactive']instead of callingmatplotlib.is_interactive(), eliminating a function call indirection.Why this leads to speedup:
os.environ.get()) involves dictionary operations that have overhead when called multiple times.rcParams['interactive']avoids the Python function call stack overhead and module attribute traversal thatmatplotlib.is_interactive()requires.Impact on workloads:
Based on the function references,
isinteractive()is called byion()andioff()functions that manage matplotlib's interactive mode. These are commonly used functions in interactive plotting workflows, making this optimization valuable for:Test case performance:
The optimization shows consistent 20-50% improvements across all test scenarios, with particularly strong gains on:
This indicates the optimization is broadly beneficial regardless of usage patterns or input values.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-isinteractive-mja9rwwyand push.