Handles are the better pointers (2018) floooh.github.io · 16 points · 8 comments...

@best

Handles are the better pointers (2018) floooh.github.io · 16 points · 8 comments · HN thread

TL;DR Niels Lohmann's floooh blog post (2018) argues that in game and data-oriented code, centralized systems should own memory and expose 'index-handles' instead of raw or smart pointers. Handles carry extra unused bits as a unique tag to detect dangling access, and a 28-Nov-2018 update proposes per-slot generation counters to avoid tag collisions. The thread is small (8 comments) and mostly skeptical, citing indirection cost, memory overhead and resizing complexity.

  • Memory management moves into central systems (rendering, physics, animation) that are the sole owners of their allocations.
  • Items of one type are stored in arrays with a private base pointer; user code receives only an index-handle.
  • Handles use only the bits needed for the index; the remaining bits hold a unique tag checked on every handle-to-pointer conversion to catch use-after-free.
  • Pointers obtained from handles should not be stored or kept across function calls, since the item may be destroyed or the array reallocated.
  • The update replaces random tags with a per-slot generation counter, bumped on release; slots are disabled when the counter overflows, giving up to 2^32 items with 32-bit handles.
  • Named real-world examples include the sokol-gfx API and the Oryol Gfx and Animation modules.

Discussion The thread is short and mixed. Commenters connect the idea to older handle-based systems such as classic Mac OS and Win16 memory handles, and to Vulkan and Win32 handle types. Most of the substantive discussion is skeptical: one long reply argues that handles are a trade-off rather than 'better pointers', pointing to per-lookup indirection, larger handles, the need to compact or manage free lists, and the difficulty of lifetime management. A few replies endorse data-oriented layouts such as SoA and entity-component systems, and one points to the Jovial language's table design. The overall mood is analytical and cautious rather than enthusiastic.

  • Handles are a trade-off, not universally better than pointers (many): Commenters say handles suit bounded domains like games and rendering, where work starts and ends at a frame or scene, but are a poor fit for general object graphs. The title's claim is considered overstated.
  • Indirection and safety checks add cost on every access (some): Each lookup becomes an array index plus bounds, null and generation checks. One commenter estimates that a 300K-node AST would add about 9.6MB of handle overhead and noticeably more cache misses.
  • Compaction and lifetime management become a hand-written garbage collector (some): Resizing or compacting arrays requires rewriting every index, and managing per-type pools and free lists quickly recreates malloc, free and GC problems.
  • Handles have a long history in OS and platform APIs (some): Classic Mac OS and Win16 used handles so the system could move memory; Win32 and Vulkan handles are often void pointers wrapped in types, best managed with RAII.
  • Data layout matters more than the handle choice (a few): Commenters point to SoA layouts, entity-component systems and column-oriented databases as the real source of performance gains in heavy computation.

Links from the comments

#memorymanagement #dataOriented #gameDev #handles #cpp