avsm/httpz

Zero heap allocation HTTP server using OxCaml. v experimental!

OCaml

125

39 commits

updated Jan 25, 2026

See the code

README

httpz - Zero-Allocation HTTP/1.1 Parser for OxCaml

A high-performance HTTP/1.1 parser and serializer achieving zero heap allocations using OxCaml's unboxed types (int16#, int64#, char#) and local allocations.

Will soon have io_uring on Linux.

Features

  • Zero heap allocations: Parser results are stack-allocated using OxCaml unboxed records and local lists
  • Unboxed integers throughout: Uses int16# for offsets/counts and int64# for content lengths - no boxing overhead
  • Direct bigstring I/O: Read and write directly to/from bigarray buffers
  • HTTP/1.1 support: Methods, headers, chunked transfer encoding, keep-alive, range requests, ETags
  • Async file server included: Production-ready static file server

Architecture

httpz achieves zero-allocation parsing through:

  1. Unboxed records (#{...}): Request, span, and header types are stack-allocated
  2. Unboxed primitives: int16# for buffer offsets, int64# for content lengths, char# for byte comparisons
  3. Local lists (@ local): Header list grows on the stack, not heap
  4. Span-based parsing: Strings are referenced by offset+length into the input buffer
  5. Pre-allocated buffers: 32KB read buffer reused across requests

Performance

Benchmarks comparing httpz (OxCaml) vs httpe (Eio-based parser):

Request Sizehttpz (ns/op)httpe (ns/op)SpeedupAllocation Reduction
Small (35B)1541591.03x45x fewer words
Medium (439B)1,1501,2181.06x399x fewer words
Large (1155B)2,7622,9121.05x823x fewer words

Throughput: 6.5M requests/sec

Detailed Timings

OperationTimeHeap Allocations
Parse minimal request (35B)300ns0 words
Parse simple request (4 headers)925ns0 words
Parse browser request (10 headers)3.3μs0 words
Parse 50 headers11.2μs0 words
Write status line76ns0 words
Write full response headers454ns0 words

True zero-allocation parsing - all values are stack-allocated via unboxed records and threaded position.

Installation

Requires OxCaml compiler from https://oxcaml.org/

Static File Server

An Async-based static file server is included:

# Serve current directory on port 8080
dune exec bin/httpz_server.exe

# Serve specific directory on custom port
dune exec bin/httpz_server.exe -- -d /var/www -p 3000

# Get help
dune exec bin/httpz_server.exe -- -help

Features:

  • Async concurrent connection handling (up to 10,000 connections)
  • Zero-copy bigstring I/O
  • MIME type detection
  • Directory traversal protection
  • Automatic index.html for directories

Running Benchmarks

# Comparative benchmark (httpz vs httpe)
dune exec bench/bench_compare.exe

# Detailed httpz benchmarks with core_bench
dune exec bench/bench_httpz.exe -- -quota 2

License

ISC

Contributors

avsm

39 commits

avsm/httpz

Zero heap allocation HTTP server using OxCaml. v experimental!

OCaml

125

39 commits

updated Jan 25, 2026

See the code

README

httpz - Zero-Allocation HTTP/1.1 Parser for OxCaml

A high-performance HTTP/1.1 parser and serializer achieving zero heap allocations using OxCaml's unboxed types (int16#, int64#, char#) and local allocations.

Will soon have io_uring on Linux.

Features

  • Zero heap allocations: Parser results are stack-allocated using OxCaml unboxed records and local lists
  • Unboxed integers throughout: Uses int16# for offsets/counts and int64# for content lengths - no boxing overhead
  • Direct bigstring I/O: Read and write directly to/from bigarray buffers
  • HTTP/1.1 support: Methods, headers, chunked transfer encoding, keep-alive, range requests, ETags
  • Async file server included: Production-ready static file server

Architecture

httpz achieves zero-allocation parsing through:

  1. Unboxed records (#{...}): Request, span, and header types are stack-allocated
  2. Unboxed primitives: int16# for buffer offsets, int64# for content lengths, char# for byte comparisons
  3. Local lists (@ local): Header list grows on the stack, not heap
  4. Span-based parsing: Strings are referenced by offset+length into the input buffer
  5. Pre-allocated buffers: 32KB read buffer reused across requests

Performance

Benchmarks comparing httpz (OxCaml) vs httpe (Eio-based parser):

Request Sizehttpz (ns/op)httpe (ns/op)SpeedupAllocation Reduction
Small (35B)1541591.03x45x fewer words
Medium (439B)1,1501,2181.06x399x fewer words
Large (1155B)2,7622,9121.05x823x fewer words

Throughput: 6.5M requests/sec

Detailed Timings

OperationTimeHeap Allocations
Parse minimal request (35B)300ns0 words
Parse simple request (4 headers)925ns0 words
Parse browser request (10 headers)3.3μs0 words
Parse 50 headers11.2μs0 words
Write status line76ns0 words
Write full response headers454ns0 words

True zero-allocation parsing - all values are stack-allocated via unboxed records and threaded position.

Installation

Requires OxCaml compiler from https://oxcaml.org/

Static File Server

An Async-based static file server is included:

# Serve current directory on port 8080
dune exec bin/httpz_server.exe

# Serve specific directory on custom port
dune exec bin/httpz_server.exe -- -d /var/www -p 3000

# Get help
dune exec bin/httpz_server.exe -- -help

Features:

  • Async concurrent connection handling (up to 10,000 connections)
  • Zero-copy bigstring I/O
  • MIME type detection
  • Directory traversal protection
  • Automatic index.html for directories

Running Benchmarks

# Comparative benchmark (httpz vs httpe)
dune exec bench/bench_compare.exe

# Detailed httpz benchmarks with core_bench
dune exec bench/bench_httpz.exe -- -quota 2

License

ISC

Contributors

avsm

39 commits

Languages

OCaml

99.6%