Просмотр исходного кода

优化下Timer Action,改成用数组,性能更佳

tanghai 4 лет назад
Родитель
Сommit
d811ea42a6

+ 9 - 5
Unity/Codes/Model/Core/Timer/TimerComponent.cs

@@ -93,11 +93,13 @@ namespace ET
         // 记录最小时间,不用每次都去MultiMap取第一个值
         private long minTime;
 
-        private readonly Dictionary<int, ITimer> timerActions = new Dictionary<int, ITimer>();
+        private const int TimeTypeMax = 10000;
+
+        private ITimer[] timerActions;
 
         public void Awake()
         {
-            this.timerActions.Clear();
+            this.timerActions = new ITimer[TimeTypeMax];
 
             HashSet<Type> types = Game.EventSystem.GetTypes(typeof (TimerAttribute));
 
@@ -119,7 +121,7 @@ namespace ET
                 foreach (object attr in attrs)
                 {
                     TimerAttribute timerAttribute = attr as TimerAttribute;
-                    this.timerActions.Add(timerAttribute.Type, iTimer);
+                    this.timerActions[timerAttribute.Type] = iTimer;
                 }
             }
         }
@@ -190,9 +192,11 @@ namespace ET
                     int type = timerAction.Type;
                     long tillTime = TimeHelper.ServerNow() + timerAction.Time;
                     this.AddTimer(tillTime, timerAction);
-                    
-                    if (!this.timerActions.TryGetValue(type, out ITimer timer))
+
+                    ITimer timer = this.timerActions[type];
+                    if (timer == null)
                     {
+                        Log.Error($"not found timer action: {type}");
                         return;
                     }
                     timer.Handle(timerAction.Object);

+ 2 - 0
Unity/Codes/Model/Core/Timer/TimerType.cs

@@ -7,5 +7,7 @@
         public const int SessionIdleChecker = 1;
         public const int ActorLocationSenderChecker = 2;
         public const int ActorMessageSenderChecker = 3;
+
+        // 不能超过1000
     }
 }

+ 3 - 1
Unity/Codes/Model/Demo/TimerType.cs

@@ -2,8 +2,10 @@
 {
     public static partial class TimerType
     {
-        // 框架层0-1000,逻辑层的timer type从1000起
+        // 框架层0-1000,逻辑层的timer type 1000-9999
         public const int MoveTimer = 1001;
         public const int AITimer = 1002;
+        
+        // 不能超过10000
     }
 }