Overview
httpz is a zero-allocation HTTP/1.1 parser for OxCaml that achieves exceptional performance through careful architectural choices. The parser processes HTTP requests without any heap allocations by leveraging OxCaml’s unboxed types, stack-allocated data structures, and span-based parsing.Core Design Principles
1. Buffer Reuse Strategy
httpz uses a fixed 32KB pre-allocated buffer that is reused across all requests:2. Span-Based Parsing
Instead of copying strings from the buffer, httpz uses spans - lightweight references into the buffer:int16#). Since the max buffer size is 32KB, int16# is sufficient and avoids the overhead of boxed integers.
Example usage:
3. Parser Combinator Approach
httpz implements a parser combinator library where position is threaded explicitly:4. Request Parsing Flow
The complete request parsing follows this sequence:- Validate buffer size against limits
- Parse request line (method, target, version)
- Parse headers in a loop, accumulating to a local list
- Validate HTTP/1.1 requirements (Host header)
- Build request struct with all parsed data
5. Header Parsing State
During header parsing, httpz maintains state in an unboxed record:6. Local Lists for Headers
Headers are accumulated in a local list that lives on the stack:exclave_ annotation indicates the function returns stack-allocated values. Headers are prepended to the accumulator:
Security Features
httpz includes comprehensive RFC 7230 validation:Configurable Limits
HTTP Smuggling Prevention
Ambiguous Framing Detection
Chunked Transfer Encoding
httpz supports chunked encoding with overflow protection:Performance Characteristics
- Parsing throughput: 6.5M requests/sec
- Minimal request (35B): 300ns, 0 heap allocations
- Simple request (4 headers): 925ns, 0 heap allocations
- Browser request (10 headers): 3.3μs, 0 heap allocations
- 50 headers: 11.2μs, 0 heap allocations