feat(ffi): expose SHM provider through the FFI (+ hiroz-go WithShmPool) - #345
Open
richardw347 wants to merge 2 commits into
Open
richardw347 wants to merge 2 commits into
richardw347 wants to merge 2 commits into
Conversation
The Rust API supports shared memory (ZContextBuilder::with_shm_pool_size / with_shm_config), but the C FFI never surfaced it, so no FFI consumer (Go/C/ Python) could make a session SHM-capable. In zenoh 1.9 a session only negotiates SHM transport if it has an SHM provider attached; setting transport/shared_memory/enabled alone is not sufficient, so an FFI consumer's large payloads always fall back to a copy over the transport. - CContextConfig: add shm_pool_bytes (appended; ABI-additive). When non-zero, hiroz_context_create_with_config attaches a provider via with_shm_pool_size, mirroring the Rust builder; 0 disables (default). Regenerated hiroz_ffi.h. - hiroz-go: add ContextBuilder.WithShmPool(bytes) and marshal it into the config struct. Verified: hiroz builds with --features ffi and regenerates the header with the new field; hiroz-go builds and links WithShmPool against the resulting staticlib. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Resolve hiroz_ffi.h conflict by keeping shm_pool_bytes from this branch. Co-authored-by: Cursor <cursoragent@cursor.com>
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
The Rust API supports shared memory —
ZContextBuilder::with_shm_pool_size()/with_shm_config()and the wholeshmmodule — but the C FFI never surfacedit.
hiroz_context_config_thas no SHM field andhiroz_context_create_with_confignever attaches a provider, so no FFIconsumer (Go/C/Python) can make a session SHM-capable.
This matters because in zenoh 1.9 a session only negotiates SHM transport with a
peer if it has an SHM provider attached. Setting
transport/shared_memory/enabled: truevia config alone is not sufficient — aprovider-less peer is treated as non-SHM and the router copies payloads out over
the transport. So an FFI consumer receiving large payloads (e.g. camera frames)
from an SHM-capable Rust publisher silently falls back to a full copy over TCP,
even with SHM "enabled".
Changes
CContextConfig: addshm_pool_bytes: usize(appended after existingfields → ABI-additive). When non-zero,
hiroz_context_create_with_configattaches a provider via
builder.with_shm_pool_size(cfg.shm_pool_bytes),mirroring the Rust builder;
0disables (default). Regeneratedhiroz_ffi.h.ContextBuilder.WithShmPool(bytes uint64)and marshal itinto the config struct.
Both mirror existing patterns (
with_shm_pool_sizealready exists in the core;the FFI error handling mirrors the
with_remap_rulesblock).Sizing note for consumers
A receiver needs only a small pool — it maps the publisher's segments and
allocates nothing per message; the pool exists solely to make the session
SHM-capable. A publisher needs a pool large enough for its in-flight payloads.
Verification
cargo build --features fficompiles the FFI change and regenerateshiroz_ffi.hwithuintptr_t shm_pool_bytes;inhiroz_context_config_t.WithShmPoolagainst the resulting staticlib.Provenance
Found consuming hiroz from Go (cgo) on a robot: a camera driver (Rust,
SHM-capable) published 20MP frames into SHM, but the Go consumer — unable to
attach a provider through the FFI — received them over TCP loopback (measured
~780 MB/s on
loduring capture where SHM would be ~KB/s). This PR is themissing FFI capability, not a workaround.