base: Add new mechanism for function memoization #4543
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This supports global (thread local) as well as local memoization using a syntax that IMHO looks nice.
say you have a function
fib
that performs poorly because it lacks memoization:you can now memoize this as follows
there are two things required: first, the function has to inherit from
memoize
. Second, the body has to be wrapped into a call tokeep
with the key and a lambda to calculate the corresponding result, which would usually just wrap the original code.Apart from the speedup, the memoization shows up when you analyse the effects:
It might be desired not to keep memoized results forever. To do so, we have to instate a local instance of the
memoize.memoized
effect. This can be done as follows:Since memoization is used locally only, the effect does not show up when effects are analysed.
NOTE: Currently, memoizaion requires keys to be orderable. Would be great
to support hashable as well
NOTE: Memoization is currently not thread safe. In case we want memoization
to be used amoung threads, we will need a thread safe variant. However,
this should currently be usable in multiple threads as long as each
thread has its own instance of memoized. We just do not profit from
values memoized in another thread.