Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster cache_get #7

Open
level99 opened this issue May 31, 2019 · 1 comment
Open

faster cache_get #7

level99 opened this issue May 31, 2019 · 1 comment

Comments

@level99
Copy link

level99 commented May 31, 2019

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
@SamuelSwartzberg
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants