Skip to content

optimization: cache contents() in __open paths#336

Merged
atoomic merged 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/cache-contents-in-open
Mar 28, 2026
Merged

optimization: cache contents() in __open paths#336
atoomic merged 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/cache-contents-in-open

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

What

Cache $mock_file->contents() result before first use in __open and _io_file_mock_open.

Why

contents() for directories scans all keys of %files_being_mocked, filters by path prefix, and sorts — an O(n) operation. Both functions called it twice per open(): once for the existence check and again for the permission check. With many mocked files, this doubled the cost of every open() call.

How

Store my $contents = $mock_file->contents() once, then use $contents in both the ENOENT check and the permission check.

Testing

open.t, io_file_compat.t, perms.t, sysopen.t all pass.

Closes #332

🤖 Generated with Claude Code

Cache the result of $mock_file->contents() before its first use in
both __open and _io_file_mock_open. For directories, contents() scans
all keys of %files_being_mocked and sorts — calling it twice per
open() was redundant work that grows linearly with mock count.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@atoomic atoomic marked this pull request as ready for review March 28, 2026 13:13
@atoomic atoomic merged commit 712f8d0 into cpan-authors:main Mar 28, 2026
19 checks passed
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.

optimization: ->contents() called twice in __open without caching; method is non-trivial for directories

2 participants