Skip to content

Preserve saved Hash entry digests through derived operations - #410

Merged
284km merged 3 commits into
mainfrom
codex-misc-hash-transform-digests
Dec 1, 2026
Merged

284km merged 3 commits into
mainfrom
codex-misc-hash-transform-digests

Conversation

@284km

@284km 284km commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Derived Hash operations carry each entry's insertion-time native digest instead of reconstructing it from a key that may already have changed. Preserve metadata through select/filter/reject, their mutating variants, compact/compact!, transform_values/transform_values!, and merge/update. Native values_at uses the correct mutable bucket or ordinary immutable-key index while preserving default-method dispatch.

Snapshots are matched by original entry position, including repeated insertion of the same key object and value under different historical digests. Digests and flags are native data in temporary regions; no Ruby roots or collector/arena policies are added. A derived singleton keeps #383's lazy representation. Incoming merge keys use their current digest; the receiver's existing entries keep their saved digests.

The review Major is fixed: when the last mutable key is filtered out, a non-small immutable result now seeds its ordinary index instead of returning with the index dropped. Small immutable results still allocate no per-entry digest rows. Corpus 1163 adds select/compact lookup checks after removing the sole mutable entry.

Corpus 1163 covers mutated Array/String-subclass keys, historical duplicate entries, callbacks that mutate keys and collect garbage, compare_by_identity, freeze/promotion, incoming-key mutation after merge!, self-merge iteration guards and overridden []/default. Main fails the new semantics; both candidate engines match ruby, index on/off, including GC stress. Existing fixtures 288, 493, 515, 819 and 1139 and integration controls 477/1120 remain matching.

The old fixture assumed srand controlled CRuby's small-Hash digest hints. A reference-only reproduction found different outputs across 76 independent ruby 4.0.10 processes with seed 1. The revised mutation cases force ST-table storage before mutation, as existing corpus 1139 does; singleton metadata remains covered by restoring the key's original digest before lookup. This removes the known oracle nondeterminism. The review's initial failing pair was not retained, so this does not conclusively explain that particular observation. The revised fixture additionally passes 64/64 independent candidate executions (two engines × index on/off × 16 repetitions), with GC_TIGHT=1, GC_STRESS=1 and seed 1, against one retained clean reference oracle.

Unchanged CRuby ruby/test_hash.rb, 20 targeted selectors combined in one file invocation per side: 38 tests, 266 assertions, 0 failures/errors/skips on reference, main evaluator/VM and candidate evaluator/VM. This includes identity and no-rehash variants of filtering, compact, transform_values, merge, values_at and rehash.

Validation uses current main 9d1c0f3, clean ruby 4.0.10 through tools/ref_ruby.sh, Mere 9dec2ff6, mgz 661a01d8, ordinary O2. Candidate binary SHA-256: 2adb31b6a3187616dbd09498579a7561e9ee1652df5db8325bd8ad53cfe3d5fb. Runtime inputs are checked before/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: StrBuf regions, 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 whitespace checks pass. Main's former inherited StrBuf preflight failure is resolved by integrating current main.

Native costs use MERE_RUBY_GC_TIGHT=1 --seed=1, three alternating main/candidate pairs per engine. Instructions are medians; RSS is the maximum of three, MiB. Small workloads use 2,000 operations; large_filter selects 1,000 of 2,000 Array keys three times. #383 controls use 10,000 small creates/dups. Immutable lookup workloads create 4,000/16,000 Integer entries plus one mutable entry, remove the latter with select/compact, then perform 5,000 lookups. The following table includes all 180 samples.

Workload Engine Instructions, main → candidate Change Max RSS, main → candidate
symbol_filter evaluator 1,228,535,629 → 1,231,741,152 +0.261% 81.875 → 81.859
symbol_filter VM 1,261,026,652 → 1,262,323,369 +0.103% 82.141 → 82.094
mutable_filter evaluator 1,348,886,538 → 1,347,805,906 -0.080% 91.812 → 91.797
mutable_filter VM 1,390,729,675 → 1,391,162,739 +0.031% 92.078 → 92.062
mutable_keep evaluator 1,350,214,296 → 1,376,367,440 +1.937% 91.562 → 93.078
mutable_keep VM 1,401,406,718 → 1,426,021,587 +1.756% 91.688 → 92.672
mutable_tv evaluator 1,306,788,241 → 1,369,236,848 +4.779% 89.750 → 90.641
mutable_tv VM 1,374,341,860 → 1,436,650,704 +4.534% 90.016 → 90.656
symbol_merge evaluator 1,178,141,603 → 1,177,762,087 -0.032% 83.062 → 83.125
symbol_merge VM 1,262,368,608 → 1,261,717,323 -0.052% 83.094 → 83.203
mutable_merge evaluator 1,347,097,741 → 1,322,429,724 -1.831% 91.750 → 90.656
mutable_merge VM 1,448,863,919 → 1,420,875,933 -1.932% 92.031 → 90.750
mutable_update evaluator 1,327,693,058 → 1,377,466,320 +3.749% 93.766 → 94.625
mutable_update VM 1,440,504,969 → 1,486,189,971 +3.171% 93.766 → 94.484
large_filter evaluator 1,193,087,641 → 1,183,508,682 -0.803% 85.375 → 85.359
large_filter VM 1,206,101,232 → 1,197,364,650 -0.724% 85.828 → 85.719
immutable_select_4k evaluator 1,257,842,663 → 1,259,166,179 +0.105% 84.641 → 85.656
immutable_select_4k VM 1,283,008,478 → 1,284,243,366 +0.096% 85.500 → 86.906
immutable_compact_4k evaluator 1,174,371,571 → 1,179,409,732 +0.429% 84.547 → 84.656
immutable_compact_4k VM 1,195,329,384 → 1,196,978,743 +0.138% 85.047 → 84.953
immutable_select_16k evaluator 1,714,670,339 → 1,719,250,274 +0.267% 91.719 → 91.688
immutable_select_16k VM 1,778,010,255 → 1,786,237,073 +0.463% 94.844 → 95.312
immutable_compact_16k evaluator 1,383,778,170 → 1,391,991,152 +0.594% 91.688 → 91.766
immutable_compact_16k VM 1,438,534,460 → 1,445,294,106 +0.470% 93.672 → 94.766
small_create evaluator 1,357,726,679 → 1,360,267,446 +0.187% 98.156 → 98.156
small_create VM 1,390,022,377 → 1,395,323,988 +0.381% 99.578 → 99.547
small_dup evaluator 1,559,761,407 → 1,560,167,248 +0.026% 97.906 → 98.172
small_dup VM 1,585,885,736 → 1,586,236,711 +0.022% 99.016 → 99.016
small_symbol_dup evaluator 1,262,718,025 → 1,261,992,446 -0.057% 84.000 → 84.000
small_symbol_dup VM 1,293,530,066 → 1,294,565,042 +0.080% 83.469 → 83.453

The small create/dup controls retain #383's cheap path. Mutating transforms and merge! retain historical metadata across callbacks and their cost is shown above; no unchanged-cost or peak-RSS reduction claim is made for those paths.

A separate 90-sample review check compares three sealed binaries: current main, the pre-fix integrated candidate, and this fix, using the same new benchmark driver, GC_TIGHT=1, seed 1, three alternating rounds. Times below measure only the 5,000 lookup loop, in seconds; whole-process instruction and peak-RSS samples are retained with the other costs. Index-off is the linear-lookup control.

Workload Engine Index Lookup median, main / pre-fix / fixed
immutable_select_4k evaluator on 0.008648 / 0.221491 / 0.008188
immutable_select_4k VM on 0.008923 / 0.218249 / 0.008618
immutable_compact_4k evaluator on 0.008304 / 0.227672 / 0.008787
immutable_compact_4k VM on 0.009196 / 0.217165 / 0.008700
immutable_select_16k evaluator on 0.009495 / 0.790266 / 0.008642
immutable_select_16k VM on 0.009506 / 0.801522 / 0.008490
immutable_compact_16k evaluator on 0.008522 / 0.790434 / 0.008941
immutable_compact_16k VM on 0.009105 / 0.812297 / 0.008693
immutable_select_4k evaluator off 0.227550 / 0.215149 / 0.219019
immutable_select_4k VM off 0.223763 / 0.214740 / 0.217092

Local validation is complete. Exact-head review and hosted CI remain; no merge-hold waiver is 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 0c13228495a17b1b6de7953546084fcbac2d795c against current main 9d1c0f32cad65ca8df45da2466beb27dcd35e015 in isolated checkouts. The entry-position snapshots correctly preserve historical digests even when the same key/value occurs more than once. One P2 performance regression needs attention: removing the final mutable key also discards the ordinary index on the derived hash (inline).

Validation performed:

  • Fresh ordinary O2 builds using pinned Mere 9dec2ff687a08433247dbdb7729bc19c8522e538 and mgz 661a01d8bb68f5acd68e3192bd47f081d179c436; clean Ruby 4.0.10 selected through tools/ref_ruby.sh.
  • Seven existing corpus fixtures (288, 493, 515, 819, 1139, 477, 1120) and a 56-case Hash-operation matrix match the reference on both base/head, evaluator/VM and index on/off: 64 control comparisons. New corpus 1163 matches in all four normal candidate modes; its four main mismatches are the behavior this PR repairs.
  • One initial evaluator/index-off GC-stress comparison mismatched; 17 follow-up executions matched. This transient remains unexplained and is not recorded as an unconditional stress pass.
  • GC-root, duplicate-definition, dead-definition and diff whitespace checks pass. The head's StrBuf gate still fails at read_path_whole/read_path_at; current main includes #409 and passes that gate. Full corpus/upstream acceptance was not rerun or inferred from the focused checks.

Hosted core CI still has active work and unassigned queues; downstream coverage is incomplete. Main run 46342657606 also remains incomplete. No merge: this PR and all other open mere-ruby PRs are drafts, merelang/mere has no open PRs, and the recorded merge holds remain in force.

Comment thread m_state.mere Outdated
let rec any9 = fn (i9: int) ->
if i9 >= n9 then false else
(match vec_get from9 i9 with (_, flag9) -> flag9 || any9 (i9 + 1)) in
if n9 == 0 || not (any9 0) then ()

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.

[P2 / Major] Preserve the ordinary index when the last mutable key is removed

When the selected rows contain only immutable keys, every saved flag is false and this returns without creating either an index or a lazy-index marker. hset_saved_entries has already called hset, so the result permanently falls back to pair-list scans. Previously the non-mutating filter/compact paths used mkhash and indexed this result.

h = {[:drop] => nil}
4000.times { |i| h[i] = i }
r = h.select { |_, value| !value.nil? } # h.compact has the same issue
5000.times { r[3999] }

With fresh pinned O2 builds, the lookup-loop median of three alternating base/head pairs grows from 0.011 s to 0.276 s in the evaluator and 0.010 s to 0.292 s in the VM. At 16,000 keys, evaluator select/compact lookups grow from about 0.009 s to 1.11/1.23 s. Disabling the index makes base/head similarly slow, consistent with the lost index. Please seed the ordinary index (or mark it for lazy creation) for non-small immutable-only results before returning here; no mutable-entry rows are needed.

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.

Fixed in 7dc6393. When filtering removes the last mutable key, hentry_restore_saved now seeds the ordinary index for non-small immutable results; the small path still creates no digest rows.

The same 5,000-lookup benchmark now takes about 0.008–0.009 s for 4,000 and 16,000 Integer keys on both engines, matching current main. The pre-fix integrated candidate takes about 0.22 s / 0.79–0.81 s respectively. The index-off control remains linear. The body includes all workloads, three alternating fixed-seed GC_TIGHT pairs, instructions and max RSS, plus a separate 90-sample main/pre-fix/fixed comparison.

Current-main integration and local verification are complete: run_corpus.sh --both 1040/1040; MERE_RUBY_VM=1 run_corpus.sh --both 1040/1040; native VM corpus 1040, zero fallback/FAIL; all source gates, packed GC mark checks, record_hygiene and whitespace checks pass. Original ruby/test_hash.rb: 38 tests / 266 assertions on reference and both main/candidate engines.

I also removed reference nondeterminism in corpus1163 by forcing ST storage before mutated-key lookups, while retaining deterministic singleton metadata coverage. The revised case passes 64/64 GC-stress executions across both engines and index settings. The review's original failing pair was not retained, so I do not claim to have conclusively explained that specific observation. Exact-head re-review is requested.

@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.

Follow-up review of 7dc6393 against current main f0f372e. The previous P2 / Major finding is addressed: hentry_restore_saved now seeds the ordinary index when removing the final mutable entry leaves a non-small immutable result. The small immutable path remains compact. I found no new actionable defect in this follow-up; the position-based metadata snapshots remain appropriate for historical duplicate entries.

Independent validation on macOS arm64:

  • Fresh ordinary O2 build of current main using pinned Mere 9dec2ff687a08433247dbdb7729bc19c8522e538 and the pinned mgz inputs. Head tests reuse the existing O2 binary (SHA-256 2adb31b6a3187616dbd09498579a7561e9ee1652df5db8325bd8ad53cfe3d5fb); all 1,829 recorded input hashes and native-source inventory coverage were verified against the fetched head, and input/binary hashes were rechecked after validation.
  • Seven existing corpus fixtures (288, 493, 515, 819, 1139, 477, 1120) plus the 56-case operation matrix match clean Ruby 4.0.10 on both base/head, evaluator/VM and index on/off: 64/64 control comparisons. Updated corpus 1163 matches in all four head modes; current main still differs in the semantics repaired here.
  • Corpus 1163 matches the retained reference in 8/8 additional head executions with GC_TIGHT=1, GC_STRESS=1 and seed 1 (two repetitions per engine/index combination). Sixteen additional independent reference processes produce identical output. These observations support the revised fixture; they cannot conclusively explain the original review's unavailable failing pair.
  • Three alternating base/head pairs per engine, operation (select/compact), and size (4,000/16,000 keys): all 48 lookup samples return the expected checksum. The 5,000-lookup medians are approximately 0.008–0.010 seconds on both revisions at both sizes, removing the reported scaling regression. Four index-off controls remain approximately 0.21–0.24 seconds at 4,000 keys. These are lookup timing checks, not peak-RSS acceptance.
  • StrBuf-region, GC-root (freshly emitted head C), duplicate/dead-definition and diff whitespace checks pass.

An initial standalone copy lacked adjacent C API headers, which suppresses RbConfig's extension-build configuration and made SOEXT comparisons fail. Repeating with the binary inside the isolated checkout restores the correct configuration and all focused head comparisons pass; this was a validation-layout issue, not a PR finding.

I did not independently rerun the full corpus, whole upstream test files, Linux checks or resource acceptance. Hosted CI remains unfinished with executing work and queues. This PR remains a draft; the recorded global merge holds also remain in force. No merge attempted, and the existing review thread is left for its owner to manage.

@284km
284km marked this pull request as ready for review November 30, 2026 07:39
@284km
284km merged commit f93fa44 into main Dec 1, 2026
39 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