Hasty Briefsbeta

Bilingual

Premature Optimization Is Fun Sometimes

2 days ago
  • #data structures
  • #C programming
  • #memory optimization
  • The article discusses optimizing memory usage for a connectivity monitoring system storing ICMP echo request data.
  • Initial struct uses 12 KiB; improvements via tagged union reduce size to 8 KiB by storing latency instead of separate sent and received timestamps.
  • Further optimizations include using 43-bit timestamps in 100μs units (covering 20 years) and bitfields to compact data.
  • Struct padding issues prevent initial bitfield savings, leading to a redesigned struct using a 4-bit counter instead of source address.
  • Final struct reduces memory to 4 KiB, with field ordering optimized for CPU instructions and bit manipulation efficiency.
  • The author notes the exercise was unnecessary for their memory-unconstrained application but enjoyable for optimization insights.