Skip to content

perf(encoding): accelerate dictionary interning, sampling, and decode gather - #10911

Merged
Xuanwo merged 1 commit into
mainfrom
xuanwo/perf-dict-encode-decode
Nov 12, 2026
Merged

Xuanwo merged 1 commit into
mainfrom
xuanwo/perf-dict-encode-decode

Conversation

@Xuanwo

@Xuanwo Xuanwo commented Nov 10, 2026

Copy link
Copy Markdown
Member

Summary

Optimize dictionary encoding, cardinality sampling, and dictionary-to-Arrow materialization in lance-encoding while preserving 100% wire-format and behavioral compatibility:

  • Monomorphized two-pass DictionaryDataBlock gather & single-pass dictionary validation (data.rs):
    • Replace per-row DataBlockBuilder virtual dispatch (N dyn append_validated calls, repeated offsets reallocations, and per-row Arc clones on nullable validity) with direct typed gather paths for 32/64-bit VariableWidthBlock (exact byte-size precomputation in u128 + single-pass copy_nonoverlapping and offset rebasing), FixedWidthDataBlock (8/16/32/64/128-bit and arbitrary byte-aligned widths), and NullableDataBlock (BooleanBuffer::collect_bool), falling back cleanly when unreferenced dictionary entries contain malformed offsets.
    • Validate the compact dictionary once in DictionaryDataBlock::into_arrow (dict.validate_layout(&data_type)) and construct the expanded VariableWidthBlock via into_arrow_unchecked, avoiding redundant UTF-8 and offset re-validation across all N expanded rows while preserving fallback semantics for partially referenced dictionaries.
  • Cache-resident CompactInterner for dictionary encoding (encodings/logical/primitive/dict.rs):
    • Replace zero-capacity std::collections::HashMap<U8SliceKey, i32> (SipHash-1-3 + 24-byte fat-pointer slots into cold input pages + per-row budget checks) across dict_encode_variable_width, dictionary_encode (64/128-bit), and encode_fixed_rows with a compact open-addressing u32 slot table (4 bytes/slot) backed by xxh3_64 / mix_u64, canonical dictionary_buffer comparisons, a 1-entry MRU consecutive-run fast path, and vacant-insertion-only byte budget tracking.
  • Zero-allocation sample_unique_ratio (encodings/logical/primitive/structural_encoder.rs):
    • Eliminate per-page Vec<usize> sample-index allocation, pre-size HashSet to total_samples, and replace SipHash-1-3 (including double-hashing of xxh3_64 outputs on variable-width pages) with FastSampleHasher / IdentityU64Hasher.

Benchmark Results

End-to-end Lance v2.2 FileWriter + FileReader over in-memory ObjectStore, 7 alternating AB/BA paired rounds (9 timed iterations after 3 warmups per round) pinned to a single physical core on AMD EPYC 4585PX:

Workload Baseline Median (ms) Candidate Median (ms) Baseline (GiB/s) Candidate (GiB/s) Median Paired Speedup Min..Max Speedup
control_fullzip_bin512_scan_4mib (control) 0.060 0.060 65.31 65.41 1.00x 0.99x..1.02x
dict_i64_128_card_scan_524k 1.701 0.306 2.30 12.75 5.57x 5.29x..5.70x
dict_i64_128_card_write_524k 4.069 1.036 0.96 3.77 3.91x 3.88x..4.01x
dict_utf8_low_card_scan_524k 3.318 1.129 5.59 16.44 2.93x 2.87x..2.96x
dict_utf8_nullable_1k_card_scan_262k 1.998 0.791 7.26 18.33 2.51x 1.59x..2.61x
dict_utf8_low_card_write_524k 7.579 3.760 2.45 4.93 2.00x 1.04x..3.54x
dict_utf8_nullable_1k_card_write_262k 6.174 3.531 2.35 4.11 1.74x 1.73x..1.77x

Test Plan

  • Added unit tests covering partial-reference validation fallback on malformed dictionary entries, exact small-string capacity reservation, non-power-of-two fixed-width gather, and CompactInterner probing/rehash equivalence (cargo test -p lance-encoding --lib)
  • Verified file format roundtrips (cargo test -p lance-file --lib)
  • Verified cargo fmt --all and full workspace cargo clippy --all --tests --benches -- -D warnings

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@github-actions github-actions Bot added A-encoding Encoding, IO, file reader/writer performance labels Nov 10, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Gate recommendation: approve.

The dictionary changes preserve the existing data and validation contracts in local tests. The encoding suite and dictionary file roundtrips passed.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 10, 2026
@Xuanwo
Xuanwo merged commit c82c152 into main Nov 12, 2026
41 checks passed
@Xuanwo
Xuanwo deleted the xuanwo/perf-dict-encode-decode branch November 12, 2026 05:03
Xuanwo added a commit that referenced this pull request Nov 12, 2026
Main's `ci` profile lint fails with `clippy::undocumented_unsafe_blocks`
at
`DictionaryDataBlock::gather_variable_width_typed` (`data.rs:1200`),
which blocks the full Rust
lint for every PR in the workspace, #10750 included.

The existing `SAFETY:` note sits inside the outer unsafe block, so it
justifies the unchecked
offset reads but leaves the block itself undocumented. This moves the
justification above the
block and extends it to cover the rest of it: the index/offset bounds
proven by
`validate_offsets_for_append` and the first pass, `total_bytes` fitting
both `usize` and `T`, the
two output allocations being disjoint from the dictionary data, and
every rebased offset and
payload byte being written before the `set_len` calls. Comment-only; no
runtime change.

Context: the block came in with #10911. Validation on this branch:
`cargo fmt --all -- --check`
and `cargo clippy --profile ci --all --tests --benches -- -D warnings`
both pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-encoding Encoding, IO, file reader/writer K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants