Skip to content

perf: O(children) directory lookups via reverse index#356

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-dir-children-index
Draft

perf: O(children) directory lookups via reverse index#356
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-dir-children-index

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

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

What

Replace O(total_mocks) linear scans with O(children) indexed lookups for directory-related operations.

Why

_files_in_dir() scans every key in %files_being_mocked on each call. It's used by readdir (contents()), the dir() constructor's has_content check, and the ENOTEMPTY checks in rmdir and rename. For test suites with many mocked files, this is a hot path paying unnecessary O(n) cost.

How

New %_dir_children reverse index maps each ancestor directory to its set of descendant mock paths. Maintained at the same 5 mutation points that already touch %files_being_mocked:

  • new() constructor (registration)
  • _create_file_through_broken_symlink()
  • _maybe_autovivify()
  • DESTROY() (cleanup)
  • __rename() re-keying loop

Each path registers under ALL ancestor directories (not just the immediate parent), preserving the existing semantics where _files_in_dir('/a') returns descendants at any depth.

Testing

Full test suite passes (94 files, 1586 subtests). Initial run caught a semantic mismatch (direct-parent-only vs all-ancestors) in globbing.t — fixed before push.

🤖 Generated with Claude Code

Replace O(total_mocks) full-hash scans in _files_in_dir() and the
dir() constructor with O(children) lookups using a %_dir_children
reverse index. The index maps each ancestor directory to the set of
descendant paths registered under it.

Maintained at mock registration (new, autovivify, broken-symlink
creation), destruction (DESTROY), and rename re-keying — the same
five mutation points that touch %files_being_mocked.

For test suites with many mocked files, this eliminates per-readdir
and per-rmdir linear scans of the entire mock registry.

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