From d4c7dc3a834edc507865816a59960c5b31f9b10c Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 7 Apr 2016 09:25:32 -0700 Subject: [PATCH] Make HashSets in TempData lazy TempData is frequently created but not modified, this reduces the cost of that without much complexity. --- .../ViewFeatures/TempDataDictionary.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs index 37b201a25b..38ea9bae88 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs @@ -11,12 +11,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures /// public class TempDataDictionary : ITempDataDictionary { + // Perf: Everything here is lazy because the TempDataDictionary is frequently created and passed around + // without being manipulated. private Dictionary _data; private bool _loaded; private readonly ITempDataProvider _provider; private readonly HttpContext _context; - private HashSet _initialKeys = new HashSet(StringComparer.OrdinalIgnoreCase); - private HashSet _retainedKeys = new HashSet(StringComparer.OrdinalIgnoreCase); + private HashSet _initialKeys; + private HashSet _retainedKeys; /// /// Initializes a new instance of the class. @@ -132,7 +134,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures ? new Dictionary(providerDictionary, StringComparer.OrdinalIgnoreCase) : new Dictionary(StringComparer.OrdinalIgnoreCase); _initialKeys = new HashSet(_data.Keys, StringComparer.OrdinalIgnoreCase); - _retainedKeys.Clear(); + _retainedKeys = new HashSet(StringComparer.OrdinalIgnoreCase); _loaded = true; }