Repository navigation
rust: store client events behind an Arc in the broadcast rings - #626
Draft
Harry Askham (harryaskham) wants to merge 1 commit into
Draft
Harry Askham (harryaskham) wants to merge 1 commit into
Harry Askham (harryaskham) wants to merge 1 commit into
Conversation
`Client` keeps one `tokio::sync::broadcast` ring for all events and one per attached resource. `broadcast` allocates and initializes every slot when a ring is created, and the event was stored inline, so each ring cost `subscription_buffer` x ~640 bytes for as long as it lived, whether or not anything was sent on it or read from it. - Store `Arc<event>` in the slot (`Slot<T>`): 32 bytes per slot on 64-bit targets instead of 656 (all events) and 632 (per resource). - Only build and send an event when the ring has a receiver. `send` stores nothing without one, and receivers are only added under the lock `fan_out` holds, so the check cannot race with a new receiver. - On `recv`, the last receiver of an event gets the event itself and earlier ones a clone: one clone fewer per event than before, at the cost of one small allocation per delivered event. - Capacity, lag behaviour, ordering, end of stream and the public API are unchanged. - Add `tests/event_rings.rs` for the delivery semantics and the cost of an idle ring, and a changelog fragment.
|
Harry Askham (@harryaskham) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
This branch has not been deployed
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.
Summary
ahp::Clientkeeps onetokio::sync::broadcastring for all events and one more for every attached resource.broadcastallocates and initializes every slot when the channel is created, and the event is stored inline, so each ring costssubscription_buffer× ~640 bytes for as long as it lives, whether or not anything is ever sent on it or read from it.This PR stores the event behind an
Arc, so an idle slot is 32 bytes.mainClient::connect, defaultsubscription_buffer(256)attach_subscription, default (256)Client::connect,subscription_buffer: 4096attach_subscription,subscription_buffer: 4096Measured with the counting allocator from the new test, on x86_64 Linux with rustc 1.99.0, at e36b0a2. There
size_of::<ClientEvent>()is 632 andsize_of::<SubscriptionEvent>()is 608.The cost is per ring, so it multiplies. A client that follows many resources holds one ring per resource. The multi-host runtime also calls
attach_subscriptionon everysubscribeand drops the handle (hosts/runtime.rs,handle_subscribe), so each subscribed resource holds a ring there too.Changes
Slot<T> = Arc<T>for the ring types inclient.rs.fan_outonly builds and sends an event when the ring has a receiver.sendstores nothing without one, and receivers are only added under the lockfan_outholds, so the check cannot race with a new receiver.recvtakes the event out of the slot: the last receiver of an event gets the event itself and earlier ones a clone, which is one clone fewer per event than before.Unchanged
Trade-off
One small allocation (the
Arc) per event delivered to a ring that has a receiver.Tests
tests/event_rings.rs. Four tests pin the delivery semantics and pass onmainas well. The fifth,an_idle_ring_costs_a_pointer_per_slot, fails onmain("the all-events ring requested 2693448 bytes for 4096 slots").#[global_allocator]in the test binary, which needs anunsafe impl GlobalAlloc. The library keeps#![forbid(unsafe_code)]. Ifunsafein a test is unwelcome, that one test can go and the other four stay.clients/rust:cargo fmt --all -- --check,cargo clippy --workspace -- -D warningsandcargo test --workspacepass.scripts/verify-change-fragments.tspasses.npm test, and the other clients, which this does not touch.A changelog fragment is added for
rust.