lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Yes! Here is another half-backed one for your entertainment! Hurray!

I use general memoizers (those that work on functions with 0-M arguments returning 0-N values) fairly frequently.  A string serialization approach can certainly work (though table and function argument handling gets a bit dicy).  But in the profiling tests I've done, an implementation that stores the arguments in a tree of tables is often significantly faster.  There's at least one such implementation already linked from the lua users wiki:

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

The argument tree approach can also be expanded to handle nil argument or nil return values; though doing so requires a small C-api function.

  static int count_args(lua_State * L) {
    lua_pushnumber(L,lua_gettop(L));
    return 1;
  }

The general memoizers I use in my own projects are written almost entirely in the C-API.  But, if you have count_args, simpler, prettier Lua implementations are possible.  Here's a demo of a count_args based M-to-N memoizer:

  http://lua-users.org/wiki/CurriedMemoization

-Sven