Browse Source

Optimize: Merge GetTabUp/GetTable operation

Akeit0 11 months ago
parent
commit
057c73e749
3 changed files with 28 additions and 25 deletions
  1. 10 4
      src/Lua/Runtime/Closure.cs
  2. 5 21
      src/Lua/Runtime/LuaVirtualMachine.cs
  3. 13 0
      src/Lua/Runtime/UpValue.cs

+ 10 - 4
src/Lua/Runtime/Closure.cs

@@ -31,6 +31,12 @@ public sealed class Closure : LuaFunction
         return upValues[index].GetValue();
         return upValues[index].GetValue();
     }
     }
 
 
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
+    internal ref readonly LuaValue GetUpValueRef(int index)
+    {
+        return ref upValues[index].GetValueRef();
+    }
+
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     internal void SetUpValue(int index, LuaValue value)
     internal void SetUpValue(int index, LuaValue value)
     {
     {
@@ -43,17 +49,17 @@ public sealed class Closure : LuaFunction
         {
         {
             return state.GetOrAddUpValue(thread, thread.GetCallStackFrames()[^1].Base + description.Index);
             return state.GetOrAddUpValue(thread, thread.GetCallStackFrames()[^1].Base + description.Index);
         }
         }
-        
+
         if (description.Index == -1) // -1 is global environment
         if (description.Index == -1) // -1 is global environment
         {
         {
             return envUpValue;
             return envUpValue;
         }
         }
-        
+
         if (thread.GetCallStackFrames()[^1].Function is Closure parentClosure)
         if (thread.GetCallStackFrames()[^1].Function is Closure parentClosure)
         {
         {
-             return parentClosure.UpValues[description.Index];
+            return parentClosure.UpValues[description.Index];
         }
         }
-        
+
         throw new Exception();
         throw new Exception();
     }
     }
 }
 }

+ 5 - 21
src/Lua/Runtime/LuaVirtualMachine.cs

@@ -308,28 +308,13 @@ public static partial class LuaVirtualMachine
                         stack.GetWithNotifyTop(instruction.A + frameBase) = context.Closure.GetUpValue(instruction.B);
                         stack.GetWithNotifyTop(instruction.A + frameBase) = context.Closure.GetUpValue(instruction.B);
                         continue;
                         continue;
                     case OpCode.GetTabUp:
                     case OpCode.GetTabUp:
+                    case OpCode.GetTable:
                         instruction = instructionRef;
                         instruction = instructionRef;
                         stackHead = ref stack.FastGet(frameBase);
                         stackHead = ref stack.FastGet(frameBase);
                         ref readonly var vc = ref RKC(ref stackHead, ref constHead, instruction);
                         ref readonly var vc = ref RKC(ref stackHead, ref constHead, instruction);
-                        var table = context.Closure.GetUpValue(instruction.B);
-
+                        ref readonly var vb = ref (instruction.OpCode == OpCode.GetTable ? ref Unsafe.Add(ref stackHead, instruction.UIntB) : ref context.Closure.GetUpValueRef(instruction.B));
                         var doRestart = false;
                         var doRestart = false;
-                        if (table.TryReadTable(out var luaTable) && luaTable.TryGetValue(vc, out var resultValue) || GetTableValueSlowPath(table, vc, ref context, out resultValue, out doRestart))
-                        {
-                            if (doRestart) goto Restart;
-                            stack.GetWithNotifyTop(instruction.A + frameBase) = resultValue;
-                            continue;
-                        }
-
-                        postOperation = PostOperationType.SetResult;
-                        return true;
-                    case OpCode.GetTable:
-                        instruction = instructionRef;
-                        stackHead = ref stack.FastGet(frameBase);
-                        ref readonly var vb = ref Unsafe.Add(ref stackHead, instruction.UIntB);
-                        vc = ref RKC(ref stackHead, ref constHead, instruction);
-                        doRestart = false;
-                        if (vb.TryReadTable(out luaTable) && luaTable.TryGetValue(vc, out resultValue) || GetTableValueSlowPath(vb, vc, ref context, out resultValue, out doRestart))
+                        if (vb.TryReadTable(out var luaTable) && luaTable.TryGetValue(vc, out var resultValue) || GetTableValueSlowPath(vb, vc, ref context, out resultValue, out doRestart))
                         {
                         {
                             if (doRestart) goto Restart;
                             if (doRestart) goto Restart;
                             stack.GetWithNotifyTop(instruction.A + frameBase) = resultValue;
                             stack.GetWithNotifyTop(instruction.A + frameBase) = resultValue;
@@ -351,9 +336,8 @@ public static partial class LuaVirtualMachine
                             }
                             }
                         }
                         }
 
 
-                        table = context.Closure.GetUpValue(instruction.A);
-
-
+                        var table = context.Closure.GetUpValue(instruction.A);
+                        
                         if (table.TryReadTable(out luaTable))
                         if (table.TryReadTable(out luaTable))
                         {
                         {
                             ref var valueRef = ref luaTable.FindValue(vb);
                             ref var valueRef = ref luaTable.FindValue(vb);

+ 13 - 0
src/Lua/Runtime/UpValue.cs

@@ -45,6 +45,19 @@ public sealed class UpValue
             return Thread!.Stack.Get(RegisterIndex);
             return Thread!.Stack.Get(RegisterIndex);
         }
         }
     }
     }
+    
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
+    internal ref readonly LuaValue GetValueRef()
+    {
+        if (IsClosed)
+        {
+            return ref value;
+        }
+        else
+        {
+            return ref Thread!.Stack.Get(RegisterIndex);
+        }
+    }
 
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public void SetValue(LuaValue value)
     public void SetValue(LuaValue value)