Skip to content

Avoid fallback character Strings during replacement transcoding - #408

Merged
284km merged 3 commits into
mainfrom
codex-misc-encode-replacement-allocation
Nov 30, 2026
Merged

284km merged 3 commits into
mainfrom
codex-misc-encode-replacement-allocation

Conversation

@284km

@284km 284km commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

When undef: :replace is selected, emit the replacement bytes directly. The old path built a Ruby String for every unrepresentable character before discovering that no fallback was needed. Ruby 4.0.10 also ignores fallback in replacement mode; this fixes the default-replacement path that incorrectly called Hash/Proc/object fallbacks. Fallback behavior without undef: :replace is preserved.

Corpus 1162 covers default/explicit replacement, nil/missing/callable fallbacks, ignored raising fallbacks, fallback-mode controls, ASCII/Latin/binary conversions, newline handling, encode!, and repeated conversions across GC. It matches ruby on both engines, including GC stress; existing fixtures 336, 349, 391 and 416 match on main and the candidate.

Original CRuby file comparison: unchanged ruby/test_transcode.rb, 14 relevant replacement/fallback/error/argument/newline/XML tests: 14 tests, 172 assertions, 0 failures/errors/skips on ruby, main evaluator/VM, and candidate evaluator/VM.

Merged main 9d1c0f3 (#409 included) and rebuilt with pinned Mere 9dec2ff6, mgz 661a01d8, ordinary O2. Reference: clean ruby 4.0.10 via tools/ref_ruby.sh. Candidate SHA-256: b103b76138012473e1d14800048366b52d01adbc572a7f95a9a5f5cbe64113d6. Runtime input hashes are checked before and after validation; evaluator runs leave MERE_RUBY_VM unset.

  • sh run_corpus.sh --both: 1040/1040 PASS, index on/off.
  • MERE_RUBY_VM=1 sh run_corpus.sh --both: 1040/1040 PASS, index on/off.
  • MERE_RUBY_VM=1 sh tools/iseq/vm_corpus.sh -j 2: 1040 on the VM, 0 fallback, 0 FAIL.
  • All source gates pass, including StrBuf region ownership, GC roots, duplicate/dead definitions, method writes, ivar door, names/namespaces/lengths, regexp captures and bracket depth. Packed GC mark checks pass under O2 and ASan/UBSan; record_hygiene and staged/unstaged whitespace checks pass.
  • Unchanged ruby/test_transcode.rb, the 14 relevant replacement/fallback/error/argument/newline/XML tests: 14 tests, 172 assertions, 0 failures/errors/skips on reference, current main evaluator/VM and integrated candidate evaluator/VM.

bench/encoding_replacement.rb 20 50000 --seed=1 and 200 50000 --seed=1: every call replaces 50,000 unrepresentable characters. Each engine uses MERE_RUBY_GC_TIGHT=1, fixed seed 1, three alternating main/candidate pairs. Instructions are medians; RSS is the maximum of three, in MiB.

Workload Engine Instructions, main → candidate Change Max RSS, main → candidate
calls20 evaluator 5,482,236,747 → 3,314,309,581 -39.545% 172.562 → 105.844
calls20 VM 5,170,917,157 → 3,316,592,158 -35.861% 158.484 → 106.672
calls200 evaluator 42,872,182,135 → 24,116,325,962 -43.748% 162.516 → 117.312
calls200 VM 42,395,124,309 → 24,077,319,242 -43.207% 169.438 → 118.125

The tight-GC baseline plateaus over these call counts; this demonstrates removed transient allocations and lower peaks rather than claiming an unbounded 2.5 MB/call leak. No collector or arena-policy changes are included. The pre-#409 inherited gate failure is resolved on this integrated head. Local validation is complete; exact-head review and hosted CI remain, with no queue exception or merge-hold waiver claimed.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed head a132dcf against current main 5b815b6. No new actionable findings.

The early replacement branch avoids allocating fallback character Strings and correctly ignores Hash/Proc/object fallbacks in replacement mode, while the ordinary fallback path remains intact. The raising-fallback regression is a useful control.

Independent validation in isolated temporary checkouts:

  • Fresh ordinary O2 builds of head and current main, using local Mere v0.1.732 and clean Ruby 4.0.10 via tools/ref_ruby.sh.
  • New corpus1162 matches Ruby on both engines in default, hash-index-disabled and GC-stress=32 configurations (6/6); main reproduces the fallback bug. Existing corpus336/349/391/416 match on both revisions in all six configurations (48/48 comparisons).
  • A separate 169-row probe covers encode/encode!, three destinations, replacement/fallback/XML combinations and default_internal. Each configuration retains all 90 reference-matching main rows and increases that count to 109. The other 60 rows remain divergent, principally XML combinations already failing on main; this is not a full transcode-conformance pass.
  • GC-root, duplicate-definition, dead-definition, method-write, ivar-door and diff whitespace checks pass. The StrBuf gate reports the same read_path_whole/read_path_at failures on head and main; no suppression was added. No full corpus, upstream-file sweep or performance remeasurement is claimed here.

Current main CI run46170689621 has 25 jobs: 22 success, build-linux/corpus failure at that same StrBuf gate, and one skipped VM comparison. PR CI run46333313906 still has running builds and queued coverage, so no queue exception or hosted completion is claimed. This remains draft and is not merged; the recorded global merge hold is preserved.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Re-reviewed head 5397281 against current main 9d1c0f3 after its main integration. No new actionable findings. The PR-specific runtime delta, corpus1162 and benchmark are unchanged from the previous reviewed head.

The early replacement branch correctly avoids allocating fallback character Strings and ignores fallback callbacks in replacement mode, while preserving the ordinary fallback path. The updated validation record explicitly separates completed local checks from pending hosted CI.

Independent validation in isolated temporary checkouts:

  • Fresh ordinary O2 candidate build with local Mere v0.1.732 and mgz 661a01d8. Candidate SHA-256: 90555124cab56ea43a4faba827f7c9fc336ba9e12ffd1274e6032b7b911755f8. Reused the previously reviewed current-main O2 binary after verifying byte identity of all 2,897 tracked repository/dependency files and its recorded binary hash. Both tracked trees remain clean. Reference: clean Ruby 4.0.10 via tools/ref_ruby.sh.
  • Corpus1162 matches Ruby on evaluator and VM, each with default settings, hash index disabled, or GC stress=32 (6/6). Current main reproduces the fallback differences in all six configurations. Existing corpus336/349/391/416 match on both revisions in all six configurations (48/48 comparisons).
  • Re-ran the independent 169-row encode/encode! probe, covering three destinations, replacement/fallback/XML combinations and default_internal. Every configuration retains all 90 reference-matching main rows and raises that count to 109. The remaining 60 rows are inherited divergences, principally XML combinations; this is not a full transcode-conformance pass.
  • Generated-C/source StrBuf, GC roots, duplicate/dead definitions, method writes, ivar door, name/namespace/length, regex-capture and diff whitespace checks pass. The earlier inherited StrBuf failure is absent on the integrated tree. The first build attempt lacked the pinned mgz submodule; initializing that dependency completed setup before the successful build and tests.

I did not independently repeat the full corpus sweeps, original upstream-file comparisons, performance measurements or platform/resource acceptance reported by the author. Current main CI run46342657606 has 24 successful jobs and one skipped evaluator/VM comparison. PR CI run46357976775 remains incomplete: build-linux in_progress; build-macos in_progress. Downstream coverage is not established. No runner-queue exception or merge-hold clearance is claimed. This remains draft and is not merged.

@284km
284km marked this pull request as ready for review November 30, 2026 05:31

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed head 53972811 (main.mere in the transcoder's undefined-character arm, corpus 1162, bench).

With undef: :replace, replacement now wins whether the replacement is explicit or the default. This matches CRuby: ECONV_UNDEF_REPLACE makes the converter substitute before an undefined-conversion error can reach the fallback, so the fallback is never consulted in that mode. Before, only an explicit replace: won, and a default replacement went through the fallback path, which built a String per codepoint. That was the reported per-call growth. Without :replace, the fallback, xml and error arms are unchanged. Corpus 1162 matches on both engines, and the repeated-call bench drops 36–44% in instructions with a lower peak. The branch includes current main (#409's gate).

No findings from me.

@284km
284km merged commit ce0fd9c into main Nov 30, 2026
20 of 35 checks passed
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