## memoize.lua

kikito
Inner party member
Posts: 3152
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### memoize.lua

Hi there!

I have implemented a small utility function that some of you might find useful.

It transforms any regular function into a memoized one.

In Programming in Lua there's a simple implementation of function memoization, but it presents several problems:
• It relies on tostring to generate a "string key" for each cached result. This is bound to handle some inputs problematically.
• It doesn't handle functions returning multiple values
• That solution is tied to a particular problem. My implementation is generic.
A quick example that I hope you will all understand:

Code: Select all

local memoize = require 'memoize'

love.graphics.newFont = memoize(love.graphics.newFont)

font1 = love.graphics.newFont(13)
...
font2 = love.graphics.newFont(13) --> A-HA!

The second time love.graphics.newFont is called, it will "remember" that the font that was already created, and just return it; It will not create a new font.

In other words, this function can be used to create "cached versions" of regular functions; functions that that will "remember" the results of previous calls just by looking at the parameter list, and just return the values without performing any calculations.

There are lots of applications - in addition to the resource-loading functions shown above, it can be useful in procedural generation functions (remembering random values), recursive functions, or just plain slow ones.

You might not need this function today. Maybe not tomorrow. But some day, you will need to cache results. On that day, memoize.lua will be there for you.

It is available on this location:

https://github.com/kikito/memoize.lua

I shall now retire to my lair! Bwahahahaa!
Last edited by kikito on Wed Apr 20, 2011 11:18 pm, edited 2 times in total.
When I write def I mean function.

sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California
Contact:

### Re: memoize.lua

Cool, can you post it?

kikito
Inner party member
Posts: 3152
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: memoize.lua

I've edited my previous post and added the url for the lib. Thank you!
When I write def I mean function.

Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

### Re: memoize.lua

I don't need it yet, but it looks pretty sweet. Let me guess, it does fancy things with metatables?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.

sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California
Contact:

### Re: memoize.lua

Taehl wrote:I don't need it yet, but it looks pretty sweet. Let me guess, it does fancy things with metatables?
It looks like it just stores a private cache and uses a function decorator to check the cache. Then it returns the cached value or the return value of the function you gave it.

kikito
Inner party member
Posts: 3152
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: memoize.lua

sharpobject wrote:
Taehl wrote:I don't need it yet, but it looks pretty sweet. Let me guess, it does fancy things with metatables?
It looks like it just stores a private cache and uses a function decorator to check the cache. Then it returns the cached value or the return value of the function you gave it.
Sharpobject's right on the money. That's precisely what it does.
When I write def I mean function.

BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: memoize.lua

Looks great kikito! Don't need it right now, but as you say, it'll be there if I do.

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: memoize.lua

Very neat. I like that it handles multiple arguments.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

### Re: memoize.lua

do you store the return values in a table and unpack it to return it? that's the only way i can think to do it.

BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: memoize.lua

ishkabible wrote:do you store the return values in a table and unpack it to return it? that's the only way i can think to do it.
That's indeed the way it's done. See lines 69, 56, and 72.

### Who is online

Users browsing this forum: No registered users and 3 guests