perf: optimize hot paths in Clone.xs#74
Merged
atoomic merged 2 commits intogaru:masterfrom Feb 24, 2026
Merged
Conversation
atoomic
approved these changes
Feb 19, 2026
atoomic
reviewed
Feb 19, 2026
Clone.xs
Outdated
| if (mg->mg_len >= 0) { /* copy the pv */ | ||
| if (mg_ptr) { | ||
| Newxz(mg_ptr, mg->mg_len+1, char); /* add +1 for the NULL at the end? */ | ||
| Newx(mg_ptr, mg->mg_len+1, char); |
Collaborator
There was a problem hiding this comment.
not sure if it s a good idea/win
atoomic
reviewed
Feb 19, 2026
Clone.xs
Outdated
| if (mg->mg_ptr) { | ||
| STRLEN *cache; | ||
| Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); | ||
| Newx(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN); |
Key performance improvements while preserving readability: - hv_clone: pre-size target hash with hv_ksplit() to avoid incremental resizing; use hv_iterkey() + hv_store() with HeHASH() to skip mortal SV allocation per key - av_clone/av_clone_iterative: write directly to target AvARRAY (safe since target is freshly created with no magic); source still read via av_fetch for tied/magical array safety - sv_clone: move NULL check before rdepth increment; replace sv_isobject() with direct SvOBJECT(SvRV()) where SvROK already verified - Magic loop: track has_qr flag to skip mg_find traversal when no qr magic present; use Newx instead of Newxz for mg_ptr copies - COW: fix overly broad SvFLAGS copy that could propagate SVs_TEMP, SVf_READONLY and other context-specific flags to clones - Remove dead rv_clone() function (never called, ref cloning is inlined in sv_clone) All changes tested on both threaded (system Perl 5.34) and non-threaded (perlbrew 5.42) builds. 254+ tests pass including threads::shared and DBI handle tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
Rebase: perf: optimize hot paths in Clone.xsBranch Actions
Automated by Kōan |
e5bc10d to
1c96fc7
Compare
atoomic
approved these changes
Feb 24, 2026
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.
Summary
Performance optimizations for the core cloning functions in Clone.xs, focused on reducing per-element overhead while keeping the code readable and maintainable.
Key changes
hv_ksplit()(avoids O(log N) rehash cycles); usehv_iterkey()+hv_store()withHeHASH()reuse instead ofhv_iterkeysv()+hv_store_ent()(avoids mortal SV allocation per hash key)AvARRAYslots instead ofav_store()— safe since target is freshly created with no magic; source still read viaav_fetch()for tied/magical array safetyrdepth++; replacesv_isobject()with directSvOBJECT(SvRV())whereSvROKis already verifiedhas_qrflag avoids second magic chain traversal viamg_find();NewxreplacesNewxzfor mg_ptr copies (avoid zero-fill of immediately-overwritten buffers)SvFLAGS(clone) = SvFLAGS(ref)that could propagateSVs_TEMP,SVf_READONLYand other context-specific flagsrv_clone()function (reference cloning is inlined insv_clone())Net impact
Test plan
make testpasses on perlbrew Perl 5.42 (non-threaded)make testpasses on system Perl 5.34 (threaded) — includest/16-threads-shared.t🤖 Generated with Claude Code