Browse Source

Optimize: Cache Chunk.GetRoot()

Akeit0 1 year ago
parent
commit
bffc0e194f
1 changed files with 5 additions and 2 deletions
  1. 5 2
      src/Lua/Runtime/Chunk.cs

+ 5 - 2
src/Lua/Runtime/Chunk.cs

@@ -14,9 +14,12 @@ public sealed class Chunk
     public required Chunk[] Functions { get; init; }
     public required int ParameterCount { get; init; }
 
+    Chunk? rootCache;
+
     internal Chunk GetRoot()
     {
-        if (Parent == null) return this;
-        return Parent.GetRoot();
+        if (rootCache != null) return rootCache;
+        if (Parent == null) return rootCache = this;
+        return rootCache = Parent.GetRoot();
     }
 }