kikito / memoize.lua

memoized functions in lua
https://github.com/kikito/memoize.lua
MIT License
100 stars 10 forks source link

faster cache_get #7

Open level99 opened 5 years ago

level99 commented 5 years ago

Here is a faster version of cache_get. It removes a hash lookup which can take a decent toll on performance under a cache hit scenario. The gain is most noticeable if there are a fair number of params (eg. 3+).

local function cache_get(cache, params)
  local node = cache
  for i=1, #params do
    node = node and node.children
    node = node and node[params[i]]
  end
  return node and node.results or nil
end
ghost commented 1 year ago

I may be wrong about this, as I only did cursory benchmarks, but fwiw, this made memoize far slower for me with single-arg functions. This may not hold for other cases, or my benchmarking may have been too simplistic, or I might have gotten something else wrong.