Skip to content

Commit

Permalink
rework StringMap.GetEnumerator()
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Dec 1, 2024
1 parent 5f252e2 commit 7f02e72
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
8 changes: 2 additions & 6 deletions NiL.JS/Core/JSValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ public bool Exists
#if !NET40
[MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
get
{ return _valueType >= JSValueType.Undefined; }
get => _valueType >= JSValueType.Undefined;
}

[Hidden]
Expand All @@ -399,10 +398,7 @@ public bool Defined
#if !NET40
[MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
get
{
return _valueType > JSValueType.Undefined;
}
get => _valueType > JSValueType.Undefined;
}

[Hidden]
Expand Down
59 changes: 26 additions & 33 deletions NiL.JS/Core/StringMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override string ToString()
#endif
}

private const int InitialSize = 2;
private const int InitialSize = 4;
private const int MaxAsListSize = 4;

private int _version;
Expand Down Expand Up @@ -595,15 +595,18 @@ public virtual bool Remove(KeyValuePair<string, TValue> item)

public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
{
List<KeyValuePair<uint, string>> numbers = null;
uint exprected = 0;

var i = 0;
while (i < _eicount)
var yielded = false;
do
{
yielded = false;
var prevVersion = _version;

for (; i < _records.Length; i++)
var minDelta = uint.MaxValue;
var indexOfMinDelta = -1;

for (var i = 0; i < _records.Length; i++)
{
int index = i;
if (_records[index].key != null
Expand All @@ -613,48 +616,38 @@ public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
{
yield return new KeyValuePair<string, TValue>(_records[index].key, _records[index].value);
exprected++;
yielded = true;
minDelta = uint.MinValue;
indexOfMinDelta = -1;
}
else
else if (number > exprected && (number - exprected < minDelta || (exprected == 0 && number == uint.MaxValue)))
{
if (numbers == null)
numbers = new List<KeyValuePair<uint, string>>();

numbers.Add(new KeyValuePair<uint, string>(number, _records[i].key));
minDelta = number - exprected;
indexOfMinDelta = index;
}
}
}

if (prevVersion != _version)
if (indexOfMinDelta >= 0)
{
i = 0;
}
yield return new KeyValuePair<string, TValue>(_records[indexOfMinDelta].key, _records[indexOfMinDelta].value);

if (numbers != null)
{
numbers.Sort((x, y) => x.Key.CompareTo(y.Key));
if (exprected + minDelta == uint.MaxValue)
break;

for (var ni = 0; ni < numbers.Count && prevVersion == _version; ni++)
{
if (numbers[ni].Key >= exprected && TryGetValue(numbers[ni].Value, out var item))
{
yield return new KeyValuePair<string, TValue>(numbers[ni].Value, item);
exprected = numbers[ni].Key + 1;
}
}
exprected += minDelta + 1;
yielded = true;
}
}
while (yielded);

i = 0;
while (i < _eicount)
for (var i = 0; i < _eicount; i++)
{
for (; i < _eicount; i++)
int index = _existsedIndexes[i];
if (_records[index].key != null
&& (!uint.TryParse(_records[index].key, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)))
{
int index = _existsedIndexes[i];
if (_records[index].key != null
&& (!uint.TryParse(_records[index].key, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)))
{
yield return new KeyValuePair<string, TValue>(_records[index].key, _records[index].value);
}
yield return new KeyValuePair<string, TValue>(_records[index].key, _records[index].value);
}
}
}
Expand Down

0 comments on commit 7f02e72

Please sign in to comment.