Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
461 changes: 461 additions & 0 deletions ASGI_NIF_OPTIMIZATIONS.md

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
# Changelog

## 1.8.0 (Unreleased)

### Added

- **ASGI NIF Optimizations** - Six optimizations for high-performance ASGI request handling
- **Direct Response Tuple Extraction** - Extract `(status, headers, body)` directly without generic conversion
- **Pre-Interned Header Names** - 16 common HTTP headers cached as PyBytes objects
- **Cached Status Code Integers** - 14 common HTTP status codes cached as PyLong objects
- **Zero-Copy Request Body** - Large bodies (≥1KB) use buffer protocol for zero-copy access
- **Scope Template Caching** - Thread-local cache of 64 scope templates keyed by path hash
- **Lazy Header Conversion** - Headers converted on-demand for requests with ≥4 headers

- **erlang_asyncio Module** - Asyncio-compatible primitives using Erlang's native scheduler
- `erlang_asyncio.sleep(delay, result=None)` - Sleep using Erlang's `erlang:send_after/3`
- `erlang_asyncio.run(coro)` - Run coroutine with ErlangEventLoop
- `erlang_asyncio.gather(*coros)` - Run coroutines concurrently
- `erlang_asyncio.wait_for(coro, timeout)` - Wait with timeout
- `erlang_asyncio.wait(fs, timeout, return_when)` - Wait for multiple futures
- `erlang_asyncio.create_task(coro)` - Create background task
- `erlang_asyncio.ensure_future(coro)` - Wrap coroutine in Future
- `erlang_asyncio.shield(arg)` - Protect from cancellation
- `erlang_asyncio.timeout` - Context manager for timeouts
- Event loop functions: `get_event_loop()`, `new_event_loop()`, `set_event_loop()`, `get_running_loop()`
- Re-exports: `TimeoutError`, `CancelledError`, `ALL_COMPLETED`, `FIRST_COMPLETED`, `FIRST_EXCEPTION`

- **Erlang Sleep NIF** - Synchronous sleep primitive for Python
- `py_event_loop._erlang_sleep(delay_ms)` - Sleep using Erlang timer
- Releases GIL during sleep, no Python event loop overhead
- Uses pthread condition variables for efficient blocking
- `py_nif:dispatch_sleep_complete/2` - NIF to signal sleep completion

- **Scalable I/O Model** - Worker-per-context architecture
- `py_event_worker` - Dedicated worker process per Python context
- Combined FD event dispatch and reselect via `handle_fd_event_and_reselect` NIF
- Sleep tracking with `sleeps` map in worker state

- **New Test Suite** - `test/py_erlang_sleep_SUITE.erl` with 8 tests
- `test_erlang_sleep_available` - Verify NIF is exposed
- `test_erlang_sleep_basic` - Basic functionality
- `test_erlang_sleep_zero` - Zero delay returns immediately
- `test_erlang_sleep_accuracy` - Timing accuracy
- `test_erlang_asyncio_module` - Module functions present
- `test_erlang_asyncio_gather` - Concurrent execution
- `test_erlang_asyncio_wait_for` - Timeout support
- `test_erlang_asyncio_create_task` - Background tasks

### Performance

- **ASGI marshalling optimizations** - 40-60% improvement for typical ASGI workloads
- Direct response extraction: 5-10% improvement
- Pre-interned headers: 3-5% improvement
- Cached status codes: 1-2% improvement
- Zero-copy body buffers: 10-15% for large bodies (≥1KB)
- Scope template caching: 15-20% for repeated paths
- Lazy header conversion: 5-10% for apps accessing few headers
- **Eliminates event loop overhead** for sleep operations (~0.5-1ms saved per call)
- **Sub-millisecond timer precision** via BEAM scheduler (vs 10ms asyncio polling)
- **Zero CPU when idle** - event-driven, no polling

## 1.7.1 (2026-02-23)

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ py:execution_mode(). %% => free_threaded | subinterp | multi_executor
- [Streaming](docs/streaming.md)
- [Threading](docs/threading.md)
- [Logging and Tracing](docs/logging.md)
- [Asyncio Event Loop](docs/asyncio.md) - Erlang-native asyncio with TCP/UDP support
- [Web Frameworks](docs/web-frameworks.md) - ASGI/WSGI integration
- [Changelog](https://github.com/benoitc/erlang-python/releases)

## License
Expand Down
Loading