Ideal hash generator for functions

I have a set of C ++ functions. I want to display these functions in a hash table, for example: unordered_map<function<ReturnType (Args...)> , SomethingElse> , where SomethingElse not relevant to this issue.

This set of functions is previously known, small (albeit less than 50) and static (not going to change).

Since search performance is critical (should be done in O(1) ), I want to define an ideal hash function.

Is there an ideal hash function generator for this scenario?

I know that there are perfect hash function generators (like GPERF or CMPH ), but since I never used them, I don’t know if they are suitable for my case.

CAUSE:

I am trying to create a structure where, given a program written in C ++, the user can select a subset of the F functions defined in this program.

For each F belonging to F , the structure implements a memoization strategy: when we call F with input i , we store (i,o) inside some data structure. So, if we call AGAIN F with i , we will return o without again performing the (costly) calculation.

“Already calculated results” will be available to different users (possibly in the cloud), so if user u1 has already calculated o , user u2 will save computational time when calling F with i (using the same annotation earlier).

Obviously, we need to save a set of pairs (f,inputs_sets) (where inputs_sets is the already computed result set that I mentioned earlier) , which is the original question: how to do this ?

Thus, using the “listing trick” suggested in the comments in this scenario may be a solution, assuming that all users use the enumeration exactly the same , which can be a problem: suppose our program has f1 , f2 , f3 what if does u1 want to memoize only f1 and f2 (so F={f1,f2} ), and u2 wants to memoize only f3 (so F={f3} )? An overkill solution may be to list all the functions defined in the program, but this can lead to a huge waste of memory.

+6
source share
1 answer

Well, maybe not what you want to hear, but think about it: since you are talking about several functions, less than 50, the search for hashes should be insignificant even in a collision. Did you really profile and see that the search is crucial?

So, I advise you to focus your energy on something else, most likely, an ideal hash function will not bring any improved characteristics in your case.

I'm going to take another step and say that I think that for less than 50 elements, a flat map (good ol vector ) will have similar performance (or maybe even better because of the cache locality). But then again, measurements are needed.

+5
source

All Articles