Skip to content

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

Merged
yukibtc merged 1 commit into
nostrdevkit:masterfrom
JSKitty:perf-accumulator-add
Aug 16, 2026
Merged

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

Conversation

@JSKitty

@JSKitty JSKitty commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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.

@yukibtc yukibtc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@yukibtc
yukibtc merged commit cd79754 into nostrdevkit:master Aug 16, 2026
1 of 7 checks passed
@JSKitty
JSKitty deleted the perf-accumulator-add branch August 16, 2026 10:30
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