chipingress/batch: compute batch-split sizes incrementally#2106
Draft
pkcll wants to merge 1 commit into
Draft
Conversation
📊 API Diff Results
|
…tchSize splitMessagesByRequestSize re-serialized the entire growing batch on every message (newBatchRequest -> proto.Size), so splitting an n-event batch walked 1+2+...+n events worth of bytes. Accumulate each event's repeated-field contribution incrementally instead, sizing the batch in O(n). eventFieldSize computes the per-event cost via protowire and matches proto.Size exactly; subsequent split batches no longer pre-allocate a full-length backing array. Also raise the default NewBatchClient batchSize from 10 to 100 for ~10x more events per PublishBatch under sustained load. batchInterval (100ms) still bounds latency for partial batches, 100 stays below the default messageBuffer (200) so the size trigger still fires, and larger batches that exceed maxGRPCRequestSize are now split in O(n). Callers can still override via WithBatchSize. Benchmarks (BenchmarkSplitMessagesByRequestSize, ~10 events/batch): n=10 2940ns/1208B/22 allocs -> 580ns/168B/3 allocs n=100 30767ns/15536B/255 allocs -> 6335ns/4160B/56 allocs n=1000 322229ns/157616B/2558 allocs -> 64900ns/43040B/559 allocs Tests assert the incremental total equals proto.Size and that every produced sub-batch fits the limit with no events dropped.
f781286 to
bd237fe
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This branch has two related changes to the chipingress batch client.
1. Compute batch-split sizes incrementally (O(n) instead of O(n²))
splitMessagesByRequestSizere-serialized the entire growing batch on every message (newBatchRequest→proto.Size), so splitting an n-event batch walked1+2+…+nevents' worth of bytes — O(n²). This accumulates each event's repeated-field contribution incrementally instead, sizing the batch in O(n).A
CloudEventBatchencodes events as a repeated field, so its serialized size is a fixed base plus the independent per-event contribution (tag+ length-delimited message).eventFieldSizecomputes that viaprotowireand matchesproto.Sizeexactly. Subsequent split batches also no longer pre-allocate a full-length backing array.Benchmarks
BenchmarkSplitMessagesByRequestSize(~10 events per sub-batch, forcing many splits):The old implementation is kept in the test file as the
quadraticbenchmark baseline so the comparison is reproducible:Tests
eventFieldSizeaccumulation equalsproto.Sizefor the whole batch (guards the additive-size assumption).proto.Size, with no events dropped across splits.-race).2. Raise default
batchSizefrom 10 to 100NewBatchClient's defaultbatchSizewas 10; raised to 100 for ~10x more events perPublishBatchunder sustained load. This is safe alongside change #1 since larger batches that exceedmaxGRPCRequestSizeare now split in O(n).batchInterval(100ms) still bounds latency for partial batches, so this only affects per-RPC event count under load, not tail latency.messageBuffer(200), so the size trigger still fires under load — no other default needs retuning.WithBatchSize.