Skip to content

fix: count nesting depth, not call count, in rdepth#139

Draft
Koan-Bot wants to merge 3 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-rdepth-nesting-count
Draft

fix: count nesting depth, not call count, in rdepth#139
Koan-Bot wants to merge 3 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-rdepth-nesting-count

Conversation

@Koan-Bot

@Koan-Bot Koan-Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

What

Make rdepth track actual nesting depth (RV dereferences) instead of total sv_clone() call count.

Why

rdepth incremented on every sv_clone() call, including sibling elements within flat arrays and hashes. A flat array of N > MAX_DEPTH elements falsely triggered the iterative fallback even though the actual nesting depth was only 2. The iterative path historically had more bugs than the recursive path, so entering it unnecessarily was both a correctness risk and a performance penalty.

How

  • Removed rdepth++ from the top of sv_clone().
  • Added rdepth + 1 at the only point that increases nesting: the RV dereference (SvRV(clone) = sv_clone(SvRV(ref), ..., rdepth + 1, ...)).
  • Sibling elements in av_clone/hv_clone loops keep the same rdepth as their parent.
  • Halved MAX_DEPTH (4000→2000, 2000→1000) since rdepth now counts nesting levels directly. Effective stack usage unchanged.

Testing

  • All 28 existing tests pass (364 subtests).
  • Added 5 new tests for wide structures: flat arrays and hashes with 3×MAX_DEPTH elements, verifying correct cloning, element count, and clone independence.
  • Updated depth constants in 6 test files to match new MAX_DEPTH semantics.

Quality Report

Changes: 7 files changed, 97 insertions(+), 37 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Koan-Bot and others added 3 commits May 13, 2026 03:11
rdepth previously incremented on every sv_clone() call, including
sibling elements within arrays and hashes.  A flat array of N>MAX_DEPTH
elements would falsely trigger the iterative fallback even though the
actual nesting depth was only 2 (one RV + one AV).

Move rdepth increment from the top of sv_clone() to the single point
that increases nesting: the RV dereference (rdepth+1 when cloning
the referent).  Sibling elements in av_clone/hv_clone loops now keep
the same rdepth as their parent container.

Halve MAX_DEPTH (4000→2000 Linux/macOS, 2000→1000 Windows) since
rdepth now counts nesting levels directly rather than call pairs.
The effective stack usage and safety margins remain identical.

Add tests for wide structures (arrays/hashes with 3×MAX_DEPTH elements)
to verify they never trigger the iterative path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
install-with-cpm@v1 regressed on Perl 5.8-5.22 after a recent cpm
update requiring Perl 5.24+.  Bump to @v2 for action-based jobs
and use direct `cpm install` in the Docker-based Linux matrix
(where cpm is pre-installed).

Matches the approach approved by @atoomic in PR garu#120.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The cycle back-edge at chain_len/2 landed exactly at the
recursive/iterative boundary on Windows (MAX_DEPTH=1000),
causing rv_clone_iterative to loop on the cycle before hseen
had registered the target.  Move the target to chain_len*3/4
so it falls in the recursive range on all platforms — hseen
catches the back-edge during the iterative walk.

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