Make HashSets in TempData lazy

TempData is frequently created but not modified, this reduces the cost of
that without much complexity.
This commit is contained in:
Ryan Nowak 2016-04-07 09:25:32 -07:00
parent cf460e9a63
commit d4c7dc3a83
1 changed files with 5 additions and 3 deletions

View File

@ -11,12 +11,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// <inheritdoc /> /// <inheritdoc />
public class TempDataDictionary : ITempDataDictionary public class TempDataDictionary : ITempDataDictionary
{ {
// Perf: Everything here is lazy because the TempDataDictionary is frequently created and passed around
// without being manipulated.
private Dictionary<string, object> _data; private Dictionary<string, object> _data;
private bool _loaded; private bool _loaded;
private readonly ITempDataProvider _provider; private readonly ITempDataProvider _provider;
private readonly HttpContext _context; private readonly HttpContext _context;
private HashSet<string> _initialKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase); private HashSet<string> _initialKeys;
private HashSet<string> _retainedKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase); private HashSet<string> _retainedKeys;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TempDataDictionary"/> class. /// Initializes a new instance of the <see cref="TempDataDictionary"/> class.
@ -132,7 +134,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
? new Dictionary<string, object>(providerDictionary, StringComparer.OrdinalIgnoreCase) ? new Dictionary<string, object>(providerDictionary, StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); : new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_initialKeys = new HashSet<string>(_data.Keys, StringComparer.OrdinalIgnoreCase); _initialKeys = new HashSet<string>(_data.Keys, StringComparer.OrdinalIgnoreCase);
_retainedKeys.Clear(); _retainedKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_loaded = true; _loaded = true;
} }