Skip to content

Commit f6c7deb

Browse files
committed
Optimize iteration
1 parent 0f1c05d commit f6c7deb

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

DictionaryList/DictionaryList.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,28 @@ internal DictionaryListEnumerator(DictionaryList<TValue> dictList)
282282
public bool MoveNext()
283283
{
284284
var theDictList = _dictList;
285-
while (_version == theDictList._version && (uint)_index < (uint)theDictList._size)
285+
var theItems = theDictList._items;
286+
var theLookup = theDictList._issetLookup;
287+
var currentIndex = _index;
288+
for (; currentIndex < theLookup.Length && currentIndex < theItems.Length; currentIndex++)
286289
{
287-
if (!theDictList._issetLookup[_index])
290+
if (theDictList._version != _version)
291+
{
292+
// modifying the collection during enumeration is not allowed
293+
ThrowHelper.ThrowBadEnumerationException();
294+
_current = default;
295+
return false;
296+
}
297+
if (!theLookup[currentIndex])
288298
{
289299
// not set; find the next one!
290-
_index++;
291300
continue;
292301
}
293-
_current = new KeyValuePair<int, TValue>(_index, theDictList[_index]);
294-
_index++;
302+
_current = new KeyValuePair<int, TValue>(currentIndex, theItems[currentIndex]);
303+
_index = currentIndex + 1;
295304
return true;
296305
}
297306

298-
if (_version != _dictList._version)
299-
{
300-
// DictionaryList was modified during enumeration; not allowed!
301-
ThrowHelper.ThrowBadEnumerationException();
302-
}
303307
// end of list
304308
_current = default;
305309
return false;

0 commit comments

Comments
 (0)