hn.today

Eight Bytes Are a Number

blog.sebastiansastre.co6 points2 comments
Screenshot of Eight Bytes Are a Number

A practical guide to zero-copy parsing in Rust shows how treating bytes in-place as already-formed numbers avoids allocations and vastly improves throughput for binary protocols. A command enum (EngineCommand) is designed so every variant is composed of Copy integer types and has a fixed-size layout: for example NewLimit is 57 bytes, CancelByOrder 16 bytes. The decoder checks the envelope kind and length, then reads fields from known offsets (read_u64, read_i64, read_u128, decode_side, etc.) to construct variants without owning buffers. The same fixed-offset approach is used for encoding, writing directly into a caller-owned payload region so neither parsing nor serialization requires intermediate heap allocations.

Concrete measurements back the claim: per-command decode times around 4.1 ns and throughput near 237-245 Melem/s. A verification using two small Rust programs and strace contrasts parse_id (which copies bytes into a Vec and reserves memory) with zero_copy_parse_id (which uses from_le_bytes on the slice). The copying version triggers extra brk/mmap/munmap activity (including a 1,052,672-byte mmap for the reserved Vec) while the zero-copy variant avoids that work, demonstrating that “eight bytes are already a number” and that preserving non-owning, Copy types yields dramatic syscall and allocation savings.

Read on blog.sebastiansastre.co2 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.