Skip to content

perf: cache BaseVec len in Cell<u64> to avoid stable memory reads#415

Draft
sasa-tomic wants to merge 1 commit intomainfrom
perf/basevec-len-cache
Draft

perf: cache BaseVec len in Cell<u64> to avoid stable memory reads#415
sasa-tomic wants to merge 1 commit intomainfrom
perf/basevec-len-cache

Conversation

@sasa-tomic
Copy link
Contributor

@sasa-tomic sasa-tomic commented Mar 18, 2026

Summary

  • Cache BaseVec::len() in a Cell<u64> instead of reading from stable memory on every call
  • MinHeap calls len() O(log n) times per push/pop (via bounds checks in sift_up/sift_down), making this a significant overhead — eliminates ~13-26 stable memory reads per MinHeap operation on heaps of 10K elements
  • Cell<u64> is necessary because all BaseVec data methods use &self (interior mutability via Memory trait)

BaseVec::len() previously read from stable memory on every call.
MinHeap calls len() O(log n) times per push/pop via sift_up/sift_down
bounds checks, making this a significant overhead. Cache the length
in a Cell<u64> and keep it in sync with the persisted value.
@sasa-tomic sasa-tomic requested a review from a team as a code owner March 18, 2026 17:39
@sasa-tomic sasa-tomic marked this pull request as draft March 18, 2026 17:53
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.

1 participant