Browse Source

Before major refactoring

Xanathar 11 năm trước cách đây
mục cha
commit
d064e8580d

+ 82 - 1
src/MoonSharp.Interpreter/DataStructs/FastStack.cs

@@ -5,7 +5,7 @@ using System.Text;
 
 
 namespace MoonSharp.Interpreter.Execution.VM
 namespace MoonSharp.Interpreter.Execution.VM
 {
 {
-	public class FastStack<T> 
+	public class FastStack<T> : IList<T>
 	{
 	{
 		T[] m_Storage;
 		T[] m_Storage;
 		int m_HeadIdx = 0;
 		int m_HeadIdx = 0;
@@ -27,6 +27,11 @@ namespace MoonSharp.Interpreter.Execution.VM
 			return item;
 			return item;
 		}
 		}
 
 
+		public void Expand(int size)
+		{
+			m_HeadIdx += size;
+		}
+
 		public void Zero(int from, int to)
 		public void Zero(int from, int to)
 		{
 		{
 			Array.Clear(m_Storage, from, to - from + 1);
 			Array.Clear(m_Storage, from, to - from + 1);
@@ -42,6 +47,10 @@ namespace MoonSharp.Interpreter.Execution.VM
 			T item = m_Storage[m_HeadIdx - 1 - idxofs];
 			T item = m_Storage[m_HeadIdx - 1 - idxofs];
 			return item;
 			return item;
 		}
 		}
+		public void CropAtCount(int p)
+		{
+			RemoveLast(Count - p);
+		}
 
 
 		public void RemoveLast( int cnt = 1)
 		public void RemoveLast( int cnt = 1)
 		{
 		{
@@ -78,5 +87,77 @@ namespace MoonSharp.Interpreter.Execution.VM
 		}
 		}
 
 
 
 
+		int IList<T>.IndexOf(T item)
+		{
+			throw new NotImplementedException();
+		}
+
+		void IList<T>.Insert(int index, T item)
+		{
+			throw new NotImplementedException();
+		}
+
+		void IList<T>.RemoveAt(int index)
+		{
+			throw new NotImplementedException();
+		}
+
+		T IList<T>.this[int index]
+		{
+			get
+			{
+				return this[index];
+			}
+			set
+			{
+				this[index] = value;
+			}
+		}
+
+		void ICollection<T>.Add(T item)
+		{
+			Push(item);
+		}
+
+		void ICollection<T>.Clear()
+		{
+			Clear();
+		}
+
+		bool ICollection<T>.Contains(T item)
+		{
+			throw new NotImplementedException();
+		}
+
+		void ICollection<T>.CopyTo(T[] array, int arrayIndex)
+		{
+			throw new NotImplementedException();
+		}
+
+		int ICollection<T>.Count
+		{
+			get { return this.Count; }
+		}
+
+		bool ICollection<T>.IsReadOnly
+		{
+			get { return false; }
+		}
+
+		bool ICollection<T>.Remove(T item)
+		{
+			throw new NotImplementedException();
+		}
+
+		IEnumerator<T> IEnumerable<T>.GetEnumerator()
+		{
+			throw new NotImplementedException();
+		}
+
+		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+		{
+			throw new NotImplementedException();
+		}
+
 	}
 	}
 }
 }

+ 25 - 22
src/MoonSharp.Interpreter/Execution/Scopes/RuntimeScope.cs

@@ -3,49 +3,49 @@ using System.Collections.Generic;
 using MoonSharp.Interpreter.Diagnostics;
 using MoonSharp.Interpreter.Diagnostics;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
+using MoonSharp.Interpreter.Execution.VM;
 
 
 namespace MoonSharp.Interpreter.Execution
 namespace MoonSharp.Interpreter.Execution
 {
 {
 	public class RuntimeScope
 	public class RuntimeScope
 	{
 	{
 		Table m_GlobalTable;
 		Table m_GlobalTable;
-		List<RValue> m_ScopeStack = new List<RValue>(131072); // start with a 512KB scope stack
-		List<LRef> m_DebugStack = new List<LRef>(131072); // start with a 512KB scope stack
-		List<int> m_LocalBaseIndexes = new List<int>(16384);
-		List<List<RValue>> m_ClosureStack = new List<List<RValue>>();
-		List<RuntimeScopeFrame> m_ScopeFrames = new List<RuntimeScopeFrame>(2048);
+		FastStack<RValue> m_ScopeStack = new FastStack<RValue>(131072); // start with a 512KB scope stack
+//		FastStack<LRef> m_DebugStack = new FastStack<LRef>(131072); // start with a 512KB scope stack
+		FastStack<int> m_LocalBaseIndexes = new FastStack<int>(16384);
+		FastStack<List<RValue>> m_ClosureStack = new FastStack<List<RValue>>(4096);
+		FastStack<RuntimeScopeFrame> m_ScopeFrames = new FastStack<RuntimeScopeFrame>(8192);
 
 
-		public RuntimeScope(Table globalTable)
+		public RuntimeScope()
 		{
 		{
-			m_GlobalTable = globalTable;
+		}
+
+		public Table GlobalTable
+		{
+			get { return m_GlobalTable; }
+			set { m_GlobalTable = value; }
 		}
 		}
 
 
 		public void EnterClosure(List<RValue> closureValues)
 		public void EnterClosure(List<RValue> closureValues)
 		{
 		{
-			m_ClosureStack.Add(closureValues);
+			m_ClosureStack.Push(closureValues);
 		}
 		}
 
 
 		public void LeaveClosure()
 		public void LeaveClosure()
 		{
 		{
-			m_ClosureStack.RemoveAt(m_ClosureStack.Count - 1);
+			m_ClosureStack.RemoveLast();
 		}
 		}
 
 
 		public void PushFrame(RuntimeScopeFrame frame)
 		public void PushFrame(RuntimeScopeFrame frame)
 		{
 		{
 			int size = frame.Count;
 			int size = frame.Count;
 
 
-			m_ScopeFrames.Add(frame);
+			m_ScopeFrames.Push(frame);
 
 
 			if (frame.RestartOfBase)
 			if (frame.RestartOfBase)
-				m_LocalBaseIndexes.Add(m_ScopeStack.Count);
-
-			for (int i = 0; i < size; i++)
-			{
-				m_ScopeStack.Add(new RValue());
-				m_DebugStack.Add(frame.m_DebugSymbols[i]);
-			}
+				m_LocalBaseIndexes.Push(m_ScopeStack.Count);
 
 
-			// Debug.WriteLine(string.Format("RPush : {0} - Stack is {1}", frame, m_ScopeStack.Count));
+			m_ScopeStack.Expand(size);
 		}
 		}
 
 
 		public void PopFrame(RuntimeScopeFrame frame)
 		public void PopFrame(RuntimeScopeFrame frame)
@@ -62,13 +62,13 @@ namespace MoonSharp.Interpreter.Execution
 			int size = frame.Count;
 			int size = frame.Count;
 			if (size > 0)
 			if (size > 0)
 			{
 			{
-				m_ScopeStack.RemoveRange(m_ScopeStack.Count - size, size);
-				m_DebugStack.RemoveRange(m_DebugStack.Count - size, size);
+				m_ScopeStack.RemoveLast(size);
+				//m_DebugStack.RemoveLast(size);
 			}
 			}
 
 
 			if (frame.RestartOfBase)
 			if (frame.RestartOfBase)
 			{
 			{
-				m_LocalBaseIndexes.RemoveAt(m_LocalBaseIndexes.Count - 1);
+				m_LocalBaseIndexes.RemoveLast();
 			}
 			}
 			return frame;
 			return frame;
 		}
 		}
@@ -133,7 +133,10 @@ namespace MoonSharp.Interpreter.Execution
 				case LRefType.Local:
 				case LRefType.Local:
 					{
 					{
 						int lastBaseIdx = m_LocalBaseIndexes[m_LocalBaseIndexes.Count - 1];
 						int lastBaseIdx = m_LocalBaseIndexes[m_LocalBaseIndexes.Count - 1];
-						m_ScopeStack[lastBaseIdx + symref.i_Index].Assign(value);
+						RValue v = m_ScopeStack[lastBaseIdx + symref.i_Index];
+						if (v == null)
+							m_ScopeStack[lastBaseIdx + symref.i_Index] = v = new RValue();
+						v.Assign(value);
 						//m_ScopeStack[lastBaseIdx + symref.Index] = value.CloneAsWritable();
 						//m_ScopeStack[lastBaseIdx + symref.Index] = value.CloneAsWritable();
 					}
 					}
 					break;
 					break;

+ 10 - 1
src/MoonSharp.Interpreter/Execution/Script.cs

@@ -33,9 +33,18 @@ namespace MoonSharp.Interpreter.Execution
 #endif
 #endif
 		}
 		}
 
 
+		RuntimeScope scope = new RuntimeScope();
+		VmExecutor executor;
+
 		public RValue Execute(Table globalContext)
 		public RValue Execute(Table globalContext)
 		{
 		{
-			VmExecutor executor = new VmExecutor(m_GlobalChunk, globalContext ?? new Table());
+			scope.GlobalTable = globalContext ?? new Table();
+
+			if (executor == null)
+			{
+				executor= new VmExecutor(m_GlobalChunk, scope);
+			}
+			executor.Reset();
 
 
 			using (var _ = new CodeChrono("MoonSharpScript.Execute"))
 			using (var _ = new CodeChrono("MoonSharpScript.Execute"))
 			{
 			{

+ 11 - 4
src/MoonSharp.Interpreter/Execution/VM/VmExecutor.cs

@@ -18,8 +18,8 @@ namespace MoonSharp.Interpreter.Execution.VM
 		Chunk m_CurChunk;
 		Chunk m_CurChunk;
 		int m_InstructionPtr;
 		int m_InstructionPtr;
 
 
-		List<RValue> m_ValueStack = new List<RValue>();
-		List<CallStackItem> m_ExecutionStack = new List<CallStackItem>();
+		FastStack<RValue> m_ValueStack = new FastStack<RValue>(131072);
+		FastStack<CallStackItem> m_ExecutionStack = new FastStack<CallStackItem>(131072);
 		bool m_Terminate = false;
 		bool m_Terminate = false;
 
 
 		RuntimeScope m_Scope;
 		RuntimeScope m_Scope;
@@ -27,13 +27,20 @@ namespace MoonSharp.Interpreter.Execution.VM
 		RValue[] m_TempRegs = new RValue[8];
 		RValue[] m_TempRegs = new RValue[8];
 
 
 
 
-		public VmExecutor(Chunk rootChunk, Table globalTable)
+		public VmExecutor(Chunk rootChunk, RuntimeScope runtimeScope)
 		{
 		{
 			m_RootChunk = m_CurChunk = rootChunk;
 			m_RootChunk = m_CurChunk = rootChunk;
 			m_InstructionPtr = 0;
 			m_InstructionPtr = 0;
-			m_Scope = new RuntimeScope(globalTable);
+			m_Scope = runtimeScope;
 		}
 		}
 
 
+		public void Reset()
+		{
+			m_CurChunk = m_RootChunk ;
+			m_InstructionPtr = 0;
+		}
+
+
 		private RValue[] StackTopToArray(int items, bool pop)
 		private RValue[] StackTopToArray(int items, bool pop)
 		{
 		{
 			RValue[] values = new RValue[items];
 			RValue[] values = new RValue[items];

+ 13 - 13
src/PerformanceComparison/Program.cs

@@ -13,9 +13,9 @@ namespace PerformanceComparison
 {
 {
 	class Program
 	class Program
 	{
 	{
-		const int ITERATIONS = 1000;
+		const int ITERATIONS = 100000;
 
 
-		static  string scriptText1 = @"
+		static  string scriptText = @"
 			function move(n, src, dst, via)
 			function move(n, src, dst, via)
 				if n > 0 then
 				if n > 0 then
 					move(n - 1, src, via, dst)
 					move(n - 1, src, via, dst)
@@ -26,7 +26,7 @@ namespace PerformanceComparison
  
  
 			move(4, 1, 2, 3)
 			move(4, 1, 2, 3)
 			";
 			";
-		static  string scriptText = @"
+		static  string scriptText2 = @"
 N = 8
 N = 8
  
  
 board = {}
 board = {}
@@ -63,15 +63,15 @@ if Find_Solution( 1 ) then
     for i = 1, N do
     for i = 1, N do
  	for j = 1, N do
  	for j = 1, N do
   	    if board[i][j] then 
   	    if board[i][j] then 
-		--print( 'Q' )
+		print( 'Q' )
 	    else 
 	    else 
-		--print( 'x' )
+		print( 'x' )
 	    end
 	    end
 	end
 	end
-	--print( '|' )
+	print( '|' )
     end
     end
 else
 else
-    --print( 'NO!' )
+    print( 'NO!' )
 end
 end
  
  
 			";
 			";
@@ -157,7 +157,7 @@ end
 			sw = Stopwatch.StartNew();
 			sw = Stopwatch.StartNew();
 			for (int i = 0; i < ITERATIONS; i++)
 			for (int i = 0; i < ITERATIONS; i++)
 			{
 			{
-				fn.Call();
+				//fn.Call();
 			}
 			}
 			sw.Stop();
 			sw.Stop();
 
 
@@ -165,11 +165,11 @@ end
 
 
 			Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());
 			Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString());
 
 
-			//Console.WriteLine("=== Moon# ===");
-			//Console.WriteLine(g_MoonSharpStr.ToString());
-			//Console.WriteLine("");
-			//Console.WriteLine("=== NLua  ===");
-			//Console.WriteLine(g_NLuaStr.ToString());
+			Console.WriteLine("=== Moon# ===");
+			Console.WriteLine(g_MoonSharpStr.ToString());
+			Console.WriteLine("");
+			Console.WriteLine("=== NLua  ===");
+			Console.WriteLine(g_NLuaStr.ToString());
 
 
 			Console.ReadKey();
 			Console.ReadKey();
 		}
 		}