Skip to content

Add allocated_bytes and total_capacity to allocator types - #7

Open
caspark wants to merge 4 commits into
zakarumych:mainfrom
caspark:main
Open

Add allocated_bytes and total_capacity to allocator types#7
caspark wants to merge 4 commits into
zakarumych:mainfrom
caspark:main

Conversation

@caspark

@caspark caspark commented Feb 13, 2026

Copy link
Copy Markdown

Compute usage on demand by walking the chunk linked list: sum of previous chunk capacities (which are ~fully used) plus cursor offset in the current chunk. The result is exact once the blink allocator has been reset a few times, but is an approximation before then. Having these stats exposed is useful to figure out how much memory is used at various points of a program's execution.

  • allocated_bytes() ("estimate or exact count of bytes in use") and total_capacity() ("total number of bytes claimed from backing allocator") are named after bumpalo's terminology but I'm not particularly attached to those
  • The 3 tests do have quite a few assertions of the form of "do some calculation to figure out expected value, assert result of the calculation for code-reader's benefit, assert that system under test returns expected value". This is helpful to me as someone not used to doing these sorts of calculations in my head but as maintainer you may want to remove some of those.
  • genAI assisted - initial implementation + tests done genai, then I fixed up the comments and iterated on the tests to cut the bazillion test cases originally generated down to 3 (minimal-ish and hopefully sane) ones that test the same invariants.

Compute usage on demand by walking the chunk linked list: sum of
previous chunk capacities (which are ~fully used) plus cursor offset
in the current chunk. Zero overhead when not called, and exact once
the allocator has warmed up to a single chunk.

Exposed on BlinkAlloc, SyncBlinkAlloc, LocalBlinkAlloc, and
accessible through Blink via allocator().

Includes 24 tests covering basics, reset, warm-up convergence,
approximation error bounds, Vec/Blink adaptor usage, edge cases,
and multi-threaded sync allocator tracking.
- Use minimal numbers (item_size=3, item_count=11, initial_chunk=32)
- Assert exact values with descriptive failure messages
- Add test_tracking_local_proxy showing shared/local interaction
- Remove redundant LocalBlinkAlloc doc comments (were copy-paste of BlinkAlloc)
Comment thread src/arena/mod.rs
#[cfg(not(feature = "nightly"))]
use allocator_api2::alloc::{AllocError, Allocator};
#[cfg(feature = "nightly")]
use core::alloc::{AllocError, Allocator};

@caspark caspark Feb 13, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

cargo fmt changes are in a separate commit in case you want to drop them

Comment thread src/tests.rs
use crate::SyncBlinkAlloc;

/// ChunkHeader has 4 usize fields: cursor, end, prev, cumulative_size.
const CHUNK_HEADER_SIZE: usize = size_of::<usize>() * 4;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

would be neater to size_of the header but it would have to be pub(crate) then

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