Skip keys array allocation

This commit is contained in:
Ben Adams 2019-04-11 11:42:32 +01:00 committed by Pranav K
parent 3265c54518
commit 614d6336fd
1 changed files with 3 additions and 10 deletions

View File

@ -146,22 +146,15 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
return; return;
} }
// Because it is not possible to delete while enumerating, a copy of the keys must be taken. // In .NET Core 3.0 a Dictionary can have items removed during enumeration
// Use the size of the dictionary as an upper bound to avoid creating more than one copy of the keys. // https://github.com/dotnet/coreclr/pull/18854
var removeCount = 0;
var keys = new string[_data.Count];
foreach (var entry in _data) foreach (var entry in _data)
{ {
if (!_initialKeys.Contains(entry.Key) && !_retainedKeys.Contains(entry.Key)) if (!_initialKeys.Contains(entry.Key) && !_retainedKeys.Contains(entry.Key))
{ {
keys[removeCount] = entry.Key; _data.Remove(entry.Key);
removeCount++;
} }
} }
for (var i = 0; i < removeCount; i++)
{
_data.Remove(keys[i]);
}
_provider.SaveTempData(_context, _data); _provider.SaveTempData(_context, _data);
} }