I'm trying to create a general memoizator for multiple and arbitrary functions.
For each function std::function<ReturnType(Args...)>
that we want to memoize, we unordered_map<Args ..., ReturnType>
(I'm keeping things simple on purpose).
The big problem comes when our memoized function has some really big argument Args ...
: for example let suppose that our function sort a vector of 10 millions numbers and then returns the sorted vector, so something like std::function(vector>.
As you can imagine, after having inserted less than 100 vectors, we have already filled 8 GBS of memory. Notice that maybe this is given from the combination of huge vectors and the memory required by the sorting algorithm (I didn't investigate on the causes).
So what about if instead of the structure described above, we define unordered_map<UUID(Args ...), ReturnType>
(where UUID= Universally Unique Identifier)? We should relax the deterministic feature (so maybe we return a wrong error), but with a very low probability.
The problem is that since I never used UUIDs, I don't know if there are suitable implementations for this application.
So my question is:
- There exists a better solution than UUIDs for this problem?
- Which UUID implementation is better suitable for this problem?
- boost uuid is a possible candidate?
- Unfortunately, the problem could be solved for
Args ...
but not forReturnType
, so there is a solution for memoized result?
Notice that the UUIDs generated for the object x
should be the same even in different runs and machines.
I know that this application doesn't make sense in a memoization context (what are the odds that an user asks two times to sort the same 10 millions elements vector?), but it's time and memory expensive (so good for benchmarking and to introduce the memory problem that I stated above), so please don't whip and crucify me because this is an absurd memoization application.
Aucun commentaire:
Enregistrer un commentaire