Skip to content

Avoid the sort scratch allocation in NegentropyStorageVector::seal - #13

Open
JSKitty wants to merge 1 commit into
nostrdevkit:masterfrom
JSKitty:perf-seal-sort
Open

Avoid the sort scratch allocation in NegentropyStorageVector::seal#13
JSKitty wants to merge 1 commit into
nostrdevkit:masterfrom
JSKitty:perf-seal-sort

Conversation

@JSKitty

@JSKitty JSKitty commented Aug 9, 2026

Copy link
Copy Markdown

Vec::sort is a stable sort, and allocates a scratch buffer proportional to the slice. For a 100,000-item store that is a transient 3.9 MB, which doubles the peak memory of building a client store.

Item orders on timestamp then id, so two elements comparing equal are byte-identical, and dedup() on the next line discards one of them either way. Stability is unobservable here, so sort_unstable produces the same sealed contents without the allocation.

Measured while sealing a 100,000-item store:

peak heap    7812 KB -> 3906 KB
transient    3906 KB -> 0 KB

The removed transient is exactly 100_000 * size_of::<Item>(). Runtime is unchanged within measurement noise.

`Vec::sort` is a stable sort, and allocates a scratch buffer proportional
to the slice. For a 100,000-item store that is a transient 3.9 MB, which
doubles the peak memory of building a client store.

`Item` orders on timestamp then id, so two elements comparing equal are
byte-identical, and `dedup()` on the next line discards one of them
either way. Stability is unobservable here, so `sort_unstable` produces
the same sealed contents without the allocation.

Measured while sealing a 100,000-item store:

    peak heap    7812 KB -> 3906 KB
    transient    3906 KB -> 0 KB

The removed transient is exactly `100_000 * size_of::<Item>()`. Runtime is
unchanged within measurement noise.
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