Browse Source

Optimize: LuaTable indexing

Akeit0 1 year ago
parent
commit
ea44315b1d
1 changed files with 23 additions and 21 deletions
  1. 23 21
      src/Lua/LuaTable.cs

+ 23 - 21
src/Lua/LuaTable.cs

@@ -31,7 +31,7 @@ public sealed class LuaTable
                 if (index > 0 && index <= array.Length)
                 {
                     // Arrays in Lua are 1-origin...
-                    return MemoryMarshalEx.UnsafeElementAt(array, index - 1);
+                    return array[index - 1];
                 }
             }
 
@@ -41,18 +41,23 @@ public sealed class LuaTable
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         set
         {
-            if (key.Type is LuaValueType.Number && double.IsNaN(key.UnsafeRead<double>()))
+            if (key.TryReadNumber(out var d))
             {
-                ThrowIndexIsNaN();
-            }
+                if (double.IsNaN(d))
+                {
+                    ThrowIndexIsNaN();
+                }
 
-            if (TryGetInteger(key, out var index))
-            {
-                if (0 < index && index <= Math.Max(array.Length * 2, 8))
+                if (MathEx.IsInteger(d))
                 {
-                    EnsureArrayCapacity(index);
-                    MemoryMarshalEx.UnsafeElementAt(array, index - 1) = value;
-                    return;
+                    var index = (int)d;
+                    if (0 < index && index <= Math.Max(array.Length * 2, 8))
+                    {
+                        if (array.Length < index)
+                            EnsureArrayCapacity(index);
+                        array[index - 1] = value;
+                        return;
+                    }
                 }
             }
 
@@ -73,6 +78,7 @@ public sealed class LuaTable
             {
                 if (array[i].Type is LuaValueType.Nil) return i;
             }
+
             return array.Length;
         }
     }
@@ -83,6 +89,7 @@ public sealed class LuaTable
         set => metatable = value;
     }
 
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public bool TryGetValue(LuaValue key, out LuaValue value)
     {
         if (key.Type is LuaValueType.Nil)
@@ -95,7 +102,7 @@ public sealed class LuaTable
         {
             if (index > 0 && index <= array.Length)
             {
-                value = MemoryMarshalEx.UnsafeElementAt(array, index - 1);
+                value = array[index - 1];
                 return value.Type is not LuaValueType.Nil;
             }
         }
@@ -113,7 +120,7 @@ public sealed class LuaTable
         if (TryGetInteger(key, out var index))
         {
             return index > 0 && index <= array.Length &&
-                MemoryMarshalEx.UnsafeElementAt(array, index - 1).Type != LuaValueType.Nil;
+                   array[index - 1].Type != LuaValueType.Nil;
         }
 
         return dictionary.TryGetValue(key, out var value) && value.Type is not LuaValueType.Nil;
@@ -121,20 +128,15 @@ public sealed class LuaTable
 
     public LuaValue RemoveAt(int index)
     {
-        if (index <= 0 || index > array.Length)
-        {
-            throw new IndexOutOfRangeException();
-        }
-
         var arrayIndex = index - 1;
-        var value = MemoryMarshalEx.UnsafeElementAt(array, arrayIndex);
+        var value = array[arrayIndex];
 
         if (arrayIndex < array.Length - 1)
         {
             array.AsSpan(arrayIndex + 1).CopyTo(array.AsSpan(arrayIndex));
         }
-        
-        MemoryMarshalEx.UnsafeElementAt(array, array.Length - 1) = default;
+
+        array[^1] = default;
 
         return value;
     }
@@ -153,7 +155,7 @@ public sealed class LuaTable
         {
             array.AsSpan(arrayIndex, array.Length - arrayIndex - 1).CopyTo(array.AsSpan(arrayIndex + 1));
         }
-        
+
         array[arrayIndex] = value;
     }