Technology1 min read

Handles are the better pointers (2018)

By · Published by Everything Blog

In short

Centralized architectures such as rendering or physics engines take complete ownership of memory allocations by grouping similar items into tightly packed arrays. User-level code receives an index-handle instead of a direct memory reference, completely avoiding standard allocation routines. Unused bits inside these handles are cleverly repurposed to store per-slot generation counters that prevent collisions. When immediate data access is required, handles are briefly converted into short-lived pointers before central control resumes.

Key points

  • Confining direct memory manipulation to central systems significantly eases the debugging…: Confining direct memory manipulation to central systems significantly eases the debugging and performance optimization process.
  • The index-handle model delivers optimal results for applications that continually instant…: The index-handle model delivers optimal results for applications that continually instantiate and destroy hundreds to millions of objects.
  • Adopting this architecture requires developers to undertake substantial restructuring whe…: Adopting this architecture requires developers to undertake substantial restructuring when transitioning away from traditional object-oriented designs.

Sources checked by our analysis pipeline: 1

Handles are the better pointers

28-Nov-2018: I’ve added a small update at the end on how to prevent ‘handle collisions’ with a per-slot generation counter

Original Post:

…wherein I talk a bit about how I’m doing dynamic memory management in C and C++ these days which basically replaces raw- and smart-pointers with ‘index-handles’.

In my last blog post I was mentioning pointer- and allocation-free programming, but was skipping over the details. This is what the following blog post is about.

This is all based on the (sometimes painful) experience of wrestling for 15+ years with fairly big C++ code bases (0.5 to around 1 mloc) where memory is often managed through smart pointers. The worst case being tens- to hundreds-of-thousands of small C++ objects, each in its own heap allocation, pointing to each other through smart pointers. While such code is quite robust in terms of memory corruption (segfaults and corruption rarely happens, since most attempts are caught by asserts when dereferencing smart pointers), this type of ‘object spiderweb code’ is also dog-slow without obvious starting points for optimization, since…

Technology