Browse Source

Optimize: vm (SetTableValue)

AnnulusGames 1 year ago
parent
commit
7740ff1cb9
1 changed files with 15 additions and 24 deletions
  1. 15 24
      src/Lua/Runtime/LuaVirtualMachine.cs

+ 15 - 24
src/Lua/Runtime/LuaVirtualMachine.cs

@@ -85,7 +85,7 @@ public static partial class LuaVirtualMachine
 
 
                             var upValue = closure.UpValues[instruction.A];
                             var upValue = closure.UpValues[instruction.A];
                             var table = upValue.GetValue();
                             var table = upValue.GetValue();
-                            await SetTableValue(state, chunk, pc, table, vb, vc, cancellationToken);
+                            await SetTableValue(state, chunk, pc, table, vb, vc, methodBuffer.AsMemory(), cancellationToken);
                             break;
                             break;
                         }
                         }
                     case OpCode.SetUpVal:
                     case OpCode.SetUpVal:
@@ -99,7 +99,7 @@ public static partial class LuaVirtualMachine
                             var table = stack.UnsafeGet(RA);
                             var table = stack.UnsafeGet(RA);
                             var vb = RK(stack, chunk, instruction.B, frame.Base);
                             var vb = RK(stack, chunk, instruction.B, frame.Base);
                             var vc = RK(stack, chunk, instruction.C, frame.Base);
                             var vc = RK(stack, chunk, instruction.C, frame.Base);
-                            await SetTableValue(state, chunk, pc, table, vb, vc, cancellationToken);
+                            await SetTableValue(state, chunk, pc, table, vb, vc, methodBuffer.AsMemory(), cancellationToken);
                         }
                         }
                         break;
                         break;
                     case OpCode.NewTable:
                     case OpCode.NewTable:
@@ -964,10 +964,7 @@ public static partial class LuaVirtualMachine
         }
         }
     }
     }
 
 
-#if NET6_0_OR_GREATER
-    [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))]
-#endif
-    static async ValueTask SetTableValue(LuaState state, Chunk chunk, int pc, LuaValue table, LuaValue key, LuaValue value, CancellationToken cancellationToken)
+    static ValueTask<int> SetTableValue(LuaState state, Chunk chunk, int pc, LuaValue table, LuaValue key, LuaValue value, Memory<LuaValue> buffer, CancellationToken cancellationToken)
     {
     {
         var thread = state.CurrentThread;
         var thread = state.CurrentThread;
         var stack = thread.Stack;
         var stack = thread.Stack;
@@ -981,6 +978,7 @@ public static partial class LuaVirtualMachine
         if (isTable && t.ContainsKey(key))
         if (isTable && t.ContainsKey(key))
         {
         {
             t[key] = value;
             t[key] = value;
+            return new(1);
         }
         }
         else if (table.TryGetMetamethod(state, Metamethods.NewIndex, out var metamethod))
         else if (table.TryGetMetamethod(state, Metamethods.NewIndex, out var metamethod))
         {
         {
@@ -993,33 +991,26 @@ public static partial class LuaVirtualMachine
             stack.Push(key);
             stack.Push(key);
             stack.Push(value);
             stack.Push(value);
 
 
-            var methodBuffer = ArrayPool<LuaValue>.Shared.Rent(1024);
-            methodBuffer.AsSpan().Clear();
-            try
-            {
-                await indexTable.InvokeAsync(new()
-                {
-                    State = state,
-                    Thread = thread,
-                    ArgumentCount = 3,
-                    FrameBase = stack.Count - 3,
-                    SourcePosition = chunk.SourcePositions[pc],
-                    ChunkName = chunk.Name,
-                    RootChunkName = chunk.GetRoot().Name,
-                }, methodBuffer, cancellationToken);
-            }
-            finally
+            return indexTable.InvokeAsync(new()
             {
             {
-                ArrayPool<LuaValue>.Shared.Return(methodBuffer);
-            }
+                State = state,
+                Thread = thread,
+                ArgumentCount = 3,
+                FrameBase = stack.Count - 3,
+                SourcePosition = chunk.SourcePositions[pc],
+                ChunkName = chunk.Name,
+                RootChunkName = chunk.GetRoot().Name,
+            }, buffer, cancellationToken);
         }
         }
         else if (isTable)
         else if (isTable)
         {
         {
             t[key] = value;
             t[key] = value;
+            return new(1);
         }
         }
         else
         else
         {
         {
             LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "index", table);
             LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "index", table);
+            return default; // dummy
         }
         }
     }
     }