Skip to content

Remove the per-item heap allocation from Accumulator::add - #12

Open
JSKitty wants to merge 1 commit into
nostrdevkit:masterfrom
JSKitty:perf-accumulator-add
Open

Remove the per-item heap allocation from Accumulator::add#12
JSKitty wants to merge 1 commit into
nostrdevkit:masterfrom
JSKitty:perf-accumulator-add

Conversation

@JSKitty

@JSKitty JSKitty commented Aug 9, 2026

Copy link
Copy Markdown

add is called once per item while sealing a storage, so building a 100,000-item client store performed 100,000 heap allocations. Each one was a 32-byte Vec holding four u64 lanes, copied over self.buf and freed immediately.

Write each lane straight back into self.buf and track the carry with overflowing_add instead. The arithmetic is the same four-lane little-endian addition; only the scratch buffer and the Wrapping comparisons around it are gone.

Measured on a 100,000-item client store reconciling against a 110,000-item relay, best of 25 runs:

client build and reconcile   2345 us -> 650 us
full exchange                4617 us -> 846 us
allocations                  107,033 -> 620

`add` is called once per item while sealing a storage, so building a
100,000-item client store performed 100,000 heap allocations. Each one was
a 32-byte `Vec` holding four `u64` lanes, copied over `self.buf` and freed
immediately.

Write each lane straight back into `self.buf` and track the carry with
`overflowing_add` instead. The arithmetic is the same four-lane
little-endian addition; only the scratch buffer and the `Wrapping`
comparisons around it are gone.

Measured on a 100,000-item client store reconciling against a 110,000-item
relay, best of 25 runs:

    client build and reconcile   2345 us -> 650 us
    full exchange                4617 us -> 846 us
    allocations                  107,033 -> 620

Peak memory is unchanged: the removed allocations were short-lived and
never coexisted.
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