Lesson 9 of 9 · 25m
Performance, Packaging, and Shipping Binaries
Measure before optimizing, choose release modes deliberately, package artifacts with provenance, and ship utilities users can trust and operate.
The last mile of a systems utility is where craftsmanship becomes visible. The binary starts quickly, uses memory predictably, reports errors cleanly, does not surprise the filesystem, and can be traced back to source. None of that happens because the compiler is fast. It happens because you measured, chose release settings deliberately, and treated packaging as part of the product.
Measure the thing users feel
Do not start with cache-line trivia. Measure startup time, steady-state throughput, peak memory, allocation count, and binary size on representative input:
/usr/bin/time -v zig-out/bin/spray --format json ./src > /tmp/out.json
hyperfine './spray ./src' './spray --jobs 8 ./src'On Linux, time -v gives maximum resident set size and page faults. hyperfine reduces benchmark noise. If those tools are unavailable, repeated shell timing is still better than intuition.
Allocation is often the first bottleneck
A parser allocating once per token is slow before it is clever. Inspect whether allocations can be reused, bounded, or phase-based:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();For batch processing, an arena can eliminate thousands of tiny frees. For streaming, a reusable buffer may be better. Do not guess: count allocations before and after. Also remember that fewer allocations can increase peak memory if you retain everything until the end.
Understand release modes as policies
Optimization mode is not just speed:
Debug best diagnostics, safety checks, slow
ReleaseSafe optimized but keeps safety checks
ReleaseFast optimized aggressively, fewer checks
ReleaseSmall size-oriented optimization, fewer checksFor security-sensitive tools, ReleaseSafe is a strong default. For embedded distribution, ReleaseSmall may win. For compute-bound trusted-input workloads, ReleaseFast may be justified. The correct answer is a measured policy, not a default copied from someone else's README.
Binary size is not vanity
Size affects download time, container image size, memory pressure in small environments, and update bandwidth. Inspect composition instead of shaving random bytes:
ls -lh zig-out/bin/spray
strip zig-out/bin/spray -o /tmp/spray-strippedStripping symbols makes operational debugging harder. A practical compromise is shipping stripped binaries while retaining unstripped artifacts and exact provenance internally.
Package for the target reality
A release matrix might look like:
spray-x86_64-linux-musl.tar.gz
spray-aarch64-linux-musl.tar.gz
spray-x86_64-windows-gnu.zip
spray-aarch64-macos.tar.gzEach archive should contain the binary, license, README or usage text, and checksum manifest. Record:
version
commit hash
Zig compiler version
target triple
optimization mode
dependency versions or lockfile hashWhen a bug report says "the Linux binary crashes," this is the difference between reproduction and guessing.
Operational behavior is a feature
Users need --version, --help, predictable exit codes, and output that can be scripted. Package managers need deterministic archives. Incident responders need enough information to identify whether they are running a vulnerable build. These are not enterprise embellishments; they are basic respect for the person running your binary.
Field notes
- Never benchmark on tiny synthetic input and then generalize. Real filesystems dominate many CLI tools more than arithmetic does.
- Reusing memory is only safe if ownership and aliasing remain clear. A faster use-after-free is still a use-after-free.
- ReleaseFast removing safety checks does not make undefined behavior impossible; it makes it less visible.
- Static linking simplifies deployment but does not remove the need for license compliance or dependency tracking.
- A compressed archive is not supply-chain security. At minimum publish checksums; ideally sign releases and keep build instructions reproducible.
- If tests only run in Debug, you do not know the behavior of the artifact you ship. Run smoke tests against release-mode binaries too.
Was this lesson helpful?