Repository navigation
Preserve saved Hash entry digests through derived operations - #410
Conversation
284km
left a comment
There was a problem hiding this comment.
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
9dec2ff687a08433247dbdb7729bc19c8522e538and mgz661a01d8bb68f5acd68e3192bd47f081d179c436; clean Ruby 4.0.10 selected throughtools/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.
| 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 () |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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.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.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.
Local validation is complete. Exact-head review and hosted CI remain; no merge-hold waiver is claimed.