Skip to content

perf: use getpwnam() for tilde expansion, deduplicate hash lookup#380

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-path-resolution
Draft

perf: use getpwnam() for tilde expansion, deduplicate hash lookup#380
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/perf-path-resolution

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

What

Two targeted optimizations in frequently-called path resolution functions.

Why

  1. Tilde expansion (_abs_path_to_file): The getpwent() loop iterated
    through ALL system password entries sequentially to find one username.
    On systems with LDAP/AD backends, this can be extremely slow. getpwnam()
    does the same lookup via NSS in O(1).

  2. Autovivify check (_maybe_autovivify): %files_being_mocked{$path}
    was accessed twice — once for the boolean check, once for the return value.
    Single lookup with if (my $existing = ...) eliminates the redundant access.

How

  • Replace 10-line endpwent/getpwent loop with single getpwnam() call
  • Cache hash lookup result in _maybe_autovivify

Testing

No behavioral change — pure optimization. Existing tests cover both code paths.

🤖 Generated with Claude Code

Two targeted optimizations in hot path functions:

1. _abs_path_to_file tilde expansion: replace getpwent() iteration loop
   with direct getpwnam() call. The old code reset the passwd iterator
   and scanned all entries sequentially to find one username — O(n) in
   the number of system users. getpwnam() does the same lookup via NSS
   in O(1).

2. _maybe_autovivify: cache the hash lookup result instead of accessing
   %files_being_mocked{$abs_path} twice (once for the truthiness check,
   once for the return value).

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