Reserve First

Reserve First Aug 16, 2025 A short post about a coding pattern that is relevant for people who use the heap liberally and manage memory with their own hands. Let’s start with two bugs. The first one is from Andrew Kelley’s HYTRADBOI 2025 talk, “Programming Without Pointers”: pub fn internString( state: *State, gpa: Allocator, bytes: []const u8, ) !String { const gop = try state.string_table.getOrPutContextAdapted( gpa, @as([]const u8, bytes), @as(String.TableIndexAdapter, .{ .bytes = state.string_bytes.items, }), @as(String.TableContext, .{ .bytes = state.string_bytes.items, }), ); if (gop.found_existing) return gop.key_ptr.*; try state.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1); const new_off: String = …

Read more on Lobste.rs