Skip to content

Comments

perf: optimize hot paths with string comparisons and reduced allocations#19

Draft
Koan-Bot wants to merge 1 commit intocern-mig:masterfrom
atoomic:koan.atoomic/perf-improvements-v2
Draft

perf: optimize hot paths with string comparisons and reduced allocations#19
Koan-Bot wants to merge 1 commit intocern-mig:masterfrom
atoomic:koan.atoomic/perf-improvements-v2

Conversation

@Koan-Bot
Copy link

Summary

Replace regex with string equality checks (eq) in high-frequency code paths, and eliminate unnecessary intermediate allocations.

Changes

  • _special_getdir (Queue.pm): Use ne string comparison instead of regex /^\.\.?$/ for filtering . and .. entries — this function is called on every first()/next()/count()/purge() operation
  • count() (Normal.pm, Simple.pm): Eliminate intermediate @list allocation — filter and count in a single pass instead of two loops
  • get() (Normal.pm): Cache $self->{type}{$name} in a local $type variable to avoid repeated hash lookups; replace regex dispatch (=~ /^(binary|string)$/) with eq comparisons
  • _add_data() (Normal.pm): Same local variable caching and eq dispatch
  • _hash2string() / _string2hash() (Normal.pm): Add index() early bailout before s///g substitutions — most hash keys/values don't contain special chars, so the substitution is typically a no-op
  • _purge_dir() (Simple.pm): Use index($_, ".") >= 0 instead of regex /\./
  • purge() option validation (Normal.pm, Simple.pm): Use eq instead of regex for matching "maxtemp" / "maxlock"

Rationale

Perl eq is ~3-5x faster than simple regex for string matching. The index() early bailout avoids the overhead of regex substitution when there's nothing to substitute (the common case for table data). The count() single-pass optimization eliminates an intermediate array allocation that was only used to iterate immediately after.

All changes are behavior-preserving. 237 tests pass.


🤖 Generated with Claude Code

Replace regex with string equality checks in high-frequency code paths:
- _special_getdir: use 'ne' instead of regex for . and .. filtering
- count(): eliminate intermediate list allocation, filter and count in one pass
- get()/add_data(): cache $self->{type}{$name} in local variable, replace
  regex dispatch with eq comparisons
- _hash2string/_string2hash: add index() early bailout before s///g
  (most hash keys/values don't contain special chars)
- _purge_dir: use index() instead of regex for dot detection
- purge option validation: use eq instead of regex

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