Measure a binary parser
Quick answer
A useful performance project compares two implementations of the same parser while preserving the exact acceptance rules. Start with correctness, then measure throughput and allocation behavior.
Define the format and the experiment
Here a record starts with one length byte followed by exactly that many payload bytes. Empty payloads are valid; missing headers, short payloads and trailing bytes are rejected. Compare an owned Vec result with a borrowed slice result. Benchmark valid and invalid inputs separately, use equivalent input batches, and record input bytes per second as well as latency distributions. The examples demonstrate semantics only and supply no measured speedup.
fn parse(input: &[u8]) -> Option<&[u8]> { let (&n, body) = input.split_first()?; (usize::from(n) == body.len()).then_some(body)}fn main() { println!("{:?} {:?}", parse(&[2, 7, 8]), parse(&[2, 7])); }Expected stdout (not executed):
Some([7, 8]) NoneThe borrowed parser rejects truncated records without copying the payload.
fn borrowed(input: &[u8]) -> Option<&[u8]> { let (&n, body) = input.split_first()?; (usize::from(n) == body.len()).then_some(body)}fn owned(input: &[u8]) -> Option<Vec<u8>> { borrowed(input).map(|b| b.to_vec()) }fn main() { let cases: &[&[u8]] = &[&[], &[0], &[1, 9], &[0, 9], &[2, 9]]; for x in cases { assert_eq!(borrowed(x), owned(x).as_deref()); } println!("{} cases agree", cases.len());}Expected stdout (not executed):
5 cases agreeThe owned version copies accepted payloads while sharing validation.
Common mistake
Do not compare a fast parser that skips validation against a slower correct parser.
Try it yourself
Build a fixed corpus of 0-, 1- and 255-byte payloads plus malformed records; verify equivalence, then record repeated release timings with both outputs consumed.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Measure a binary parser — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
The toy parser receives a single zero length byte and no payload. What does its contract require?
Measure a binary parser — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn parse(input: &[u8]) -> Option<&[u8]> { let (&n, body) = input.split_first()?; (usize::from(n) == body.len()).then_some(body) } fn main() { println!("{:?} {:?}", parse(&[2, 7, 8]), parse(&[2, 7])); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Measure a binary parser — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn borrowed(input: &[u8]) -> Option<&[u8]> { let (&n, body) = input.split_first()?; (usize::from(n) == body.len()).then_some(body) } fn owned(input: &[u8]) -> Option<Vec<u8>> { borrowed(input).map(|b| b.to_vec()) } fn main() { let cases: &[&[u8]] = &[&[], &[0], &[1, 9], &[0, 9], &[2, 9]]; for x in cases { assert_eq!(borrowed(x), owned(x).as_deref()); } println!("{} cases agree", cases.len()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.