hn.today

Handles are the better pointers (2018)

floooh.github.io48 points22 comments
Screenshot of Handles are the better pointers (2018)

The approach replaces raw and smart pointers as public object references with typed index-handles, especially in programs that create and destroy many items. It responds to large C++ systems where separately allocated objects form a web of references: smart pointers may catch some invalid accesses, but the resulting code can be slow from cache misses, fragmented in memory, and hard to diagnose when lingering references prevent cleanup. The central recommendation is to put allocation and lifetime management inside the systems that use the data - such as rendering, physics, or animation - rather than spreading it across application code. These systems have the domain knowledge to validate lifetimes, set useful limits, and arrange data for efficient processing.

Each system stores items of a given type in arrays or pools and gives callers an index-handle instead of an address. This reduces allocations, enables compact or cache-friendly layouts, and lets the system relocate storage without invalidating handles. When direct access is necessary, a lookup checks that the index is in range and its slot is occupied, then returns a temporary pointer; callers should neither store that pointer nor keep it across function calls. A slot’s occupancy alone cannot detect a stale handle after the slot has been reused for another item. The proposed safeguard is to include a per-slot generation counter in the handle and verify it during lookup, reducing accidental handle collisions and strengthening use-after-free detection. The design fits data-oriented architectures better than decentralized object-oriented code.

Read on floooh.github.io22 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Tangled in a Box

Tangled in a Box

Tangled in a Box packages Tangled’s Git collaboration stack for local or private-network use, launched with Docker Compose. It’s a proof of concept with hardcoded secrets that should be replaced before any real deployment. (jola.dev)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.