⚡️ Speed up function getpluginversioninfo by 57%
#85
+11
−4
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.
📄 57% (0.57x) speedup for
getpluginversioninfoinsrc/_pytest/helpconfig.py⏱️ Runtime :
896 microseconds→571 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 56% speedup by applying several micro-optimizations that reduce overhead in the plugin iteration loop:
Key Optimizations:
Local variable caching:
lines_append = lines.appendeliminates repeated attribute lookups on thelinesobject during each loop iteration.Smarter
getattrusage: Instead ofgetattr(plugin, "__file__", repr(plugin))which always evaluatesrepr(plugin)even when not needed, the code now usesgetattr(plugin, "__file__", None)and only callsrepr(plugin)when the file attribute is actually missing.Attribute access optimization:
dist.project_nameanddist.versionare cached to local variables (pn,ver) to avoid repeated attribute lookups within the f-string.Why This Matters:
The function is called from
showversion()andpytest_report_header()during pytest startup and debug output generation. While not in a critical hot path, the optimization provides significant benefits when many plugins are registered. Test results show the most dramatic improvements with larger plugin counts:Performance Impact by Use Case:
The optimization excels when plugins have
__file__attributes (common case), showing 30-67% improvements. For plugins without__file__attributes that requirerepr()calls, the optimization is more modest (5-6% faster) or occasionally slightly slower due to the additional conditional check, but this represents the minority case in typical pytest environments.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-getpluginversioninfo-mi9v02d9and push.