Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 765eec8

Browse files
committed
Pre-box Empty enumerator
1 parent 4354dbb commit 765eec8

File tree

1 file changed

+83
-23
lines changed

1 file changed

+83
-23
lines changed

src/Microsoft.AspNet.WebUtilities/SafeLazyDictionary.cs

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ internal class SafeLazyDictionary<TValue> : IDictionary<string, TValue>
1212
#if !DNXCORE50
1313
private static readonly string[] EmptyKeys = new string[0];
1414
private static readonly TValue[] EmptyValues = new TValue[0];
15-
private static readonly KeyValuePair<string, TValue>[] EmptyEnumerator = new KeyValuePair<string, TValue>[0];
1615
#endif
17-
public IDictionary<string, TValue> Store { get; set; }
16+
private static readonly Enumerator EmptyEnumerator = new Enumerator();
17+
// Pre-box
18+
private static readonly IEnumerator<KeyValuePair<string, TValue>> EmptyIEnumeratorType = EmptyEnumerator;
19+
private static readonly IEnumerator EmptyIEnumerator = EmptyEnumerator;
20+
21+
private Dictionary<string, TValue> Store { get; set; }
1822

1923
public SafeLazyDictionary()
2024
{
@@ -133,11 +137,14 @@ public void Clear()
133137

134138
public bool Contains(KeyValuePair<string, TValue> item)
135139
{
136-
if (Store == null)
140+
TValue value;
141+
if (Store == null ||
142+
!Store.TryGetValue(item.Key, out value) ||
143+
!value.Equals(item.Value))
137144
{
138145
return false;
139146
}
140-
return Store.Contains(item);
147+
return true;
141148
}
142149

143150
public bool ContainsKey(string key)
@@ -162,20 +169,7 @@ public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
162169
arrayIndex++;
163170
}
164171
}
165-
166-
public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
167-
{
168-
if (Store == null)
169-
{
170-
#if DNXCORE50
171-
return ((IEnumerable<KeyValuePair<string, TValue>>)Array.Empty<KeyValuePair<string, TValue>>()).GetEnumerator();
172-
#else
173-
return ((IEnumerable<KeyValuePair<string, TValue>>)EmptyEnumerator).GetEnumerator();
174-
#endif
175-
}
176-
return Store.GetEnumerator();
177-
}
178-
172+
179173
public bool Remove(KeyValuePair<string, TValue> item)
180174
{
181175
if (Store == null)
@@ -211,17 +205,83 @@ public bool TryGetValue(string key, out TValue value)
211205
return Store.TryGetValue(key, out value);
212206
}
213207

208+
public Enumerator GetEnumerator()
209+
{
210+
if (Store == null)
211+
{
212+
return EmptyEnumerator;
213+
}
214+
return new Enumerator(Store.GetEnumerator());
215+
}
216+
217+
IEnumerator<KeyValuePair<string, TValue>> IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator()
218+
{
219+
if (Store == null)
220+
{
221+
return EmptyIEnumeratorType;
222+
}
223+
return Store.GetEnumerator();
224+
}
214225
IEnumerator IEnumerable.GetEnumerator()
215226
{
216227
if (Store == null)
217228
{
218-
#if DNXCORE50
219-
return Array.Empty<TValue>().GetEnumerator();
220-
#else
221-
return EmptyEnumerator.GetEnumerator();
222-
#endif
229+
return EmptyIEnumerator;
223230
}
224231
return Store.GetEnumerator();
225232
}
233+
234+
public struct Enumerator : IEnumerator<KeyValuePair<string, TValue>>
235+
{
236+
private Dictionary<string, TValue>.Enumerator _dictionaryEnumerator;
237+
private bool _notEmpty;
238+
239+
internal Enumerator(Dictionary<string, TValue>.Enumerator dictionaryEnumerator)
240+
{
241+
_dictionaryEnumerator = dictionaryEnumerator;
242+
_notEmpty = true;
243+
}
244+
245+
public bool MoveNext()
246+
{
247+
if (_notEmpty)
248+
{
249+
return _dictionaryEnumerator.MoveNext();
250+
}
251+
return false;
252+
}
253+
254+
public KeyValuePair<string, TValue> Current
255+
{
256+
get
257+
{
258+
if (_notEmpty)
259+
{
260+
return _dictionaryEnumerator.Current;
261+
}
262+
return default(KeyValuePair<string, TValue>);
263+
}
264+
}
265+
266+
public void Dispose()
267+
{
268+
}
269+
270+
object IEnumerator.Current
271+
{
272+
get
273+
{
274+
return Current;
275+
}
276+
}
277+
278+
void IEnumerator.Reset()
279+
{
280+
if (_notEmpty)
281+
{
282+
((IEnumerator)_dictionaryEnumerator).Reset();
283+
}
284+
}
285+
}
226286
}
227287
}

0 commit comments

Comments
 (0)