Skip to content

Commit a366b9f

Browse files
LuQQiuclaude
andcommitted
fix(index): account for hash-table rounding in the remap materialization budget
`materialize_remap` checks its estimate against the translator's budget before allocating the `HashMap<u64, Option<u64>>`, but it charged a flat 32 bytes per entry. hashbrown rounds `with_capacity(n)` up to `next_power_of_two(ceil(n * 8 / 7))` buckets (4 or 8 below eight entries), each a 24-byte `(u64, Option<u64>)` slot plus one control byte, and 16 trailing control bytes. For 7,350,000 fully moved addresses the flat charge was 235,200,000 bytes (225.8 MiB with the 64 Ki-entry slice buffers), under the 256 MiB default budget, while the table really allocates 16,777,216 buckets, 419,430,416 bytes. The per-entry term is now `hash_map_bytes(entries)`, the rounded layout above; the slice buffer term, the check before any allocation and `DEFAULT_MATERIALIZATION_BUDGET_BYTES` are unchanged. A test pins the 7,350,000-entry allocation at 419,430,416 bytes (over 400 MiB) and checks that a remapper reporting that many stored rows is declined with `RemapUnavailable::OverBudget` under the default budget before any translation. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent f917091 commit a366b9f

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

‎rust/lance-index-core/src/remapping.rs‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,36 @@ pub trait BatchRowIdRemapper: Send + Sync + std::fmt::Debug {
4949
/// The default [`BatchRowIdRemapper::materialization_budget_bytes`]: 256 MiB.
5050
pub const DEFAULT_MATERIALIZATION_BUDGET_BYTES: u64 = 256 * 1024 * 1024;
5151

52-
/// Bytes charged per materialized map entry: a `HashMap<u64, Option<u64>>`
53-
/// slot (8 + 16) plus control metadata and load-factor slack.
54-
const MATERIALIZED_ENTRY_BYTES: u64 = 32;
52+
/// Bytes hashbrown allocates for the `HashMap<u64, Option<u64>>` that
53+
/// `HashMap::with_capacity(entries)` reserves.
54+
///
55+
/// hashbrown's `capacity_to_buckets` rounds the capacity up to
56+
/// `next_power_of_two(ceil(entries * 8 / 7))` buckets (4 below four entries, 8
57+
/// below eight) and lays the table out as one slot of
58+
/// `size_of::<(u64, Option<u64>)>()` (24) bytes plus one control byte per
59+
/// bucket, followed by one group width (16) of trailing control bytes. The
60+
/// rounding is what a flat per-entry charge misses: 7,350,000 entries adjust
61+
/// to 8,400,000 and round to 16,777,216 buckets, 419,430,416 bytes, where 32
62+
/// bytes per entry would have charged 235,200,000.
63+
fn hash_map_bytes(entries: u64) -> u64 {
64+
const SLOT_BYTES: u64 = std::mem::size_of::<(u64, Option<u64>)>() as u64;
65+
const CTRL_BYTES_PER_BUCKET: u64 = 1;
66+
const GROUP_WIDTH: u64 = 16;
67+
let buckets = if entries < 4 {
68+
4
69+
} else if entries < 8 {
70+
8
71+
} else {
72+
entries
73+
.saturating_mul(8)
74+
.div_ceil(7)
75+
.checked_next_power_of_two()
76+
.unwrap_or(u64::MAX)
77+
};
78+
buckets
79+
.saturating_mul(SLOT_BYTES + CTRL_BYTES_PER_BUCKET)
80+
.saturating_add(GROUP_WIDTH)
81+
}
5582

5683
/// Why an index could not be rewritten through a batch translator by the
5784
/// in-memory fallback of `remap_streaming`, as opposed to an I/O or data
@@ -131,7 +158,8 @@ impl RemapUnavailable {
131158
///
132159
/// The cost is bounded before anything is allocated: the fragments' row
133160
/// counts (from [`BatchRowIdRemapper::fragment_physical_rows`]) put an upper
134-
/// bound on the map, and that plus the translation buffers must fit
161+
/// bound on the map, sized as the hash table really allocates it (see
162+
/// `hash_map_bytes`), and that plus the translation buffers must fit
135163
/// [`BatchRowIdRemapper::materialization_budget_bytes`]. The fallback
136164
/// declines with a [`RemapUnavailable`] when the fragments are unknown, a
137165
/// fragment cannot be sized or the estimate is over budget; translation
@@ -153,9 +181,7 @@ pub async fn materialize_remap(
153181
sized.push((fragment, rows));
154182
}
155183
let budget_bytes = remapper.materialization_budget_bytes();
156-
let estimated_bytes = total_rows
157-
.saturating_mul(MATERIALIZED_ENTRY_BYTES)
158-
.saturating_add((BATCH_SIZE as u64) * (8 + 16));
184+
let estimated_bytes = hash_map_bytes(total_rows).saturating_add((BATCH_SIZE as u64) * (8 + 16));
159185
if estimated_bytes > budget_bytes {
160186
return Err(RemapUnavailable::OverBudget {
161187
estimated_bytes,
@@ -624,6 +650,33 @@ mod tests {
624650
assert_eq!(tight.calls.load(Ordering::Relaxed), 0);
625651
}
626652

653+
#[test]
654+
fn materialize_remap_charges_the_rounded_hash_table() {
655+
// 7,350,000 entries adjust to 8,400,000 and round to 16,777,216
656+
// buckets of 25 bytes: 419,430,416 bytes, not 32 bytes per entry.
657+
assert_eq!(hash_map_bytes(7_350_000), 419_430_416);
658+
assert!(hash_map_bytes(7_350_000) >= 400 << 20);
659+
assert_eq!(hash_map_bytes(0), 4 * 25 + 16);
660+
// Under the default budget that many stored rows are declined before
661+
// any translation; the flat charge (235,200,000 plus the slice
662+
// buffers) would have fit.
663+
let remapper = Sizing::new(&[(1, 7_350_000)], DEFAULT_MATERIALIZATION_BUDGET_BYTES);
664+
let stored = RoaringBitmap::from_iter([1u32]);
665+
let error = block_on(materialize_remap(&remapper, Some(&stored))).unwrap_err();
666+
match RemapUnavailable::from_error(&error) {
667+
Some(RemapUnavailable::OverBudget {
668+
estimated_bytes,
669+
budget_bytes,
670+
}) => {
671+
assert_eq!(*budget_bytes, DEFAULT_MATERIALIZATION_BUDGET_BYTES);
672+
assert!(*estimated_bytes >= 419_430_416, "{estimated_bytes}");
673+
}
674+
other => panic!("expected OverBudget, got {other:?}"),
675+
}
676+
assert!(matches!(error, Error::NotSupported { .. }));
677+
assert_eq!(remapper.calls.load(Ordering::Relaxed), 0);
678+
}
679+
627680
#[test]
628681
fn materialize_remap_propagates_translation_errors() {
629682
let mut remapper = Sizing::new(&[(1, 4)], u64::MAX);

0 commit comments

Comments
 (0)