Lazy allocate dictionary in ItemsDictionary (#9284)

This commit is contained in:
Ben Adams 2019-04-14 16:09:17 +01:00 committed by David Fowler
parent 5889aea06c
commit dc08a60f5e
2 changed files with 87 additions and 43 deletions

View File

@ -406,7 +406,7 @@ namespace Microsoft.AspNetCore.Http.Internal
{ {
public ItemsDictionary() { } public ItemsDictionary() { }
public ItemsDictionary(System.Collections.Generic.IDictionary<object, object> items) { } public ItemsDictionary(System.Collections.Generic.IDictionary<object, object> items) { }
public System.Collections.Generic.IDictionary<object, object> Items { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public System.Collections.Generic.IDictionary<object, object> Items { get { throw null; } }
int System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.Object,System.Object>>.Count { get { throw null; } } int System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.Object,System.Object>>.Count { get { throw null; } }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.Object,System.Object>>.IsReadOnly { get { throw null; } } bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.Object,System.Object>>.IsReadOnly { get { throw null; } }
object System.Collections.Generic.IDictionary<System.Object,System.Object>.this[object key] { get { throw null; } set { } } object System.Collections.Generic.IDictionary<System.Object,System.Object>.this[object key] { get { throw null; } set { } }

View File

@ -8,111 +8,155 @@ namespace Microsoft.AspNetCore.Http.Internal
{ {
public class ItemsDictionary : IDictionary<object, object> public class ItemsDictionary : IDictionary<object, object>
{ {
private IDictionary<object, object> _items;
public ItemsDictionary() public ItemsDictionary()
: this(new Dictionary<object, object>()) {}
{
}
public ItemsDictionary(IDictionary<object, object> items) public ItemsDictionary(IDictionary<object, object> items)
{ {
Items = items; _items = items;
} }
public IDictionary<object, object> Items { get; } public IDictionary<object, object> Items => this;
// Replace the indexer with one that returns null for missing values // Replace the indexer with one that returns null for missing values
object IDictionary<object, object>.this[object key] object IDictionary<object, object>.this[object key]
{ {
get get
{ {
object value; if (_items != null && _items.TryGetValue(key, out var value))
if (Items.TryGetValue(key, out value))
{ {
return value; return value;
} }
return null; return null;
} }
set { Items[key] = value; } set
{
EnsureDictionary();
_items[key] = value;
}
} }
void IDictionary<object, object>.Add(object key, object value) void IDictionary<object, object>.Add(object key, object value)
{ {
Items.Add(key, value); EnsureDictionary();
_items.Add(key, value);
} }
bool IDictionary<object, object>.ContainsKey(object key) bool IDictionary<object, object>.ContainsKey(object key)
{ => _items != null && _items.ContainsKey(key);
return Items.ContainsKey(key);
}
ICollection<object> IDictionary<object, object>.Keys ICollection<object> IDictionary<object, object>.Keys
{ {
get { return Items.Keys; } get
{
if (_items == null)
{
return EmptyDictionary.Dictionary.Keys;
}
return _items.Keys;
}
} }
bool IDictionary<object, object>.Remove(object key) bool IDictionary<object, object>.Remove(object key)
{ => _items != null && _items.Remove(key);
return Items.Remove(key);
}
bool IDictionary<object, object>.TryGetValue(object key, out object value) bool IDictionary<object, object>.TryGetValue(object key, out object value)
{ {
return Items.TryGetValue(key, out value); value = null;
return _items != null && _items.TryGetValue(key, out value);
} }
ICollection<object> IDictionary<object, object>.Values ICollection<object> IDictionary<object, object>.Values
{ {
get { return Items.Values; } get
{
if (_items == null)
{
return EmptyDictionary.Dictionary.Values;
}
return _items.Values;
}
} }
void ICollection<KeyValuePair<object, object>>.Add(KeyValuePair<object, object> item) void ICollection<KeyValuePair<object, object>>.Add(KeyValuePair<object, object> item)
{ {
Items.Add(item); EnsureDictionary();
_items.Add(item);
} }
void ICollection<KeyValuePair<object, object>>.Clear() void ICollection<KeyValuePair<object, object>>.Clear() => _items?.Clear();
{
Items.Clear();
}
bool ICollection<KeyValuePair<object, object>>.Contains(KeyValuePair<object, object> item) bool ICollection<KeyValuePair<object, object>>.Contains(KeyValuePair<object, object> item)
{ => _items != null && _items.Contains(item);
return Items.Contains(item);
}
void ICollection<KeyValuePair<object, object>>.CopyTo(KeyValuePair<object, object>[] array, int arrayIndex) void ICollection<KeyValuePair<object, object>>.CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{ {
Items.CopyTo(array, arrayIndex); if (_items == null)
{
//Delegate to Empty Dictionary to do the argument checking.
EmptyDictionary.Collection.CopyTo(array, arrayIndex);
}
_items.CopyTo(array, arrayIndex);
} }
int ICollection<KeyValuePair<object, object>>.Count int ICollection<KeyValuePair<object, object>>.Count => _items?.Count ?? 0;
{
get { return Items.Count; }
}
bool ICollection<KeyValuePair<object, object>>.IsReadOnly bool ICollection<KeyValuePair<object, object>>.IsReadOnly => _items?.IsReadOnly ?? false;
{
get { return Items.IsReadOnly; }
}
bool ICollection<KeyValuePair<object, object>>.Remove(KeyValuePair<object, object> item) bool ICollection<KeyValuePair<object, object>>.Remove(KeyValuePair<object, object> item)
{ {
object value; if (_items == null)
if (Items.TryGetValue(item.Key, out value) && Equals(item.Value, value))
{ {
return Items.Remove(item.Key); return false;
}
if (_items.TryGetValue(item.Key, out var value) && Equals(item.Value, value))
{
return _items.Remove(item.Key);
} }
return false; return false;
} }
IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator() private void EnsureDictionary()
{ {
return Items.GetEnumerator(); if (_items == null)
{
_items = new Dictionary<object, object>();
}
} }
IEnumerator IEnumerable.GetEnumerator() IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator()
=> _items?.GetEnumerator() ?? EmptyEnumerator.Instance;
IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator() ?? EmptyEnumerator.Instance;
private class EmptyEnumerator : IEnumerator<KeyValuePair<object, object>>
{ {
return Items.GetEnumerator(); // In own class so only initalized if GetEnumerator is called on an empty ItemsDictionary
public readonly static IEnumerator<KeyValuePair<object, object>> Instance = new EmptyEnumerator();
public KeyValuePair<object, object> Current => default;
object IEnumerator.Current => null;
public void Dispose()
{ }
public bool MoveNext() => false;
public void Reset()
{ }
}
private static class EmptyDictionary
{
// In own class so only initalized if CopyTo is called on an empty ItemsDictionary
public readonly static IDictionary<object, object> Dictionary = new Dictionary<object, object>();
public static ICollection<KeyValuePair<object, object>> Collection => Dictionary;
} }
} }
} }