Cache property lookups for the object parsing

This commit is contained in:
2016-04-27 11:16:54 -04:00
parent 9730600164
commit 790930dd66
10 changed files with 185 additions and 58 deletions
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Concurrent;
namespace LeafWeb.Core.Utility
{
public static class Memoizer
{
public static Func<A, R> ThreadsafeMemoize<A, R>(Func<A, R> f)
{
var cache = new ConcurrentDictionary<A, R>();
return argument => cache.GetOrAdd(argument, f);
}
public static Func<A1, A2, R> ThreadsafeMemoize<A1, A2, R>(Func<A1, A2, R> f)
{
var cache = new ConcurrentDictionary<Tuple<A1, A2>, R>();
return (a1, a2) => cache.GetOrAdd(Tuple.Create(a1, a2), t => f(t.Item1, t.Item2));
}
}
}