⚡️ Speed up function _check_initialpaths_for_relpath by 140%
#67
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.
📄 140% (1.40x) speedup for
_check_initialpaths_for_relpathinsrc/_pytest/nodes.py⏱️ Runtime :
23.6 milliseconds→9.84 milliseconds(best of165runs)📝 Explanation and details
The optimization replaces the expensive
commonpathfunction call with Python's built-inPath.is_relative_to()method in the critical path checking loop.Key change: Instead of
if commonpath(path, initial_path) == initial_path:, the optimized version usesif path.is_relative_to(initial_path):. This eliminates the need to:Pathobjects to strings viastr()os.path.commonpath()which performs complex path resolutionPathobject from the resultinitial_pathWhy this is faster:
Path.is_relative_to()is a native pathlib method that directly checks the parent-child relationship without string conversions or intermediate object creation. The profiler shows the optimization reduces the hot loop from 98.5% of runtime (98ms) to 97.1% (48ms) - a ~50% reduction in the most expensive operation.Performance impact based on function references: The
_check_initialpaths_for_relpathfunction is called during pytest's node initialization whennodeidconstruction fails withValueError. This happens during test collection and node creation, making it part of pytest's startup path. The 139% speedup will meaningfully improve test discovery performance, especially in projects with complex directory structures or many initial paths.Test case effectiveness: The optimization shows consistent 50-185% speedups across all test scenarios, with particularly strong gains in large-scale cases (e.g., 1000+ initial paths) where the loop overhead compounds. Edge cases like relative paths, symlinks, and Unicode filenames all benefit equally since the core optimization avoids string processing entirely.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_check_initialpaths_for_relpath-mi9il9fwand push.