今天我看到this question关于ConcurrentDictionary
方法的一些性能差异,我把它看作是一种过早的微观优化。但是,经过一番思考,我意识到(如果我没有弄错),每次我们将一个lambda传递给一个方法时,CLR都需要分配内存,传递适当的闭包(如果需要的话),然后收集过了一段时间。C#lambda分配和集合
有三种可能性:
LAMBDA没有关闭:
// the lambda should internally compile to a static method, // but will CLR instantiate a new ManagedDelegate wrapper or // something like that? return concurrent_dict.GetOrAdd(key, k => ValueFactory(k));
拉姆达与闭合:
// this is definitely an allocation return concurrent_dict.GetOrAdd(key, k => ValueFactory(k, stuff));
外检查(如检查前的状态锁):
// no lambdas in the hot path if (!concurrent_dict.TryGetValue(key, out value)) return concurrent_dict.GetOrAdd(key, k => ValueFactory(k));
第三种情况显然是免分配的,第二种情况需要分配。
但是第一种情况(没有捕获的lambda)完全没有分配(至少在更新的CLR版本中)?另外,这是运行时的实现细节,还是标准指定的东西?