Skip to content

Commit b9bdda8

Browse files
authored
Merge pull request #60 from Akeit0/optimize-table-next
Optimize: LuaTable.TryGetNext
2 parents 7dfe7b1 + e0dd90a commit b9bdda8

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/Lua/Internal/LuaValueDictionary.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public LuaValue this[LuaValue key]
4545
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4646
get
4747
{
48-
ref LuaValue value = ref FindValue(key);
48+
ref LuaValue value = ref FindValue(key, out _);
4949
if (!Unsafe.IsNullRef(ref value))
5050
{
5151
return value;
@@ -77,7 +77,7 @@ public void Clear()
7777
}
7878

7979
public bool ContainsKey(LuaValue key) =>
80-
!Unsafe.IsNullRef(ref FindValue(key));
80+
!Unsafe.IsNullRef(ref FindValue(key, out _));
8181

8282
public bool ContainsValue(LuaValue value)
8383
{
@@ -97,8 +97,9 @@ public bool ContainsValue(LuaValue value)
9797

9898
public Enumerator GetEnumerator() => new Enumerator(this);
9999

100-
internal ref LuaValue FindValue(LuaValue key)
100+
internal ref LuaValue FindValue(LuaValue key, out int index)
101101
{
102+
index = -1;
102103
ref Entry entry = ref Unsafe.NullRef<Entry>();
103104
if (_buckets != null)
104105
{
@@ -123,6 +124,7 @@ internal ref LuaValue FindValue(LuaValue key)
123124
entry = ref entries[i];
124125
if (entry.hashCode == hashCode && entry.key.Equals(key))
125126
{
127+
index = i;
126128
goto ReturnFound;
127129
}
128130

@@ -352,7 +354,7 @@ public bool Remove(LuaValue key)
352354
[MethodImpl(MethodImplOptions.AggressiveInlining)]
353355
public bool TryGetValue(LuaValue key, out LuaValue value)
354356
{
355-
ref LuaValue valRef = ref FindValue(key);
357+
ref LuaValue valRef = ref FindValue(key, out _);
356358
if (!Unsafe.IsNullRef(ref valRef))
357359
{
358360
value = valRef;
@@ -363,6 +365,27 @@ public bool TryGetValue(LuaValue key, out LuaValue value)
363365
return false;
364366
}
365367

368+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
369+
public bool TryGetNext(LuaValue key, out KeyValuePair<LuaValue,LuaValue> pair)
370+
{
371+
ref LuaValue valRef = ref FindValue(key, out var index);
372+
if (!Unsafe.IsNullRef(ref valRef))
373+
{
374+
Entry[] entries = _entries!;
375+
while (++index < _count)
376+
{
377+
ref Entry entry = ref entries[index];
378+
if (entry is { next: >= -1, value.Type: not LuaValueType.Nil })
379+
{
380+
pair = new(entry.key, entry.value);
381+
return true;
382+
}
383+
}
384+
}
385+
386+
pair = default;
387+
return false;
388+
}
366389

367390
[MethodImpl(MethodImplOptions.AggressiveInlining)]
368391
private ref int GetBucket(uint hashCode)
@@ -428,6 +451,7 @@ public bool MoveNext()
428451

429452
public KeyValuePair<LuaValue, LuaValue> Current => _current;
430453
}
454+
431455
static class ThrowHelper
432456
{
433457
public static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported()

src/Lua/LuaTable.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,9 @@ public bool TryGetNext(LuaValue key, out KeyValuePair<LuaValue, LuaValue> pair)
194194
}
195195
else
196196
{
197-
var foundKey = false;
198-
foreach (var kv in dictionary)
197+
if(dictionary.TryGetNext(key, out pair))
199198
{
200-
if (foundKey && kv.Value.Type is not LuaValueType.Nil)
201-
{
202-
pair = kv;
203-
return true;
204-
}
205-
206-
if (kv.Key.Equals(key))
207-
{
208-
foundKey = true;
209-
}
199+
return true;
210200
}
211201
}
212202

0 commit comments

Comments
 (0)