Browse Source

Optimize: LuaStack operations

Akeit0 1 year ago
parent
commit
45417a5fdc
2 changed files with 28 additions and 21 deletions
  1. 25 18
      src/Lua/Runtime/LuaStack.cs
  2. 3 3
      src/Lua/Runtime/UpValue.cs

+ 25 - 18
src/Lua/Runtime/LuaStack.cs

@@ -3,7 +3,7 @@ using Lua.Internal;
 
 namespace Lua.Runtime;
 
-public class LuaStack(int initialSize = 256)
+public sealed class LuaStack(int initialSize = 256)
 {
     LuaValue[] array = new LuaValue[initialSize];
     int top;
@@ -13,15 +13,22 @@ public class LuaStack(int initialSize = 256)
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public void EnsureCapacity(int newSize)
     {
-        var size = array.Length;
-        if (size >= newSize) return;
+        if (array.Length >= newSize) return;
+        
+        Resize(ref array, newSize);
+        return;
 
-        while (size < newSize)
+        static void Resize(ref LuaValue[] array, int newSize)
         {
-            size *= 2;
-        }
+            var size = array.Length;
+            while (size < newSize)
+            {
+                size *= 2;
+            }
 
-        Array.Resize(ref array, size);
+            Array.Resize(ref array, size);
+        }
+        
     }
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -34,7 +41,7 @@ public class LuaStack(int initialSize = 256)
     public void Push(LuaValue value)
     {
         EnsureCapacity(top + 1);
-        UnsafeGet(top) = value;
+        array[top] = value;
         top++;
     }
 
@@ -51,8 +58,8 @@ public class LuaStack(int initialSize = 256)
     {
         if (top == 0) ThrowEmptyStack();
         top--;
-        var item = UnsafeGet(top);
-        UnsafeGet(top) = default;
+        var item = array[top];
+        array[top] = default;
         return item;
     }
 
@@ -60,14 +67,8 @@ public class LuaStack(int initialSize = 256)
     public void PopUntil(int newSize)
     {
         if (newSize >= top) return;
-        if (newSize == 0)
-        {
-            array.AsSpan().Clear();
-        }
-        else
-        {
-            array.AsSpan(newSize).Clear();
-        }
+        
+        array.AsSpan(newSize,top-newSize).Clear();
         top = newSize;
     }
 
@@ -101,6 +102,12 @@ public class LuaStack(int initialSize = 256)
     {
         return ref MemoryMarshalEx.UnsafeElementAt(array, index);
     }
+    
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
+    internal ref LuaValue Get(int index)
+    {
+        return ref array[index];
+    }
 
     static void ThrowEmptyStack()
     {

+ 3 - 3
src/Lua/Runtime/UpValue.cs

@@ -42,7 +42,7 @@ public sealed class UpValue
         }
         else
         {
-            return Thread!.Stack.UnsafeGet(RegisterIndex);
+            return Thread!.Stack.Get(RegisterIndex);
         }
     }
 
@@ -55,7 +55,7 @@ public sealed class UpValue
         }
         else
         {
-            Thread!.Stack.UnsafeGet(RegisterIndex) = value;
+            Thread!.Stack.Get(RegisterIndex) = value;
         }
     }
 
@@ -64,7 +64,7 @@ public sealed class UpValue
     {
         if (!IsClosed)
         {
-            value = Thread!.Stack.UnsafeGet(RegisterIndex);
+            value = Thread!.Stack.Get(RegisterIndex);
         }
 
         IsClosed = true;