Reserve (Memory) First
3 days ago
- #memory management
- #exception safety
- #Zig programming
- The article discusses a coding pattern relevant for manual memory management, focusing on 'exception safety' issues.
- Two bugs are highlighted: one from Andrew Kelley's talk and another from Ghostty terminal emulator, both related to uninitialized memory access.
- Three levels of exception safety are defined: Strong, Basic, and No Exception Safety.
- The proposed solution is a 'reserve first' pattern: reserve memory before making changes to avoid errors during mutation.
- The article includes 'Spicy Takes' suggesting Zig should modify its append methods and consider aborting on Out of Memory (OOM) errors.
- TigerBeetle is mentioned as an example of reserving all resources upfront and avoiding allocations post-initialization.