⚡️ Speed up function Node_fspath by 293%
#66
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.
📄 293% (2.93x) speedup for
Node_fspathinsrc/_pytest/legacypath.py⏱️ Runtime :
153 microseconds→38.9 microseconds(best of51runs)📝 Explanation and details
The optimization applies LRU caching to the
legacy_pathfunction using@lru_cache(maxsize=256). This provides a 292% speedup by avoiding repeated instantiation ofLEGACY_PATHobjects for the same path inputs.Key optimization:
functools.lru_cachedecorator with a 256-entry cache tolegacy_path()LEGACY_PATH(path)object creation is expensive (~13μs per call), but the function is deterministic - same input always produces equivalent output. The cache stores recently created instances and returns them directly for repeated calls.Performance impact:
LEGACY_PATH(path)construction entirelyWorkload suitability:
This optimization excels when:
LEGACY_PATHinstancesThe cache particularly benefits scenarios with repeated path access patterns common in test frameworks, where the same file paths are referenced multiple times during test collection and execution.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Node_fspath-mi9gq3cland push.