8000 perf: sort set element encodings with sort_unstable (#794) by adityamukho · Pull Request #800 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content

perf: sort set element encodings with sort_unstable (#794) - #800

Merged
adityamukho merged 2 commits into
v3from
perf/794-set-sort-unstable
Oct 30, 2026
Merged

adityamukho merged 2 commits into
v3from
perf/794-set-sort-unstable

Conversation

@adityamukho
Copy link
Copy Markdown
Collaborator

Implements #794 (follow-up to the #772 audit, #798). This is the audit's encoded-set-sort.patch (docs/audits/772/evidence/patches/) plus a reference test.

Change

keys::set_elements sorts element encodings with sort_unstable instead of sort. Equal encodings are identical byte strings and are removed by the dedup that follows, so stability cannot be observed. Encoded bytes, deduplication, signed-zero/NaN handling and errors are unchanged. No API, file-format, error-code, MSRV, size-cap or coverage-floor change.

Equivalence test

set_element_encoding_is_the_sorted_distinct_encodings compares the encoding with a BTreeSet reference over 2,000 random sets. The sets have up to about 60 elements and mix every scalar kind, with repeated elements, ±0.0, NaNs and strings up to 300 bytes (long sets). Each set is tested in shuffled, encoding-sorted and reversed order. The test passes with both the old stable sort and the new one.

Sizes (bench machine, Rust 1.99.1)

Artifact v3 (8c6c4a1) This PR Saved
Native CLI (release) 1,263,592 1,260,568 3,024
Thin-LTO dist CLI 1,502,888 1,498,776 4,112
WASI 879,785 878,835 950
Browser Wasm (raw, before wasm-bindgen) 1,525,738 1,524,903 835

End-to-end set workload (bench machine, release profile, taskset -c 2)

100,000 entities, one 32-element set each, ingested in 500-fact transactions, then a checkpoint, 200 set-constant point queries and a full scan with count. Seven input shapes: ints/strings/keywords in ascending and descending order, plus mixed. Base and head runs alternate, with medians over 5 rounds. Phase changes ranged from −6.3% to +2.3%, except the two outliers below, which were rerun for 10 more rounds:

Shape / phase Base median Head median Change (10-round rerun)
str-desc checkpoint 1,610.6 ms 1,606.0 ms −0.3% (first pass showed +6.0%)
mixed, 200 point queries 178.2 ms (range 173–202) 183.5 ms (range 172–224) +2.9%

The mixed point-query path sorts only 200 × 32 encodings in total, so this sort cannot explain a ~5 ms gap; I read it as code layout or noise, and the ranges overlap. Every other phase was within ±4%, most slightly faster. Machine noise produced a few multi-second outliers in individual runs; medians are reported.

Verification

  • cargo test: 2,038 passed, 19 ignored (one new test)
  • CI-equivalent clippy on Rust 1.99.1: native --all-features, wasm32-unknown-unknown --features browser, wasm32-wasip1
  • cargo +1.89 check --all-features (MSRV)
  • cargo fmt --check

Docs: CHANGELOG (Performance), docs/TEST_COVERAGE.md counts and a #794 verification note, CLAUDE.md test count.

🤖 Generated with Claude Code

https://claude.ai/code/session_01H2a7QjqyDrSDo1LKTK619q

Equal element encodings are identical bytes and are deduplicated right
after the sort, so stability is unobservable: the encoded bytes are
unchanged. Drops the stable sort's scratch allocation and its code
(native release binary 1,263,592 -> 1,260,568 bytes).

Adds a reference test: the encoding equals the BTreeSet-ordered distinct
encodings over random sets with duplicates, signed zeros, NaNs and long
strings, in shuffled, sorted and reversed order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2a7QjqyDrSDo1LKTK619q
@codecov
codecov Bot commented Oct 30, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Keep both #794 and #795 CHANGELOG entries; combine test counts
(2,058 total, 2,039 active).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H2a7QjqyDrSDo1LKTK619q
@adityamukho
adityamukho merged commit 15badca into v3 Oct 30, 2026
24 checks passed
@adityamukho
adityamukho deleted the perf/794-set-sort-unstable branch October 30, 2026 06:19
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

0