Skip to content

core: optimize away dynamic_cast, avoid allocations - #424

Merged
MariusBgm merged 9 commits into
mainfrom
dev/zero_copy_opt
Nov 3, 2026
Merged

MariusBgm merged 9 commits into
mainfrom
dev/zero_copy_opt

Conversation

@MariusBgm

@MariusBgm MariusBgm commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Three profiling-driven optimizations on top of #423. No API, ABI or wire format change.

  • Remove two per-message dynamic_cast calls: one was a plain upcast, the other is now resolved once at receiver registration. Takes the RTTI strcmp with it.
  • Stop rebuilding the received ServiceDescriptor per message; pool the serialization buffer and the receive blob (thread local, no locking).
  • Pass asio a single buffer instead of a buffer sequence when there is only one, and stop zero filling pooled blobs that are overwritten immediately afterwards.
  • Net: -16.9% instructions, -46% allocations per message (callgrind and DHAT, 10 byte payloads, two participants; wall clock is too noisy on WSL and Windows to resolve this).

@MariusBgm
MariusBgm added this pull request to stack #425 September 16, 2026 09:00
@MariusBgm MariusBgm changed the title dev/zero copy opt core: optimize away dynamic_cast, avoid allocations Sep 16, 2026
@MariusBgm
MariusBgm marked this pull request as ready for review September 16, 2026 09:11
@MariusBgm MariusBgm added the status: ready for review Complete and waiting for a reviewer. label Sep 23, 2026

@snps-fricke snps-fricke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Really nice work, the fan-out design is clean and very well tested. If anything, I'd reiterate on the lifetime of receive buffers and see if that path can be simplified.

@snps-fiodorov snps-fiodorov removed the status: ready for review Complete and waiting for a reviewer. label Oct 9, 2026
@KonradBreitsprecherBkd KonradBreitsprecherBkd added the status: approved Approved and waiting to be merged, for example for a release window. label Oct 21, 2026
@MariusBgm MariusBgm self-assigned this Oct 21, 2026
Base automatically changed from dev/zero_copy to main November 2, 2026 10:11
MariusBgm and others added 8 commits November 2, 2026 11:33
Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
Reusable receive descriptor — VAsioConnection now assigns into a member ServiceDescriptor instead of constructing a fresh one per received message. std::map assignment reuses nodes and std::string assignment reuses buffers, so the supplemental-data allocations disappear. Safe because only the io thread reaches it and dispatch is synchronous.

Pooled serialization buffer — a thread-local free list in MessageBuffer. The writing constructor takes a buffer from it; MakeSendItem hands it back once a small message has been copied inline. Thread-local means no synchronization, and a buffer acquired on one thread and recycled on another just migrates between lists.

Pooled receive blob — AcquireReceiveBlob reuses a blob only when its use_count() == 1, which is precisely the condition that no deserialized payload still aliases it. Self-correcting: if a payload somehow outlives dispatch, that entry is skipped and a fresh blob is allocated.

Both pools are capped (32 buffers / 8 blobs, 64 KB each) so large messages don't cause hoarding.

Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
Two independent findings from profiling the small message path with callgrind
and DHAT, 400,000 messages of 10 bytes between two participants.

asio copies a buffer sequence into the async operation, which DHAT attributed
as one allocation of 16 bytes per message. A single buffer is stored in the
operation by value instead, so AsyncWriteSome and AsyncReadSome now take that
path when the sequence holds exactly one buffer. That is the common case: a
small message is written as one contiguous buffer, and the receive ring only
needs a second buffer when its free region wraps.

AcquireReceiveBlob resized a pooled blob to the exact message size, value
initializing bytes that RingBuffer::Read overwrites immediately afterwards.
Pooled blobs now only grow and stay at their high water size, and the caller
reads and views exactly the message's bytes. This part is a regression from
the commit that introduced the pool.

Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
- replace thread_local receive blob pool with ReceiveBlobPool owned by VAsioConnection
- add Experimental.UseReceiveBufferPool (default true) to disable pooling for diagnosis
- reuse best-fitting blob, cap slack at 4 KiB, acquire fence after use_count check
- add VAsioPeer tests: inline/owned/shared send items, partial writes, aggregation, receive paths
- add ReceiveBlobPool, VAsioProxyPeer and serdes aliasing / RPC wire format tests
- add fan-out ITests: pubsub 1:3 (aggregation, pool off), RPC 1:2, large Ethernet frames, mixed direct/proxied subscribers
- use non-uniform payloads in pubsub ITest harness
- document UseReceiveBufferPool

Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
Signed-off-by: Marius Börschig <Marius.Boerschig@vector.com>
constructor, which the receive blob pool relied on.

- Add an explicit constructor viewing a whole shared vector without
  copying. VAsioPeer narrows it to the message with Subspan(), which
  stays the only way to narrow a view.
- Port the tests that still used MakeSharedSpan.

Signed-off-by: Marius Börschig <marius.boerschig@vector.com>
- Dispatch is synchronous and nothing keeps a received payload past its
  handler, so at most one blob is in use at a time.
- The use_count guard stays: a retained payload keeps its blob, and the
  pool continues with a fresh one instead of overwriting it.
- A retained payload now pins up to MaxBlobSize (64 KiB), since the
  slack bound is gone.

Signed-off-by: Marius Börschig <marius.boerschig@vector.com>
Comment thread SilKit/source/core/vasio/VAsioPeer.cpp Resolved
- Nothing on the receive path keeps a SerializedMessage past the call;
  lvalue references state that in the signatures.
- Not const&: the const Deserialize() copies the buffer, which for
  vector-backed messages (e.g. proxied ones) copies the payload.

Signed-off-by: Marius Börschig <marius.boerschig@vector.com>
@MariusBgm
MariusBgm merged commit b96e945 into main Nov 3, 2026
16 checks passed
@MariusBgm
MariusBgm deleted the dev/zero_copy_opt branch November 3, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: approved Approved and waiting to be merged, for example for a release window.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants