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;
}