Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How can you use memoized functions in Roblox Lua?

Asked by 6 years ago
Edited 6 years ago

I was really interested in this question Linked Arrays. According to that question, linked arrays make performance better. The asker linked what intrigued them to the topic. It was a Dev Forum post about Basics of Optimization by a user known as BlueTalsem. One of his ways of optimizing was memoizing expensive functions. Link here. I assumed that expensive functions are functions that make performance worse or take too much memory. I got interested. I found this git-hub page.

This is what I understood:

Memoized functions gets the results of a function. It's parameter is the function it gets the results of that argument. It has an optional argument which is its cashe. This is the place where the memoized function is stored. If not, it will default to its own table.

Memoized functions improve performance for memory. To get that memory back, you just simply make the memoized function nil.

When I was reading, I noticed this

01local sum = function(...)
02  local result = 0
03  local params = { ... }
04  for i = 1, #params do
05        result = result + params[i]
06  end
07    return result
08end
09 
10local memoized_sum = memoize(sum)
11 
12print(memoized_sum())     -- 0
13print(memoized_sum(1))    -- 1
14print(memoized_sum(1, 2)) -- 3
15print(memoized_sum(3, 1)) -- 4
16 
17memoized_sum = nil

I have added the memoized_sum = nil to retrieve the memory back

The git-hub page said that the internal cache structure would be like this:

01{
02  results  = { 0 },
03  children = {
04    [1] = {
05      results = { 1 },
06      children = {
07        [2] = {
08          results = { 3 },
09        }
10      }
11    }
12    [3] = {
13      children = {
14        [1] = {
15          results = { 4 },
16        }
17      }
18    }
19  }
20}

However, the problem is that there is no such function called memoize(). So, what can I do to make memoized functions and if I have misunderstood memoized functions or I still need to know more, please tell me by using answers

Thank you!

.

0
"memoize" is this if you looked at the variables on the top of spec/memoize_spec.lua. The module code is https://github.com/kikito/memoize.lua/blob/master/memoize.lua. (I don't have the time to make a proper answer.) hiimgoodpack 2009 — 6y
0
Okay, so, I should do a few edits and then I can use this as a module saSlol2436 716 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Memoization, at its simplest, is storing the results of an expensive function in a memory cache and returning the cached result when the same inputs occur again. If you wanted to make a memoize function, all you would need to do is pass the function, the parameter, and the result into a memory cache (hint: dictionary/table) and index that value when the same input is used)

01local memoryCache = {}
02 
03function memoize(func, ...)
04    local params = {...}
05    local index = {}
06    local funcCached = false
07    for i = 1, #memoryCache do -- finds the function
08        if memoryCache[i][1] = func then -- function
09            index = memoryCache[i]
10            funcCached = true
11            for k = 2, #index do -- finds the parameters
12                if index[k][1] = params then --parameters
13                    return index[k][2] --result
14                end
15            end
View all 35 lines...

This function basically takes in another function, checks if it has been cached already. If it is not cached, it adds the function to the memory cache.

Ad

Answer this question