Skip to content

chore: limit Vec preallocation to follow serde convention#6616

Merged
LesnyRumcajs merged 1 commit intomainfrom
limit-allocs-deserialization
Feb 16, 2026
Merged

chore: limit Vec preallocation to follow serde convention#6616
LesnyRumcajs merged 1 commit intomainfrom
limit-allocs-deserialization

Conversation

@LesnyRumcajs
Copy link
Copy Markdown
Member

@LesnyRumcajs LesnyRumcajs commented Feb 16, 2026

Summary of changes

Changes introduced in this pull request:

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Outside contributions

  • I have read and agree to the CONTRIBUTING document.
  • I have read and agree to the AI Policy document. I understand that failure to comply with the guidelines will lead to rejection of the pull request.

Summary by CodeRabbit

  • Bug Fixes

    • Reduced excessive memory preallocation during deserialization by applying a more cautious sizing strategy, improving memory stability.
  • Tests

    • Added unit tests validating the cautious sizing behavior across representative sizes.
  • Performance

    • Improved vector pre-allocation during bulk reads to reduce reallocations and improve efficiency.

@LesnyRumcajs LesnyRumcajs requested a review from a team as a code owner February 16, 2026 13:47
@LesnyRumcajs LesnyRumcajs requested review from akaladarshi and sudo-shashank and removed request for a team February 16, 2026 13:47
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 16, 2026

No actionable comments were generated in the recent review. 🎉


Walkthrough

Adds a private helper size_hint_cautious_cid(...) that clamps preallocation for CID collections (1 MiB cap) and uses it in CBOR CID deserialization to compute reserved capacity; also preallocates the CID Vec in a chain store read path. No public API changes.

Changes

Cohort / File(s) Summary
Encoding helper & tests
src/utils/encoding/mod.rs
Adds private size_hint_cautious_cid(size_hint: usize) -> usize with MAX_PREALLOC_BYTES = 1 MiB and unit tests covering 0 and 1 MiB hints.
CID deserialization
src/utils/encoding/cid_de_cbor.rs
Replace direct reserve from size hints with capacity computed via size_hint_cautious_cid(...) on both MapAccess and SeqAccess deserialization paths (affects allocation sizing only).
Chain store prealloc
src/chain/store/chain_store.rs
Initialize CID result vector with Vec::with_capacity(amt.count() as usize) and populate via Amt::for_each_cacheless instead of index-based get loop.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

rust

Suggested reviewers

  • akaladarshi
  • sudo-shashank
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: limiting Vec preallocation during deserialization to follow serde convention, which is the primary objective of the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch limit-allocs-deserialization

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/utils/encoding/mod.rs`:
- Around line 12-18: The helper size_hint_cautious<T> can divide by zero for
zero-sized types (ZSTs); update it to first check std::mem::size_of::<T>() and
if it is 0 return size_hint unchanged (or otherwise avoid dividing) before
computing MAX_PREALLOC_BYTES / size; modify the body of size_hint_cautious to
guard on the size (using a local let size = std::mem::size_of::<T>() and an
early return when size == 0) and then compute size_hint.min(MAX_PREALLOC_BYTES /
size).
🧹 Nitpick comments (1)
src/utils/encoding/mod.rs (1)

270-279: Add a ZST case to cover the new guard.

After guarding size_of::<T>() == 0, add a () test case so regressions are caught.

🧪 Suggested test tweak
     fn size_hint_cautious_test() {
         assert_eq!(size_hint_cautious::<u8>(0), 0);
         assert_eq!(size_hint_cautious::<u8>(1024 * 1024), 1024 * 1024);
         assert_eq!(size_hint_cautious::<u8>(2 * 1024 * 1024), 1024 * 1024);
         assert_eq!(
             size_hint_cautious::<u64>(2 * 1024 * 1024),
             (1024 * 1024) / std::mem::size_of::<u64>()
         );
+        assert_eq!(size_hint_cautious::<()>(2 * 1024 * 1024), 1024 * 1024);
     }

Comment thread src/utils/encoding/mod.rs Outdated
@akaladarshi
Copy link
Copy Markdown
Collaborator

@LesnyRumcajs CI is failing

@LesnyRumcajs LesnyRumcajs force-pushed the limit-allocs-deserialization branch from 2fda973 to fa6df15 Compare February 16, 2026 16:23
@LesnyRumcajs LesnyRumcajs force-pushed the limit-allocs-deserialization branch from fa6df15 to 99c495a Compare February 16, 2026 16:24
@LesnyRumcajs LesnyRumcajs added the RPC requires calibnet RPC checks to run on CI label Feb 16, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 63.21%. Comparing base (63a6089) to head (99c495a).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/chain/store/chain_store.rs 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/utils/encoding/cid_de_cbor.rs 85.82% <100.00%> (+0.21%) ⬆️
src/utils/encoding/mod.rs 100.00% <100.00%> (ø)
src/chain/store/chain_store.rs 68.67% <80.00%> (ø)

... and 12 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63a6089...99c495a. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LesnyRumcajs LesnyRumcajs added this pull request to the merge queue Feb 16, 2026
Merged via the queue into main with commit 24ab830 Feb 16, 2026
34 checks passed
@LesnyRumcajs LesnyRumcajs deleted the limit-allocs-deserialization branch February 16, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RPC requires calibnet RPC checks to run on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants