memoize.lua

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

memoize.lua

Post by kikito »

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.
User avatar
sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California

Re: memoize.lua

Post by sharpobject »

Cool, can you post it?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: memoize.lua

Post by kikito »

:cry:

I've edited my previous post and added the url for the lib. Thank you!
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: memoize.lua

Post by Taehl »

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+.
User avatar
sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California

Re: memoize.lua

Post by sharpobject »

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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: memoize.lua

Post by kikito »

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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: memoize.lua

Post by BlackBulletIV »

Looks great kikito! Don't need it right now, but as you say, it'll be there if I do.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: memoize.lua

Post by vrld »

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
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: memoize.lua

Post by ishkabible »

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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: memoize.lua

Post by BlackBulletIV »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests