perf: use getpwnam() for tilde expansion, deduplicate hash lookup#380
Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Draft
perf: use getpwnam() for tilde expansion, deduplicate hash lookup#380Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Conversation
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>
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
Two targeted optimizations in frequently-called path resolution functions.
Why
Tilde expansion (
_abs_path_to_file): Thegetpwent()loop iteratedthrough 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).
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
endpwent/getpwentloop with singlegetpwnam()call_maybe_autovivifyTesting
No behavioral change — pure optimization. Existing tests cover both code paths.
🤖 Generated with Claude Code