Skip to content

Add shared memory regions and shared buffers over iommap - #73

Merged
benoitc merged 1 commit into
mainfrom
shared-memory
Aug 29, 2026
Merged

Add shared memory regions and shared buffers over iommap#73
benoitc merged 1 commit into
mainfrom
shared-memory

Conversation

@benoitc

@benoitc benoitc commented Aug 29, 2026

Copy link
Copy Markdown
Owner

Adds shared memory between Erlang and Python contexts, on top of iommap (an optional dependency).

Isolated contexts copy every argument and result through the socket, which is fine for terms and wrong for a 64 MB array. A py_shm region is a fixed-size file mapped MAP_SHARED by both sides: Erlang reads it with no copy and writes it with one, Python sees it as a buffer (numpy.frombuffer works on it directly). The handle is a plain term, so it travels inside any argument or result, and the same wrappers serve embedded and isolated contexts, so a pool can mix modes. py_buffer:new(#{shared => true}) builds the streaming input buffer (wsgi.input) on such a region, with a ring, backpressure on the writer, and reads that survive a child restart.

Flow control and notifications reuse the existing callback path rather than a new protocol, which is what makes the mechanism mode-independent. Sharing keeps fault isolation intact; it does hand the callee the bytes of the region, so handles can be passed read-only and py_shm:binary/3 is documented as a live view. Sealing and syscall filtering are left for the security isolation work.

Measured on macOS: 64 MB from Python to Erlang in 5.5 ms instead of 284 ms through the socket, 64 MB from Erlang to Python in 74 ms instead of 324 ms; below a couple of MB the socket stays cheaper. Validated on macOS and FreeBSD.

Bulk data between Erlang and Python contexts no longer has to cross the
socket: py_shm regions are files mapped MAP_SHARED on both sides (no copy
on the Erlang side, one copy when Erlang writes), usable as plain terms in
any context mode, and py_buffer:new(#{shared => true}) is a streaming
input buffer over such a region with ring backpressure, so wsgi.input
works in isolated contexts. iommap is an optional dependency. Read-only
handles keep a callee from writing into a region.
@benoitc
benoitc merged commit b8e5cbf into main Aug 29, 2026
39 of 40 checks passed
@benoitc
benoitc deleted the shared-memory branch August 29, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant