Skip to content

Commit

Permalink
Merge pull request #2901 from rainers/issue14025
Browse files Browse the repository at this point in the history
fix Issue 14025 - unittests for memoize fail intermittently
  • Loading branch information
andralex committed Jan 24, 2015
2 parents f07a77d + e661e59 commit a5b1f5b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions std/functional.d
Expand Up @@ -1013,22 +1013,29 @@ template memoize(alias fun, uint maxSize)
initialized = (cast(size_t*)GC.calloc(nwords * size_t.sizeof, attr | GC.BlkAttr.NO_SCAN))[0 .. nwords];
}

import core.bitop : bts;
import core.bitop : bt, bts;
import std.conv : emplace;

size_t hash;
foreach (ref arg; args)
hash = hashOf(arg, hash);
// cuckoo hashing
immutable idx1 = hash % maxSize;
if (!bts(initialized.ptr, idx1))
return emplace(&memo[idx1], args, fun(args)).res;
if (!bt(initialized.ptr, idx1))
{
emplace(&memo[idx1], args, fun(args));
bts(initialized.ptr, idx1); // only set to initialized after setting args and value (bugzilla 14025)
return memo[idx1].res;
}
else if (memo[idx1].args == args)
return memo[idx1].res;
// FNV prime
immutable idx2 = (hash * 16777619) % maxSize;
if (!bts(initialized.ptr, idx2))
if (!bt(initialized.ptr, idx2))
{
emplace(&memo[idx2], memo[idx1]);
bts(initialized.ptr, idx2); // only set to initialized after setting args and value (bugzilla 14025)
}
else if (memo[idx2].args == args)
return memo[idx2].res;
else if (idx1 != idx2)
Expand Down

0 comments on commit a5b1f5b

Please sign in to comment.