Skip to content

perf: skip O(n) filehandle scan for path-based operations#381

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-skip-fh-lookup-for-paths
Draft

perf: skip O(n) filehandle scan for path-based operations#381
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-skip-fh-lookup-for-paths

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented Apr 17, 2026

What

Skip the expensive _fh_to_file scan when the argument to _find_file_or_fh is a plain path string.

Why

_find_file_or_fh is the central dispatch for every mocked file operation. Previously it unconditionally called _fh_to_file, which iterates (with sort) over all %files_being_mocked entries 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

  • Add a type check before calling _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.
  • Remove the unnecessary sort in _fh_to_file — iteration order doesn't matter for equality lookups.

Complementary to PR #352 (which makes _fh_to_file itself 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

_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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant