Httpz – 零分配 HTTP/1.1 解析器,用于 OxCaml
Httpz – Zero-Allocation HTTP/1.1 Parser for OxCaml

原始链接: https://github.com/avsm/httpz

## httpz: 一个零分配的 HTTP/1.1 解析器 & 服务器 httpz 是一个高性能的 HTTP/1.1 解析器和序列化器,使用 OxCaml 构建,旨在实现**零堆分配**。它通过无装箱类型、为记录和列表等数据结构使用本地栈分配,以及直接 bigstring I/O 来实现这一点。 主要特性包括对完整 HTTP/1.1 的支持(方法、头部、分块编码、keep-alive),以及一个生产就绪、即将支持并行、异步静态文件服务器。基准测试表明,与基于 Eio 的解析器(如 `httpe`)相比,httpz 具有显著的速度提升和分配减少——对于小型请求,速度提升高达 3.14 倍,分配的字数减少 94 倍,并实现了 **14.6M 请求/秒 的吞吐量**。 httpz 利用基于 span 的解析和预分配的缓冲区来提高效率。它需要 OxCaml 编译器 ([https://oxcaml.org/](https://oxcaml.org/)),并提供命令行工具用于服务器操作和基准测试。

## Httpz:为 OCaml 设计的零分配 HTTP/1.1 解析器 一个名为 **Httpz** 的新型 HTTP/1.1 解析器,使用 OxCaml 扩展为 OCaml 构建,正在 Hacker News 上受到关注。其关键特性是 **零堆分配** – 解析结果存储在栈上,避免为每个请求产生垃圾回收开销。这是通过 OxCaml 的 ‘local’ 类型和解箱记录实现的。 作者正在为 Httpz 添加无数据竞争的并行性,以便在其自己的 Web 服务器中使用,该服务器还利用 io_uring 实现零拷贝缓冲区和效果处理程序以实现高效的纤程恢复。 讨论集中在 *为什么* 栈分配是有益的(提高速度和数据局部性等常数因子)以及大型头部可能导致的栈溢出等潜在缺点。开发者澄清说,当前实现具有无限制的栈增长,但计划将头部解析限制为应用程序请求的头部,从而降低风险。 OxCaml 允许开发者编写高性能的 OCaml 代码,最大限度地减少或消除垃圾回收,提供了一种反向的 Rust 式方法。
相关文章

原文

A high-performance HTTP/1.1 parser and serializer that aims for zero heap allocations using OxCaml's unboxed types and local allocations.

Will soon have io_uring on Linux.

  • Zero heap allocations: Parser results are stack-allocated using OxCaml unboxed records and local lists
  • Direct bigstring I/O: Read and write directly to/from bigarray buffers
  • HTTP/1.1 support: Methods, headers, chunked transfer encoding, keep-alive
  • Async file server included: Production-ready static file server. Soon to be parallel.

httpz achieves zero-allocation parsing through:

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

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

Request Size httpz (ns/op) httpe (ns/op) Speedup Allocation Reduction
Small (35B) 69 218 3.14x 94x fewer words
Medium (439B) 792 1,690 2.13x 399x fewer words
Large (1155B) 1,771 4,017 2.27x 829x fewer words

Throughput: 14.6M requests/sec (vs 4.6M for httpe)

Operation Time Heap Allocations
Parse minimal request 209ns 3 words
Parse browser request (10 headers) 2.8μs 3 words
Parse 50 headers 7.7μs 3 words
Write status line 21ns 3 words
Write full response headers 62ns 3 words

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

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
# 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

ISC

联系我们 contact @ memedata.com