Skip to content

Add asyncio-compatible Channel API#18

Merged
benoitc merged 6 commits intomainfrom
feature/channel-api
Mar 8, 2026
Merged

Add asyncio-compatible Channel API#18
benoitc merged 6 commits intomainfrom
feature/channel-api

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 8, 2026

Summary

  • Add bidirectional Channel API for Erlang-Python message passing using enif_ioq for zero-copy buffering
  • Implement Python asyncio integration with async for iteration and await channel.receive()
  • Add benchmark comparing Channel vs Reactor performance (7.9x faster for small messages, 2.0x for 16KB)
  • Optimize large message performance by eliminating double allocation/copy in send path

benoitc added 6 commits March 8, 2026 18:35
Bidirectional channel for Erlang-Python communication with:
- Sync: receive(), try_receive(), __iter__
- Async: async_receive(), __aiter__/__anext__
- Backpressure via max_size option
- Zero-copy buffering using enif_ioq

Async support reuses timer dispatch pattern - no new event types needed.
Add channel_send_owned_binary() that takes ownership of a binary and
directly enqueues it to the IOQueue without an additional copy.

Before: nif_channel_send called enif_term_to_binary (alloc + copy),
then channel_send which called enif_alloc_binary + memcpy (alloc + copy).
This resulted in 2 allocations and 2 copies per message.

After: nif_channel_send calls enif_term_to_binary then passes the binary
directly to channel_send_owned_binary which enqueues without copying.
This results in 1 allocation and 1 copy per message.

For 16KB messages, this eliminates 16KB of unnecessary memory operations
per send, improving speedup vs Reactor from 1.6x to 2.0x.
Remove info_msg calls from async_pending and write_ready handlers
that were polluting logs and adding overhead on every async request.
- Add docs/channel.md with API reference and examples
- Update CHANGELOG.md with Channel API features
@benoitc benoitc merged commit a46b940 into main Mar 8, 2026
11 checks passed
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