From 8e5814e61aecefa28167f1860a62e95e6acbdbbd Mon Sep 17 00:00:00 2001 From: pangweiwei Date: Sun, 14 Jan 2018 14:22:48 +0800 Subject: [PATCH] fix timer gc --- Assets/Plugins/Slua_Managed/Timer.cs | 43 ++++++++++++++-------------- Assets/Slua/Editor/LuaConsole.cs | 4 +++ Assets/Slua/Resources/main.txt | 8 ++++++ Assets/Slua/example/Main.cs | 6 ++-- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/Timer.cs b/Assets/Plugins/Slua_Managed/Timer.cs index 5c7c1215..367ce199 100644 --- a/Assets/Plugins/Slua_Managed/Timer.cs +++ b/Assets/Plugins/Slua_Managed/Timer.cs @@ -27,6 +27,7 @@ namespace SLua public class LuaTimer : LuaObject { + const int TimerMaxInit = 16; class Timer { internal int sn; @@ -35,13 +36,13 @@ class Timer internal Func handler; internal bool delete; internal IntPtr L; - internal LinkedList container; + internal List container; } class Wheel { internal static int dial_scale = 256; internal int head; - internal LinkedList[] vecDial; + internal List[] vecDial; internal int dialSize; internal int timeRange; internal Wheel nextWheel; @@ -50,20 +51,20 @@ internal Wheel(int dialSize) this.dialSize = dialSize; this.timeRange = dialSize * dial_scale; this.head = 0; - this.vecDial = new LinkedList[dial_scale]; + this.vecDial = new List[dial_scale]; for (int i = 0; i < dial_scale; ++i) { - this.vecDial[i] = new LinkedList(); + this.vecDial[i] = new List(TimerMaxInit); } } - internal LinkedList nextDial() + internal List nextDial() { return vecDial[head++]; } internal void add(int delay, Timer tm) { var container = vecDial[(head + (delay - (dialSize - jiffies_msec)) / dialSize) % dial_scale]; - container.AddLast(tm); + container.Add(tm); tm.container = container; } } @@ -74,8 +75,8 @@ internal void add(int delay, Timer tm) static float pileSecs; static float nowTime; static Dictionary mapSnTimer; - static LinkedList executeTimers; - + static List executeTimers; + static int intpow(int n, int m) { int ret = 1; @@ -126,7 +127,8 @@ internal static void tick(float deltaTime) { if (executeTimers == null) return; - + + //UnityEngine.Profiling.Profiler.BeginSample ("timer tick"); nowTime += deltaTime; pileSecs += deltaTime; int cycle = 0; @@ -138,12 +140,10 @@ internal static void tick(float deltaTime) for (int i = 0; i < cycle; ++i) { var timers = wheels[0].nextDial(); - LinkedListNode node = timers.First; for (int j = 0; j < timers.Count; ++j) { - var tm = node.Value; - executeTimers.AddLast(tm); - node = node.Next; + var tm = timers[j]; + executeTimers.Add(tm); } timers.Clear(); @@ -156,10 +156,9 @@ internal static void tick(float deltaTime) if (wheel.nextWheel != null) { var tms = wheel.nextWheel.nextDial(); - LinkedListNode tmsNode = tms.First; for (int k = 0; k < tms.Count; ++k) { - var tm = tmsNode.Value; + var tm = tms[k]; if (tm.delete) { mapSnTimer.Remove(tm.sn); @@ -168,7 +167,6 @@ internal static void tick(float deltaTime) { innerAdd(tm.deadline, tm); } - tmsNode = tmsNode.Next; } tms.Clear(); } @@ -179,11 +177,12 @@ internal static void tick(float deltaTime) } } } - - while (executeTimers.Count > 0) + // run timer callback + for (int n=0;n 0) { innerAdd(now() + tm.cycle, tm); @@ -193,6 +192,8 @@ internal static void tick(float deltaTime) mapSnTimer.Remove(tm.sn); } } + executeTimers.Clear(); + //UnityEngine.Profiling.Profiler.EndSample (); } static bool inited = false; static void init() @@ -210,7 +211,7 @@ static void init() } } mapSnTimer = new Dictionary(); - executeTimers = new LinkedList(); + executeTimers = new List(TimerMaxInit); } static int fetchSn() diff --git a/Assets/Slua/Editor/LuaConsole.cs b/Assets/Slua/Editor/LuaConsole.cs index 011e8a48..00a34a85 100644 --- a/Assets/Slua/Editor/LuaConsole.cs +++ b/Assets/Slua/Editor/LuaConsole.cs @@ -426,7 +426,11 @@ void ResizeScrollView() GUILayout.Space(4); Event e = Event.current; + #if UNITY_2017_3_OR_NEWER + if (e.type == EventType.MouseDown && dragSpliterRect.Contains(e.mousePosition)) + #else if (e.type == EventType.mouseDown && dragSpliterRect.Contains(e.mousePosition)) + #endif { e.Use(); inputAreaResizing = true; diff --git a/Assets/Slua/Resources/main.txt b/Assets/Slua/Resources/main.txt index 07eb53ef..c5c95ef4 100644 --- a/Assets/Slua/Resources/main.txt +++ b/Assets/Slua/Resources/main.txt @@ -202,6 +202,14 @@ function main() cube.transform.localRotation = Quaternion.Euler(0,0,math.sin(Time.time)*90) return true end) + + -- test timer + for i=1,20 do + LuaTimer.Add(0,i*1000,function() + print("timer callback",i*1000) + return true + end) + end print(UnityEngine.PrimitiveType.Cube,type(UnityEngine.PrimitiveType.Cube)) diff --git a/Assets/Slua/example/Main.cs b/Assets/Slua/example/Main.cs index fb243e9d..032bf33e 100644 --- a/Assets/Slua/example/Main.cs +++ b/Assets/Slua/example/Main.cs @@ -25,8 +25,10 @@ void Start() void log(string cond, string trace, LogType lt) { - logText.text += (cond + "\n"); - + string txt = logText.text + (cond + "\n"); + if(txt.Length>1000) + txt = txt.Substring (txt.Length - 1000); + logText.text = txt; } void tick(int p)