A walkthrough of iteratively optimizing a ring buffer data structure for a connectivity monitoring system. Starting from a 12 KiB struct with nanosecond timestamps and separate sent/received fields, the author progressively reduces memory usage through tagged unions, reduced timestamp precision (100 microsecond units), bitfields, and careful field ordering to eliminate struct padding. The final result is a single-page (4 KiB) struct. The post also explores compiler-level micro-optimizations around bit access patterns, including flipping the meaning of a boolean flag to allow the compiler to elide a mask instruction. The author acknowledges the exercise was entirely pointless since the application isn't memory-constrained, but found it enjoyable.

5m read timeFrom invlpg.com
Post cover image
Table of contents
Premature Optimization is Fun SometimesAddendum 2025-06-21Addendum 2025-06-22
5.5K Impressions