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