tensor-broker
tensor-broker demonstrates zero-copy cross-process sharing of a 64 MiB FP16
tensor ([4096, 8192], 67,108,864 bytes) on macOS using x/xpctensor
(IOSurfaceCreateXPCObject / IOSurfaceLookupFromXPCObject + x/zerocopy
SurfacePool / MTLBuffer) and a self-contained parent/child subprocess
benchmark (os.Args[0] -role=client).
What it proves
- 64 MiB FP16 handoff latency vs. Unix-domain socket copy control:
Measures
xpctensor.Client.Fetch (XPC Mach send right + MTLBuffer wrap)
and cross-process IOSurfaceLookup + NewBufferWithBytesNoCopy against a
64 MiB payload copy over a Unix-domain socket.
- Physical memory aliasing across processes:
- Parent flips sentinel bytes at offsets
0, n/3, n/2, and n-1 in its
MTLBuffer and confirms the child observes both the write and the restore
through its own MTLBuffer.Contents() without transferring tensor bytes.
- Child writes
0xCAFEBABEDEADBEEF at offset 64 of its MTLBuffer; parent
reads it directly from its own MTLBuffer.
- Crash cleanup on
SIGKILL:
- Abrupt XPC client disconnect triggers
BrokerOptions.OnPeerDisconnect and
decrements IOSurfaceGetUseCount back to 0.
- Sending
SIGKILL to the child subprocess holding the IOSurface lease
causes the kernel to tear down the child's descriptors immediately,
reclaiming the lease (useCount 1 -> 0, IOSurfaceIsInUse == false).
Usage
CGO_ENABLED=0 go run ./examples/xpc/tensor-broker
CGO_ENABLED=0 go test -v -count=1 ./examples/xpc/tensor-broker
Flags:
-size-mib (default 64): tensor size in MiB (FP16).
-iters (default 5): benchmark iterations.
-role (demo or client): internal re-exec switch for the child process.
Measured results
Measured on Apple M4 Max (128 GiB), macOS 26.6.2 (25G83), CGO_ENABLED=0:
xpctensor / tensor-broker benchmark (64 MiB FP16 tensor, 67108864 bytes)
=======================================================================
[1] x/xpctensor XPC Broker (IOSurfaceCreateXPCObject -> MTLBuffer)
Fetch latency (median / min): 289 µs / 151 µs
Zero-copy aliasing verified: true
Disconnect lease cleanup: true
[2] Cross-Process Parent <-> Child Subprocess
Zero-copy attach (median): 233 µs (IOSurfaceLookup: 19 µs, MTLBuffer wrap: 27 µs)
UDS 64 MiB copy (median): 1392.91 ms (45.9 MiB/s)
Speedup (zero-copy vs UDS): 5957.9x
Parent->Child alias check: true
Child->Parent sentinel seen: true
SIGKILL cleanup: useCount 1 -> 0, inUse=false (reclaimed in 3345 µs)