我正在GitHub上探索ASP.NET核心的来源,看看ASP.NET团队用来加速框架的那种技巧。我看到一些引起我兴趣的东西。在源代码中 服务提供者,在Dispose实现中,他们枚举一个字典,他们发表评论来表明一个表演技巧:
private readonly Dictionary<IService, object> _resolvedServices = new Dictionary<IService, object>();
// Code removed for brevity
public void Dispose()
{
// Code removed for brevity
// PERF: We've enumerating the dictionary so that we don't allocate to enumerate.
// .Values allocates a KeyCollection on the heap, enumerating the dictionary allocates
// a struct enumerator
foreach (var entry in _resolvedServices)
{
(entry.Value as IDisposable)?.Dispose();
}
_resolvedServices.Clear();
}
如果字典是这样列举的,有什么区别?
foreach (var entry in _resolvedServices.Values)
{
(entry as IDisposable)?.Dispose();
}
它有性能影响吗?或者是因为分配了一个 ValueCollection 会消耗更多的内存吗?