Skip to content

[ENH]: Precompute data chunk len()#6442

Merged
tanujnay112 merged 1 commit intomainfrom
metadata_len_opt
Feb 15, 2026
Merged

[ENH]: Precompute data chunk len()#6442
tanujnay112 merged 1 commit intomainfrom
metadata_len_opt

Conversation

@tanujnay112
Copy link
Copy Markdown
Contributor

@tanujnay112 tanujnay112 commented Feb 15, 2026

Description of changes

  • Improvements & Bug fixes
    • DataChunk.len() used to count the number of visibility[i] = True values to compute the length(). This is inefficient to compute on every invocation. The metadata log reader has a loop that invokes that invokes this len() function for every fetched log record. This appears to continue to take up > 40% of CPU on stack traces during gets on a high number of log records. This change precomputes this length value to fix this perf issue.
  • New functionality
    • ...

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the _docs section?_

@github-actions
Copy link
Copy Markdown

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@tanujnay112 tanujnay112 marked this pull request as ready for review February 15, 2026 00:48
@propel-code-bot
Copy link
Copy Markdown
Contributor

Precompute Chunk visible length

Adds a cached visible_count to Chunk<T> so that len() returns the precomputed value instead of iterating visibility on every call. The constructor, Clone, and set_visibility paths now initialize or refresh the cached count, eliminating the repeated CPU-intensive scan noted in metadata log reads.

Key Changes

• Extended Chunk<T> in rust/types/src/data_chunk.rs with a visible_count field cloned alongside data and visibility.
• Initialized visible_count in Chunk::new to the full length of the backing data and returned it directly in len().
• Updated set_visibility to recompute visible_count whenever the visibility vector is replaced, keeping the cache consistent.

Possible Issues

• If any consumer mutates visibility without using set_visibility (e.g., by cloning the arc slice and modifying it), visible_count would desynchronize; reviewers should confirm such patterns do not exist.

This summary was automatically generated by @propel-code-bot

/// # Arguments
/// * `visibility` - A vector of boolean values indicating the visibility of the elements
pub fn set_visibility(&mut self, visibility: Vec<bool>) {
self.visible_count = visibility.iter().filter(|&v| *v).count();
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.

Important

[Logic] The method documentation explicitly states that visibility length should match the data chunk length. However, this is not enforced, which could lead to inconsistent state where len() (based on visible_count) disagrees with iter().count() (based on data.len() and visibility) if a longer visibility vector is passed.

Add an assertion to enforce this invariant and prevent subtle bugs.

Suggested change
self.visible_count = visibility.iter().filter(|&v| *v).count();
assert_eq!(visibility.len(), self.data.len(), "Visibility vector length must match data length");
self.visible_count = visibility.iter().filter(|&v| *v).count();
Context for Agents
The method documentation explicitly states that `visibility` length should match the data chunk length. However, this is not enforced, which could lead to inconsistent state where `len()` (based on `visible_count`) disagrees with `iter().count()` (based on `data.len()` and `visibility`) if a longer visibility vector is passed. 

Add an assertion to enforce this invariant and prevent subtle bugs.

```suggestion
        assert_eq!(visibility.len(), self.data.len(), "Visibility vector length must match data length");
        self.visible_count = visibility.iter().filter(|&v| *v).count();
```

File: rust/types/src/data_chunk.rs
Line: 81

@tanujnay112 tanujnay112 merged commit 874a1f9 into main Feb 15, 2026
121 of 133 checks passed
tanujnay112 added a commit that referenced this pull request Feb 18, 2026
- **[ENH]: Cache rust git submodules in mounted volume (#6424)**
- **[CHORE](k8s) increase dev CPU limits from 100m to 200-300m (#6435)**
- **[ENH] replace live cloud tests with k8s integration tests (#6434)**
- **[ENH] Make dirty_log_collections metric mcmr-aware. (#6353)**
- **[ENH] Quantized Spann Segment Writer (#6397)**
- **[ENH] Wire up quantized writer in compaction (#6399)**
- **[ENH] Quantized Spann Segment Reader (#6405)**
- **[ENH] Wire up quantized reader in new orchestrator (#6409)**
- **[ENH] Garbage collect usearch index files (#6416)**
- **[ENH] Trace quantized spann implementation (#6425)**
- **[ENH]: Precompute data chunk len() (#6442)**
- **[BUG]: Compaction version file flush was incomplete on MCMR
(#6423)**
- **[DOC]: Fixed broken links in Readme (#6440)**
- **[DOC] Fix link to Rust documentation (#6443)**
- **[ENH]: Allow users to disable FTS in schema (#6214)**

---------

Co-authored-by: Robert Escriva <[email protected]>
Co-authored-by: Macronova <[email protected]>
Co-authored-by: Nilpotent <[email protected]>
Co-authored-by: anderk222 <[email protected]>
Co-authored-by: Sanket Kedia <[email protected]>
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.

2 participants