-
-
Notifications
You must be signed in to change notification settings - Fork 127
perf(store): reserve the prekey map for a batch insert #1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+255
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa04c47
perf(store): reserve the prekey map for a batch insert
claude 10d4057
docs(observability): point the prekey-reserve ledger note at the righ…
claude cf70e5e
perf(store): reserve only the prekey rows a batch must add
claude 4dfd8b5
perf(store): reserve for a prekey batch only when its ids are distinct
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! Allocation accounting for the prekey batch write on the connect path. | ||
| //! | ||
| //! `upload_pre_keys_pass` generates `DEFAULT_WANTED_PRE_KEY_COUNT` (812) one-time | ||
| //! prekeys and hands them to `store_prekeys_batch` as one call. `divan::AllocProfiler` | ||
| //! is wired as the global allocator so each row reports allocation count and bytes | ||
| //! next to wall time -- the count is the signal here, since the map's growth is the | ||
| //! only thing this path allocates: the records themselves are `Bytes` slices of one | ||
| //! shared buffer the caller already owns, so they cost refcount bumps, not copies. | ||
| //! | ||
| //! The `populated` row is the second upload pass: the same batch size arriving at a | ||
| //! map that already holds a window. It exists so a reservation that over-grows an | ||
| //! already-populated table would show up as bytes here rather than silently. | ||
|
|
||
| use bytes::Bytes; | ||
| use divan::{Bencher, black_box}; | ||
| use futures::executor::block_on; | ||
| use wacore::store::in_memory::InMemoryBackend; | ||
| use wacore::store::traits::SignalStore; | ||
|
|
||
| #[global_allocator] | ||
| static ALLOC: divan::AllocProfiler = divan::AllocProfiler::system(); | ||
|
|
||
| fn main() { | ||
| divan::main(); | ||
| } | ||
|
|
||
| /// `DEFAULT_WANTED_PRE_KEY_COUNT` in `src/prekeys.rs`, mirroring WA Web's | ||
| /// UPLOAD_KEYS_COUNT. The small row is there to show the growth is what scales. | ||
| const CONNECT_BATCH: usize = 812; | ||
|
|
||
| /// Upper bound on one encoded `PreKeyRecordStructure`, the same figure | ||
| /// `upload_pre_keys_pass` sizes its shared buffer with. | ||
| const RECORD_LEN: usize = 74; | ||
|
|
||
| /// The batch as the upload path actually hands it over: every record is a slice of | ||
| /// one contiguous buffer, so building it allocates once regardless of `count`. | ||
| fn batch(first_id: u32, count: usize) -> Vec<(u32, Bytes)> { | ||
| let shared = Bytes::from(vec![7u8; count * RECORD_LEN]); | ||
| (0..count) | ||
| .map(|i| { | ||
| ( | ||
| first_id + i as u32, | ||
| shared.slice(i * RECORD_LEN..(i + 1) * RECORD_LEN), | ||
| ) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| #[divan::bench(args = [64, CONNECT_BATCH])] | ||
| fn store_prekeys_batch(bencher: Bencher, count: usize) { | ||
| bencher | ||
| .with_inputs(|| (InMemoryBackend::new(), batch(1, count))) | ||
| .bench_refs(|(backend, keys)| { | ||
| block_on(backend.store_prekeys_batch(keys, false)).unwrap(); | ||
| black_box(&backend); | ||
| }); | ||
| } | ||
|
|
||
| /// Prekey ids are minted from the monotonic NEXT_PK_ID counter, so a second pass | ||
| /// carries ids past the first window's -- every row is new, none overwrites. | ||
| #[divan::bench(name = "store_prekeys_batch/populated")] | ||
| fn store_prekeys_batch_populated(bencher: Bencher) { | ||
| bencher | ||
| .with_inputs(|| { | ||
| let backend = InMemoryBackend::new(); | ||
| block_on(backend.store_prekeys_batch(&batch(1, CONNECT_BATCH), true)).unwrap(); | ||
| let next = batch(CONNECT_BATCH as u32 + 1, CONNECT_BATCH); | ||
| (backend, next) | ||
| }) | ||
| .bench_refs(|(backend, keys)| { | ||
| block_on(backend.store_prekeys_batch(keys, false)).unwrap(); | ||
| black_box(&backend); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new benchmark calls
.unwrap()here and again on lines 66 and 71, contrary to the repository-wide prohibition on.unwrap()outside tests. These calls also make benchmark failures panic without contextual error propagation.Context Used: CLAUDE.md (source)
Prompt To Fix With AI