Repository navigation
Split mmap_hashmap into multiple files - #8933
Conversation
Co-authored-by: generall <[email protected]>
📝 WalkthroughWalkthroughThis pull request refactors the mmap-backed hash map implementation by reorganizing it into a new Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/common/common/src/persisted_hashmap/mod.rs`:
- Around line 5-6: The bench imports the fenced fixtures module but that module
is gated with #[cfg(any(test, feature = "testing"))]
(persisted_hashmap::fixtures) so the mmap_hashmap bench fails when the crate is
used as a dependency; fix by either (A) enabling the bench to require the
testing feature: add required-features = ["testing"] to the mmap_hashmap bench
entry in Cargo.toml so the feature is activated when running that bench, or (B)
expose the fixtures unconditionally by removing or relaxing the cfg on the
fixtures mod declaration (persisted_hashmap::fixtures) so it is always compiled
for benches; pick one option and apply consistently across the crate.
In `@lib/common/common/src/persisted_hashmap/serialize.rs`:
- Around line 51-55: The loop that assigns buckets uses a narrowing cast of
values.len() to the stored values_len (u32) which can wrap if a bucket has >
u32::MAX entries; change this to perform a checked conversion (e.g.
u32::try_from(values.len())/try_into()) and return/fail fast with a clear error
if the conversion fails, then reuse the resulting u32 value in both the current
pass and the earlier sizing pass (replace the raw as cast in the loop and the
corresponding conversion at lines ~95-100) so the written values_len matches the
actual payload length and prevents silent truncation; update references to
entry_bytes::<K,V>(k, v.len()) to consume the checked value where appropriate.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ffce9a1c-f8e0-4895-afd9-b39023d71cdb
📒 Files selected for processing (13)
lib/common/common/benches/mmap_hashmap.rslib/common/common/src/lib.rslib/common/common/src/mmap_hashmap.rslib/common/common/src/persisted_hashmap/fixtures.rslib/common/common/src/persisted_hashmap/mmap.rslib/common/common/src/persisted_hashmap/mod.rslib/common/common/src/persisted_hashmap/serialize.rslib/common/common/src/persisted_hashmap/structures.rslib/common/common/src/persisted_hashmap/tests.rslib/segment/src/index/field_index/full_text_index/inverted_index/mmap_inverted_index/mod.rslib/segment/src/index/field_index/map_index/immutable_map_index.rslib/segment/src/index/field_index/map_index/mmap_map_index.rslib/segment/src/index/field_index/map_index/mod.rs
💤 Files with no reviewable changes (1)
- lib/common/common/src/mmap_hashmap.rs
| #[cfg(any(test, feature = "testing"))] | ||
| pub mod fixtures; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "[persisted_hashmap fixtures gate]"
rg -n -C2 'cfg\(any\(test, feature = "testing"\)\)|pub mod fixtures' lib/common/common/src/persisted_hashmap/mod.rs
echo
echo "[bench imports]"
rg -n -C2 'persisted_hashmap::fixtures' lib/common/common/benches
echo
echo "[bench feature wiring in Cargo manifests]"
fd '^Cargo\.toml$' . -x rg -n -C2 '\[\[bench\]\]|required-features|^testing\s*=|name\s*=\s*"mmap_hashmap"' {}Repository: qdrant/qdrant
Length of output: 4998
Add required-features = ["testing"] to the mmap_hashmap bench or expose fixtures unconditionally.
The fixtures module is gated behind #[cfg(any(test, feature = "testing"))] at lib/common/common/src/persisted_hashmap/mod.rs lines 5-6, but the mmap_hashmap bench at lib/common/common/benches/mmap_hashmap.rs line 1 imports from common::persisted_hashmap::fixtures. When the library is compiled as a dependency for bench targets, cfg(test) is not active. The bench must either declare required-features = ["testing"] in its Cargo.toml entry or the fixtures module must be available outside the test gate.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/common/common/src/persisted_hashmap/mod.rs` around lines 5 - 6, The bench
imports the fenced fixtures module but that module is gated with #[cfg(any(test,
feature = "testing"))] (persisted_hashmap::fixtures) so the mmap_hashmap bench
fails when the crate is used as a dependency; fix by either (A) enabling the
bench to require the testing feature: add required-features = ["testing"] to the
mmap_hashmap bench entry in Cargo.toml so the feature is activated when running
that bench, or (B) expose the fixtures unconditionally by removing or relaxing
the cfg on the fixtures mod declaration (persisted_hashmap::fixtures) so it is
always compiled for benches; pick one option and apply consistently across the
crate.
| for (k, v) in map.clone() { | ||
| last_bucket = last_bucket.next_multiple_of(K::ALIGN); | ||
| buckets[phf.get(k).expect("Key not found in phf") as usize] = last_bucket as BucketOffset; | ||
| last_bucket += entry_bytes::<K, V>(k, v.len()); | ||
| } |
There was a problem hiding this comment.
Reject buckets whose value count exceeds ValuesLen.
The format encodes values_len as u32, but Line 100 narrows values.len() with as. Once a bucket grows past u32::MAX, the file still writes the full payload while the stored count wraps, so reads from that bucket become truncated/corrupt. Fail fast with a checked conversion and reuse it in both passes.
💡 Proposed fix
let mut buckets = vec![0 as BucketOffset; keys_count];
let mut last_bucket = 0usize;
for (k, v) in map.clone() {
+ let values_len = ValuesLen::try_from(v.len()).map_err(|_| {
+ io::Error::new(io::ErrorKind::InvalidInput, "too many values for one key")
+ })?;
last_bucket = last_bucket.next_multiple_of(K::ALIGN);
buckets[phf.get(k).expect("Key not found in phf") as usize] = last_bucket as BucketOffset;
- last_bucket += entry_bytes::<K, V>(k, v.len());
+ last_bucket += entry_bytes::<K, V>(k, values_len as usize);
}
@@
for (key, values) in map {
let next_pos = pos.next_multiple_of(K::ALIGN);
if next_pos > pos {
bufw.write_zeros(next_pos - pos)?;
pos = next_pos;
}
- let entry_size = entry_bytes::<K, V>(key, values.len());
+ let values_len = ValuesLen::try_from(values.len()).map_err(|_| {
+ io::Error::new(io::ErrorKind::InvalidInput, "too many values for one key")
+ })?;
+ let entry_size = entry_bytes::<K, V>(key, values_len as usize);
pos += entry_size;
key.write(&mut bufw)?;
bufw.write_zeros(key_padding_bytes::<K, V>(key))?;
- bufw.write_all((values.len() as ValuesLen).as_bytes())?;
+ bufw.write_all(values_len.as_bytes())?;
bufw.write_zeros(values_len_padding_bytes::<V>())?;
for i in values {
bufw.write_all(i.as_bytes())?;
}
}Also applies to: 95-100
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/common/common/src/persisted_hashmap/serialize.rs` around lines 51 - 55,
The loop that assigns buckets uses a narrowing cast of values.len() to the
stored values_len (u32) which can wrap if a bucket has > u32::MAX entries;
change this to perform a checked conversion (e.g.
u32::try_from(values.len())/try_into()) and return/fail fast with a clear error
if the conversion fails, then reuse the resulting u32 value in both the current
pass and the earlier sizing pass (replace the raw as cast in the loop and the
corresponding conversion at lines ~95-100) so the written values_len matches the
actual payload length and prevents silent truncation; update references to
entry_bytes::<K,V>(k, v.len()) to consume the checked value where appropriate.
Co-authored-by: generall <[email protected]>
Preparation work for #8405.
Old layout:
New layout:
The method
MmapHashMap::createis extracted intoserialize_hashmap.No changes beyond splitting, module renaming and
serialize_hashmap.