1

I want to design a hash table library that keeps usage statistics and based on how it is used will use different implementations at runtime. For example use a certain implementation for small size hash tables and a different one for large ones (or tweak parameters to maximize cache hits).

For example let's say I have a SmallDataHashTable, optimized for small size tables, BigDataHashTable optimized for large ones, LookUpHashTable optimized for lookup, and InsertHashTable optimized for insertion deletion and so on. Instead of leaving it to the user (developer) to decide which one works best for their use case, I want to create a BestHashTable that adapts to the best implementation based on how it is being used. For example if the user is using mostly large tables with lots of insertions, then a certain implementation is used under the hood.

To summarize let's say I keep usage statistics for n parameters. I want to select/tune implementation based on these statistics:

1- Can such design even in theory be the "best" general use hash table?
2- How do I go about designing such data structure? (I will be implementing it in C++ if that matters)

11
  • (From someone's comment a few days ago: tylerayoung.com/2019/01/29/…) It does not require reflection at all. Reflection does not help. Also, C++ is not a JIT language, so usage statistics don't help much.
    – rwong
    Commented Mar 24, 2019 at 6:46
  • This usage relies heavily on what conditions make a hash table small/large. Also, why couldn't you use abstract classes? You could have a "base" (we can call it something like UsageBase), then you could make separate implementations for small and large. Commented Mar 24, 2019 at 6:52
  • @eparham7861 I don't want the user to decide which subclass to use. I want the hash table to automatically use specific sub-class based on how the hash table is being used.
    – pooya13
    Commented Mar 24, 2019 at 7:59
  • @rwong thanks! I took out reflection. I'm not sure what keywords would describe what I have in mind haha. I guess I am looking for a term for describing code that is able to use different runtime behaviour based on usage history.
    – pooya13
    Commented Mar 24, 2019 at 8:04
  • 1
    It is common practice (and not really difficult) to have an implementation that (automatically) increases the number of hash buckets at some level of collisions / density.
    – Erik Eidt
    Commented Mar 24, 2019 at 14:00

1 Answer 1

4

You can have a proxy class that provides the same methods as a hash table and initially delegates them to an instance of the implementation optimized for small sizes.

Once the hashtable grows over a certain size, the proxy creates an instance of the large implementation, moves all the entries to this instance and makes it the default. A question than arrises: will you revert to using the smaller instance if a lot of entries are deleted?

1
  • Thank you for your help. I have reworded my question to fit the rules. Would you like to revise your answer?
    – pooya13
    Commented Mar 24, 2019 at 17:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.