perf: skip O(n) filehandle scan for path-based operations#381
Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Draft
perf: skip O(n) filehandle scan for path-based operations#381Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Conversation
_find_file_or_fh called _fh_to_file on every operation, even when the argument is clearly a path string. _fh_to_file iterates (with sort) over all entries in %files_being_mocked comparing stringified filehandles — a guaranteed mismatch for plain strings, but O(n log n) wasted work. Add a fast-path check: only call _fh_to_file when the argument is a reference (IO::Handle, glob ref) or a bare glob (*FH). Plain strings skip straight to _abs_path_to_file. Also remove the unnecessary sort in _fh_to_file — iteration order is irrelevant for equality-based lookups. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Skip the expensive
_fh_to_filescan when the argument to_find_file_or_fhis a plain path string.Why
_find_file_or_fhis the central dispatch for every mocked file operation. Previously it unconditionally called_fh_to_file, which iterates (withsort) over all%files_being_mockedentries comparing stringified filehandles — a guaranteed mismatch for path strings, but O(n log n) wasted work per call.Most operations are path-based (open, stat, unlink, mkdir, etc.), so this overhead applied to nearly every call.
How
_fh_to_file: only invoke it when the argument is a reference (IO::Handle, glob ref) or a bare glob (*FH). Plain strings go directly to_abs_path_to_file.sortin_fh_to_file— iteration order doesn't matter for equality lookups.Complementary to PR #352 (which makes
_fh_to_fileitself O(1) via reverse hash). This PR avoids calling it at all when unnecessary.Testing
Full test suite passes locally.
🤖 Generated with Claude Code
Quality Report
Changes: 30 files changed, 176 insertions(+), 1456 deletions(-)
Code scan: clean
Tests: passed (OK)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline