2 ms·
I think that a pattern that doesn't get nowhere near enough attention is the handle [0] (as opposed to objects/pointers) pattern. What's a handle? Think of it
by adamnemecek 5y ago
I think that a pattern that doesn't get nowhere near enough attention is the handle [0] (as opposed to objects/pointers) pattern.
What's a handle? Think of it as a file descriptor. Your code doesn't store the object itself but some sort of index into some array.
I'm partial to the generational arena indices which solve the ABA problem [0] by having a handle that's composed of index and generation (both are uints). Index is the offset into an array. When you remove an element at offset nn, you put offset n on a free list and next time you insert an object, you return an index where the generation counter is incremented. If someone had a stale index (with an old generation counter) to the previously removed object, when they try to access it next time, they will get a null.
This really shines for data models where you have complex relationship. By having all data in a single centralized store and interacting with data using your indices, you can update relationships as needed
This post summarizes well why that is https://floooh.github.io/2018/06/17/handles-vs-pointers.html https://floooh.github.io/2018/06/17/handles-vs-pointers.html
[0] https://en.wikipedia.org/wiki/Handle_(computing) https://en.wikipedia.org/wiki/Handle_(computing)
[1] https://en.wikipedia.org/wiki/ABA_problem https://en.wikipedia.org/wiki/ABA_problem