Skip to content

feat(encoding): cap the dictionary size a page may be encoded with - #9939

Merged
wjones127 merged 4 commits into
lance-format:mainfrom
dshepelev15:perf/encoding-dict-size-cap
Oct 20, 2026
Merged

wjones127 merged 4 commits into
lance-format:mainfrom
dshepelev15:perf/encoding-dict-size-cap

Conversation

@dshepelev15

@dshepelev15 dshepelev15 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

should_dictionary_encode gates dictionary encoding on cardinality (num_values / dict-divisor, capped at 100k) and on the encoded size relative to the raw page (dict-size-ratio). Nothing bounds the dictionary itself, so a page of large values with enough duplicates gets a dictionary of tens or hundreds of megabytes.

The reader loads a page's dictionary whole on first touch (MiniBlockScheduler::init_layout adds the dictionary buffer to the page initialization), so every point read into such a page reads the whole dictionary. Observed on a 2.1 table of JPEG thumbnails (large_binary, ~50 KB per value, 4096 rows per page): in 64 sampled fragments 48 of 4,479 pages (about 1% of pages and of rows) were dictionary pages with 137–2019 distinct values and 6–131 MB dictionaries (mean 54 MB). A single-row take that landed in one of them read 95 MB over 6 requests; a batch of 128 rows per fragment read 157 MiB for 27 MiB of values. The decoded dictionary is then cached in the metadata cache and evicts other files' metadata.

Change

  • DictEncodingBudget carries a max_dict_bytes budget from field metadata lance-encoding:dict-max-size, 8 MiB by default.
  • dict::dictionary_encode enforces it exactly while building a variable-width dictionary; a page over the budget falls back to the regular structural encoding, as it already does for the entry and encoded-size budgets. Fixed-width dictionaries (at most 100k entries of 8 or 16 bytes) cannot reach the default and are left alone.

8 MiB keeps the dictionaries that help scans (categorical strings, ids: at most 100k entries) untouched and bounds the first-touch read of a dictionary page to a few milliseconds in-region. The docs row for the new key in docs/src/format/file/encoding.md is left for a follow-up since that directory is gated by the format vote.

Tests

  • dict::tests::test_dictionary_encode_respects_dict_byte_limit: a 25,600-byte dictionary is refused under a 16 KiB budget and built under a 25,600-byte one.
  • test_encode_skips_dictionary_above_max_dict_size: the same array is dictionary encoded under the default budget and written without a dictionary when the field sets lance-encoding:dict-max-size below the dictionary size.
  • cargo fmt --all, cargo clippy --all --tests --benches -- -D warnings clean.

@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 pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added A-encoding Encoding, IO, file reader/writer enhancement New feature or request labels Oct 13, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 13, 2026
@dshepelev15
dshepelev15 force-pushed the perf/encoding-dict-size-cap branch from 3d2bbd0 to 5979028 Compare October 13, 2026 11:35
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 13, 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 metadata-only cap still bounds eager loading of large variable-width dictionaries and preserves the existing structural fallback and file format. Fixed-width dictionary behavior is preserved. The focused checks found no blocker.

Please mark this PR with the breaking-change label.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 13, 2026
@dshepelev15

Copy link
Copy Markdown
Contributor Author

The default cap changes which pages get a dictionary (dictionaries above 8 MiB of values are no longer written), so this should carry the breaking-change label; I cannot add labels from the fork, could a maintainer apply it?

@wjones127
wjones127 self-requested a review October 14, 2026 03:05

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

This makes sense to me. Might give @westonpace a chance to comment if he wants, as he's more familiar with the encoding logic. The 200MB page you describe is interesting; I do wonder if there's other things that will be sub-optimal for the encoder one those.

.metadata
.get(DICT_MAX_SIZE_META_KEY)
.and_then(|val| val.parse().ok())
.unwrap_or(DEFAULT_DICT_MAX_SIZE_BYTES);

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.

question (non-blocking): Why silently fall back to the 8 MiB default when dict-max-size fails to parse? If this matches how dict-divisor and dict-size-ratio are parsed that's fine. Otherwise a typo like 8MiB quietly keeps the default, so a log::warn! may be worth it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It matches dict-divisor and dict-size-ratio: both parse with .parse().ok() and fall back silently, so I kept the three options consistent. Also rebased onto main, where the encoder moved to primitive/structural_encoder.rs.

return None;
}
dictionary_buffer.extend_from_slice(key);
if dictionary_buffer.len() > max_dict_bytes {

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.

praise: Checking the cap right after the append keeps it exact and aborts early, so we never build a huge dictionary just to discard it. Good that it's in the generic fn and covers both the u32 and u64 offset paths.

The dictionary heuristic only looked at cardinality and at the encoded
size relative to the raw page, so a page of large values (images,
documents) with enough duplicates was dictionary encoded with a
dictionary of tens or hundreds of megabytes. The reader loads a page's
dictionary whole the first time the page is touched, which turns every
point read into a read of the whole dictionary: on a table of JPEG
thumbnails about 1% of pages were such pages, with 6-131 MB
dictionaries, and a single-row take hit 95 MB over 6 requests.

Add a byte budget for variable-width dictionary values,
`lance-encoding:dict-max-size` (field metadata), 8 MiB by default,
enforced exactly while the dictionary is built so that pages over the
budget fall back to the regular structural encoding.
@dshepelev15
dshepelev15 force-pushed the perf/encoding-dict-size-cap branch from 5979028 to 9f60001 Compare October 19, 2026 21:09
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 19, 2026
…ze-cap

# Conflicts:
#	rust/lance-encoding/src/encodings/logical/primitive/structural_encoder.rs
…efore appending

A present but unparsable lance-encoding:dict-max-size fell back to the
default silently; it now panics like dict-size-ratio does. The byte cap is
compared before the value is appended, so an aborted attempt never grows the
discarded buffer past the cap.
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 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants