Browse Source

refactor: replace FastListCore with PooledList in Scanner

Akeit0 7 months ago
parent
commit
02dad703f2

+ 1 - 0
src/Lua/CodeAnalysis/Compilation/Declarements.cs

@@ -28,6 +28,7 @@ unsafe struct TextReader(char* ptr, int length)
     public char Current => ptr[Position];
     public char Current => ptr[Position];
 
 
     public ReadOnlySpan<char> Span => new(ptr, length);
     public ReadOnlySpan<char> Span => new(ptr, length);
+    public int Length => length;
 }
 }
 
 
 internal unsafe struct AssignmentTarget(ref AssignmentTarget previous, ExprDesc exprDesc)
 internal unsafe struct AssignmentTarget(ref AssignmentTarget previous, ExprDesc exprDesc)

+ 4 - 2
src/Lua/CodeAnalysis/Compilation/Parser.cs

@@ -41,7 +41,6 @@ internal class Parser : IPoolNode<Parser>, IDisposable
         }
         }
 
 
         parser.Scanner = scanner;
         parser.Scanner = scanner;
-        scanner.Buffer.Clear();
         return parser;
         return parser;
     }
     }
 
 
@@ -49,6 +48,8 @@ internal class Parser : IPoolNode<Parser>, IDisposable
 
 
     public void Release()
     public void Release()
     {
     {
+        Scanner.Buffer.Dispose();
+        Scanner = default;
         ActiveVariables.Clear();
         ActiveVariables.Clear();
         PendingGotos.Clear();
         PendingGotos.Clear();
         ActiveLabels.Clear();
         ActiveLabels.Clear();
@@ -918,7 +919,8 @@ internal class Parser : IPoolNode<Parser>, IDisposable
             LastLine = 1,
             LastLine = 1,
             LookAheadToken = new(0, TkEos),
             LookAheadToken = new(0, TkEos),
             L = l,
             L = l,
-            Source = name
+            Source = name,
+            Buffer = new PooledList<char>(r.Length)
         });
         });
         var f = Function.Get(p, PrototypeBuilder.Get(name));
         var f = Function.Get(p, PrototypeBuilder.Get(name));
         p.Function = f;
         p.Function = f;

+ 1 - 1
src/Lua/CodeAnalysis/Compilation/Scanner.cs

@@ -10,7 +10,7 @@ using static Constants;
 internal struct Scanner
 internal struct Scanner
 {
 {
     public LuaState L;
     public LuaState L;
-    public FastListCore<char> Buffer;
+    public PooledList<char> Buffer;
     public TextReader R;
     public TextReader R;
     public int Current;
     public int Current;
     public int LineNumber, LastLine;
     public int LineNumber, LastLine;

+ 20 - 1
src/Lua/Internal/PooledList.cs

@@ -3,7 +3,7 @@ using System.Runtime.CompilerServices;
 
 
 namespace Lua.Internal;
 namespace Lua.Internal;
 
 
-internal ref struct PooledList<T>
+internal  struct PooledList<T> :IDisposable
 {
 {
     T[]? buffer;
     T[]? buffer;
     int tail;
     int tail;
@@ -15,6 +15,7 @@ internal ref struct PooledList<T>
 
 
     public bool IsDisposed => tail == -1;
     public bool IsDisposed => tail == -1;
     public int Count => tail;
     public int Count => tail;
+    public int Length => tail;
 
 
     public void Add(in T item)
     public void Add(in T item)
     {
     {
@@ -61,6 +62,24 @@ internal ref struct PooledList<T>
         items.CopyTo(buffer.AsSpan()[tail..]);
         items.CopyTo(buffer.AsSpan()[tail..]);
         tail += items.Length;
         tail += items.Length;
     }
     }
+    
+    public void PopUntil(int count)
+    {
+        ThrowIfDisposed();
+
+        if (count > tail) throw new ArgumentOutOfRangeException(nameof(count));
+
+        tail = count;
+    }
+    
+    public void Pop(int count)
+    {
+        ThrowIfDisposed();
+
+        if (count > tail) throw new ArgumentOutOfRangeException(nameof(count));
+
+        tail -= count;
+    }
 
 
     public void Clear()
     public void Clear()
     {
     {