Skip to content

Commit

Permalink
Add memoize to the build. Fix it so it builds, and at least part of i…
Browse files Browse the repository at this point in the history
…t runs. Add in a benchmark which shows the performance difference between a naive recursive fibonacci, and a version using a memoized Y combinator
  • Loading branch information
Whiteknight committed Apr 2, 2011
1 parent ca4183b commit 92ae143
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
36 changes: 36 additions & 0 deletions benchmarks/memoize/fib.winxed
@@ -0,0 +1,36 @@
function main[main]()
{
count_time("fib(30) unmemoized", function() {
say(sprintf("fib(30) = %f", [fib_standard(30)]));
});

count_time("fib(30) with memoization", function() {
load_bytecode("rosella/memoize.pbc");
using Rosella.Memoize.Y;
var answer = (Y(function(g) {
var _g = g;
return function(int n) {
if (n <= 1)
return n;
return _g(n - 1) + _g(n - 2);
};
}))(30);
say(sprintf("fib(30) = %f", [answer]));
});
}

function count_time(string description, var code)
{
say(sprintf("Starting %s", [description]));
float starttime = floattime();
code();
float endtime = floattime();
say(sprintf("Total time: %fs", [endtime - starttime]));
}

function fib_standard(int i)
{
if (i <= 1)
return i;
return fib_standard(i - 1) + fib_standard(i - 2);
}
8 changes: 8 additions & 0 deletions setup.winxed
Expand Up @@ -178,16 +178,24 @@ function setup_experimental_libraries(var rosella)

// A library for creating and using cheap, transparent decorator
setup_winxed_lib(rosella, "decorate",
"include/Core",
"include/Proxy",
"decorate/Builder",
"decorate/Factory"
);

setup_winxed_lib(rosella, "contract",
"include/Core",
"contract/Contract",
"contract/Method",
"contract/Interface"
);

setup_winxed_lib(rosella, "memoize",
"include/Core",
"memoize/Memoize",
"memoize/StringKeygen"
);
}

function setup_winxed_lib(var rosella, string name, var files [slurpy])
Expand Down
23 changes: 16 additions & 7 deletions src/memoize/Memoize.winxed
Expand Up @@ -21,9 +21,13 @@ namespace Rosella { namespace Memoize
var f = func;
var memfunc = function(var p [slurpy], var n [named,slurpy]) {
string key = generator.key(p, n);
if (exists cache[key])
say("memoizing key: " + key);
if (exists cache[key]) {
say("found in cache");
return cache[key];
}
var value = f(p:[flat], n:[flat,named]);
say("Adding key to cache: " + key);
cache[key] = value;
return value;
};
Expand Down Expand Up @@ -61,13 +65,18 @@ namespace Rosella { namespace Memoize

return function(var p [slurpy], var n [slurpy,named]) {
string key = _keygen.key(p, n);
if (exists _cache[key])
if (exists _cache[key]) {
return _cache[key];
var value = (F(function(var _p [slurpy], var _n [slurpy,named]) {
return (Y(F, _cache)(_p:[flat], _n:[flat,named]);
}))(arg);
_cache[key] = answer;
return answer;
}
var value = (
F(
function(var _p [slurpy], var _n [slurpy,named]) {
return (Y(F, _cache))(_p:[flat], _n:[flat,named]);
}
)
)(p:[flat], n:[flat,named]);
_cache[key] = value;
return value;
};
}
}}
4 changes: 2 additions & 2 deletions src/memoize/StringKeygen.winxed
Expand Up @@ -4,11 +4,11 @@ namespace Rosella { namespace Memoize
{
function key(var p, var n)
{
var sb = new "StringBuilder"
var sb = new "StringBuilder";
for (string ip in p)
${ push sb, ip };
for (string key in n) {
${ push sp, key };
${ push sb, key };
string val = n[key];
${ push sb, key };
}
Expand Down

0 comments on commit 92ae143

Please sign in to comment.