Skip to content

perf: optimize hot paths in Clone.xs#74

Merged
atoomic merged 2 commits intogaru:masterfrom
atoomic:koan.atoomic/xs-perf-improvements
Feb 24, 2026
Merged

perf: optimize hot paths in Clone.xs#74
atoomic merged 2 commits intogaru:masterfrom
atoomic:koan.atoomic/xs-perf-improvements

Conversation

@Koan-Bot
Copy link
Contributor

@Koan-Bot Koan-Bot commented Feb 19, 2026

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_clone: pre-size target hash via hv_ksplit() (avoids O(log N) rehash cycles); use hv_iterkey() + hv_store() with HeHASH() reuse instead of hv_iterkeysv() + hv_store_ent() (avoids mortal SV allocation per hash key)
  • av_clone / av_clone_iterative: write directly to target AvARRAY slots instead of av_store() — safe since target is freshly created with no magic; source still read via av_fetch() for tied/magical array safety
  • sv_clone: move NULL guard before rdepth++; replace sv_isobject() with direct SvOBJECT(SvRV()) where SvROK is already verified
  • Magic loop: has_qr flag avoids second magic chain traversal via mg_find(); Newx replaces Newxz for mg_ptr copies (avoid zero-fill of immediately-overwritten buffers)
  • COW path: fix overly broad SvFLAGS(clone) = SvFLAGS(ref) that could propagate SVs_TEMP, SVf_READONLY and other context-specific flags
  • Dead code: remove unused rv_clone() function (reference cloning is inlined in sv_clone())

Net impact

  • 41 insertions, 59 deletions (net -18 lines)
  • All 254+ tests pass on both threaded (5.34) and non-threaded (5.42) Perl
  • No new dependencies or API changes

Test plan

  • make test passes on perlbrew Perl 5.42 (non-threaded)
  • make test passes on system Perl 5.34 (threaded) — includes t/16-threads-shared.t
  • CI matrix (Linux/Windows, Perl 5.8-latest)

🤖 Generated with Claude Code

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it s a good idea/win

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@atoomic atoomic marked this pull request as ready for review February 19, 2026 18:00
Koan-Bot and others added 2 commits February 23, 2026 17:24
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>
@Koan-Bot
Copy link
Contributor Author

Rebase: perf: optimize hot paths in Clone.xs

Branch koan.atoomic/xs-perf-improvements has been rebased onto master and force-pushed.

Actions

  • Read PR comments and review feedback
  • Rebased koan.atoomic/xs-perf-improvements onto origin/master
  • Applied review feedback
  • Force-pushed koan.atoomic/xs-perf-improvements

Automated by Kōan

@Koan-Bot Koan-Bot force-pushed the koan.atoomic/xs-perf-improvements branch from e5bc10d to 1c96fc7 Compare February 24, 2026 00:24
@atoomic atoomic merged commit e14343b into garu:master Feb 24, 2026
27 checks passed
@atoomic atoomic deleted the koan.atoomic/xs-perf-improvements branch February 24, 2026 00:35
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.

2 participants