Let's say I have an associative array keyed by unsigned int
; values could be of any fixed-size type. There is some pre-defined maximum no. of instances.
API usage example: MyStruct * valuePtr = get(1234);
and put(6789, &myStructInstance);
...basic.
I want to minimise cache misses as I read entries rapidly and at random from this array, so I pre-malloc(sizeof(MyType) * MAX_ENTRIES)
to ensure locality of reference inasmuch as possible.
Genericism is important for the values array. I've looked at C pseudo-generics, but prefer void *
for simplicity; however, not sure if this is at odds with performance goals. Ultimately, I would like to know what is best for performance.
How should I implement my associative array for performance? Thoughts thus far...
- Do I pass the associative array a single
void *
pointer to themalloc
ed values array and allow it to use that internally (for which we would need to guarantee a matching keys array size)? Can I do this generically, since the type needs(?) to be known in order to index into values array? - Do I have a separate
void * valuePtrs[]
within the associative array, then have these pointers point to each element in themalloc
ed values array? This would seem to avoid need to know about concrete type? - Do I use C pseudo-generics and thus allow
get()
to return a specific value type? Surely in this case, the only benefit is not having to explicitly cast, e.g.MyStruct* value = (MyStruct*) get(...)
... the array element still has to be dereferenced and so has the same overhead?
And, in general, does the above approach to minimising cahce misses appear to make sense?