Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
struct Object {
Tag tag; // The tag is any piece of data that uniquely identifies the object.
// ...
struct Handle {
Tag tag;
Index index; // This caches a speculative table index for an object with the corresponding tag.
Object *deref(Handle *h) {
Index i = h->index; // This doesn't have to be atomic because we will validate whatever we read.
if (i < num_objects) {
Object *o = objects + i;
if (o->tag == h->tag) return o; // Fast: Well-predicted path, object tag load is only a control dependency, acts as prefetch.
i = search(h->tag); // Slow: One or more branch mispredicts, must perform hash table or linear tag search.
h->index = i; // As with the read, this write also doesn't have to be atomic.
// Next time this handle gets dereferenced, it will hit the fast path unless the object has since been relocated.
return objects + i;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment