Skip to content

Conversation

xzfc
Copy link
Contributor

@xzfc xzfc commented Jul 22, 2025

It turns out MADV_POPULATE_READ performance is influenced by the MADV_RANDOM and MADV_SEQUENTIAL flags.
Obvious in hindsight, but still surprising that the kernel won't enable readahead unconditionlly given that it knows that it will be reading the whole file.

// Slow, many 4K IOPS
madvise(ptr, size, MADV_RANDOM);
madvise(ptr, size, MADV_POPULATE_READ);
// Fast, as uses large readahead
madvise(ptr, size, MADV_SEQUENTIAL);
madvise(ptr, size, MADV_POPULATE_READ);

Profiler

It found using this profiler: https://github.com/xzfc/qdrant-tools/tree/3934acc179c9187a6b90d470de9a48fd0a9dd3c9/bpf-mfault-paths

Fix

Since a lot of our files mmaped twice (for sequential and for random), the fix in this PR is trivial: let populate() use the sequential mmap if it available.

Fixed:

  • Dense vector storages, except deleted flags
  • Gridstore pages (used in sparse vectors)

Some mmaps are not duplicated, so these are not fixed:

  • MmapBitSlice (deleted flags).
  • Gridstore bitmasks (bitmask.dat and gaps.dat; used in sparse vectors)
  • Quantized vector storage. (not used)
  • Payload storages. (not used)

CPU usage/saturation on 400M benchmark (left is before this commit, right is after):
2025-07-18_03:16:20

@xzfc xzfc requested review from timvisee and generall July 22, 2025 09:54
Copy link
Contributor

coderabbitai bot commented Jul 22, 2025

📝 Walkthrough

Walkthrough

This change modifies the internal implementation of the populate method across several memory-mapped data structures. Specifically, the method is updated to call populate() on read-only or sequentially advised mmap fields (such as mmap_seq, mmap_sequential, or mmap_seq), instead of the previous writable or default mmap fields. Additionally, new populate methods are introduced in the MmapTypeReadOnly<[T]> and MmapSliceReadOnly<T> structs to support this behavior. No public API signatures are changed; all modifications are internal to method implementations.

Estimated code review effort

2 (~15 minutes)

Possibly related PRs

  • disk cache hygiene #6323: Introduces populate and clear_cache methods across mmap-based components and removes delayed mmap population, directly relating to the changes in this PR regarding memory population strategies.

Suggested reviewers

  • timvisee

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac852aa and 67262c4.

📒 Files selected for processing (4)
  • lib/common/memory/src/chunked_utils.rs (1 hunks)
  • lib/common/memory/src/mmap_type_readonly.rs (3 hunks)
  • lib/gridstore/src/page.rs (1 hunks)
  • lib/segment/src/vector_storage/dense/mmap_dense_vectors.rs (1 hunks)
🧠 Learnings (5)
📓 Common learnings
Learnt from: generall
PR: qdrant/qdrant#6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The `populate()` method in `QuantizedMmapStorage` correctly returns void instead of a `Result` because it directly implements the `Madviseable` trait which defines `populate(&self)` without a return type. Adding an unnecessary `Ok(())` return would trigger Clippy warnings about unnecessary Result wrapping.
Learnt from: generall
PR: qdrant/qdrant#6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The `populate()` method in `QuantizedMmapStorage` does not return a `Result` because the underlying `mmap.populate()` method doesn't return a Result either. Adding an unnecessary `Ok(())` would cause Clippy to complain.
lib/gridstore/src/page.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

lib/common/memory/src/chunked_utils.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

lib/segment/src/vector_storage/dense/mmap_dense_vectors.rs (4)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

Learnt from: timvisee
PR: #6569
File: lib/segment/src/vector_storage/sparse/volatile_sparse_vector_storage.rs:56-76
Timestamp: 2025-05-21T12:52:15.229Z
Learning: In volatile sparse vector storage implementations, the total_sparse_size field should be updated when vectors are inserted, updated, or deleted to maintain an accurate count of the total number of non-zero elements across all vectors.

lib/common/memory/src/mmap_type_readonly.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: timvisee
PR: #6644
File: lib/gridstore/src/blob.rs:41-57
Timestamp: 2025-06-06T07:52:38.478Z
Learning: In the Blob trait implementations for gridstore (lib/gridstore/src/blob.rs), the maintainer prefers using assert! for input validation over Result-based error handling, as they don't expect invalid inputs in real scenarios and changing to Result would require updating the entire trait interface.

🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: generall
PR: qdrant/qdrant#6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The `populate()` method in `QuantizedMmapStorage` correctly returns void instead of a `Result` because it directly implements the `Madviseable` trait which defines `populate(&self)` without a return type. Adding an unnecessary `Ok(())` return would trigger Clippy warnings about unnecessary Result wrapping.
Learnt from: generall
PR: qdrant/qdrant#6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The `populate()` method in `QuantizedMmapStorage` does not return a `Result` because the underlying `mmap.populate()` method doesn't return a Result either. Adding an unnecessary `Ok(())` would cause Clippy to complain.
lib/gridstore/src/page.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

lib/common/memory/src/chunked_utils.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

lib/segment/src/vector_storage/dense/mmap_dense_vectors.rs (4)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: coszio
PR: #6565
File: lib/posting_list/src/builder.rs:63-67
Timestamp: 2025-05-26T14:47:23.505Z
Learning: In the posting_list crate's PostingChunk struct, avoid adding extra fields like storing computed chunk_bits to prevent struct bloat and maintain mmap compatibility. The bit calculations from offsets are inexpensive compared to the memory and compatibility benefits of keeping the struct minimal.

Learnt from: timvisee
PR: #6569
File: lib/segment/src/vector_storage/sparse/volatile_sparse_vector_storage.rs:56-76
Timestamp: 2025-05-21T12:52:15.229Z
Learning: In volatile sparse vector storage implementations, the total_sparse_size field should be updated when vectors are inserted, updated, or deleted to maintain an accurate count of the total number of non-zero elements across all vectors.

lib/common/memory/src/mmap_type_readonly.rs (3)

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage correctly returns void instead of a Result because it directly implements the Madviseable trait which defines populate(&self) without a return type. Adding an unnecessary Ok(()) return would trigger Clippy warnings about unnecessary Result wrapping.

Learnt from: generall
PR: #6323
File: lib/segment/src/vector_storage/quantized/quantized_mmap_storage.rs:12-16
Timestamp: 2025-04-07T23:31:22.515Z
Learning: The populate() method in QuantizedMmapStorage does not return a Result because the underlying mmap.populate() method doesn't return a Result either. Adding an unnecessary Ok(()) would cause Clippy to complain.

Learnt from: timvisee
PR: #6644
File: lib/gridstore/src/blob.rs:41-57
Timestamp: 2025-06-06T07:52:38.478Z
Learning: In the Blob trait implementations for gridstore (lib/gridstore/src/blob.rs), the maintainer prefers using assert! for input validation over Result-based error handling, as they don't expect invalid inputs in real scenarios and changing to Result would require updating the entire trait interface.

🔇 Additional comments (5)
lib/common/memory/src/mmap_type_readonly.rs (2)

150-153: Well-implemented populate method for slice types.

The new populate method correctly delegates to the underlying Arc<Mmap> and provides consistent error handling by returning std::io::Result<()>. This enables other components to populate sequentially-advised read-only memory maps for better performance.


340-345: Consistent delegation pattern for MmapSliceReadOnly.

The implementation properly delegates to the internal MmapTypeReadOnly<[T]> instance and maintains error propagation. The documentation clearly explains the method's purpose.

lib/segment/src/vector_storage/dense/mmap_dense_vectors.rs (1)

248-248: Excellent performance optimization aligned with PR objectives.

Switching to mmap_sequential.populate() leverages the kernel's larger readahead windows for sequential access patterns, directly addressing the performance issue described in the PR. This change improves dense vector storage performance while maintaining existing functionality.

lib/gridstore/src/page.rs (1)

153-153: Consistent optimization for gridstore pages.

The change to use mmap_seq.populate() follows the same performance optimization pattern as other components in this PR, leveraging sequential access advice for better readahead behavior during population.

lib/common/memory/src/chunked_utils.rs (1)

47-47: Proper integration with new MmapSliceReadOnly populate method.

The change correctly utilizes the newly added populate() method from MmapSliceReadOnly<T> and maintains consistent error handling. This optimization benefits chunked storage by leveraging sequential access patterns during memory population.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@generall generall merged commit a959e8a into dev Jul 22, 2025
16 checks passed
@generall generall deleted the madv-populate-sequential branch July 22, 2025 12:05
@agourlay
Copy link
Member

Awesome work 👏

I spent some time investigating some page fault around populate a while back.
It might be somewhat related #6714

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.

3 participants