3 ms·
I haven't done more than skim the code, but is this basically unrolled trampolining via exceptions?
by phyzome 9y ago
I haven't done more than skim the code, but is this basically unrolled trampolining via exceptions?
- teraflop 9y agoIt's a little more devious than that. The key is that the decorator maintains a stack of call parameters, and a map from parameters to results. When a recursive call happens, it aborts the top-level call, runs the recursive call directly, memoizes the result, and then re-executes the original call. Hence the warning about this only being usable for pure functions. EDIT: also I just noticed that the map keys aren't the parameters themselves, but their string representations. So heaven help you if you try this with a data type whose str() method isn't one-to-one.
- xapata 9y agoBetter to use the method demonstrated in functools' lru_cache: https://github.com/python/cpython/blob/3.6/Lib/functools.py#L421 https://github.com/python/cpython/blob/3.6/Lib/functools.py#...