Browse Source

增加一个Queue做缓存,减少内存分配

tanghai 11 years ago
parent
commit
28af032717

+ 5 - 3
CSharp/Game/Model/Component/TimerComponent.cs

@@ -23,6 +23,8 @@ namespace Model
 		/// </summary>
 		private readonly MultiMap<long, ObjectId> timeId = new MultiMap<long, ObjectId>();
 
+		private readonly Queue<long> timeoutTimer = new Queue<long>();
+
 		public ObjectId Add(long time, int callbackId, Env env)
 		{
 			Timer timer = new Timer
@@ -50,18 +52,18 @@ namespace Model
 		public void Update()
 		{
 			long timeNow = TimeHelper.Now();
-			var timeoutTimer = new List<long>();
 			foreach (long time in this.timeId.Keys)
 			{
 				if (time > timeNow)
 				{
 					break;
 				}
-				timeoutTimer.Add(time);
+				timeoutTimer.Enqueue(time);
 			}
 
-			foreach (long key in timeoutTimer)
+			while (timeoutTimer.Count > 0)
 			{
+				long key = timeoutTimer.Dequeue();
 				List<ObjectId> timeOutId = this.timeId[key];
 				foreach (ObjectId id in timeOutId)
 				{

+ 5 - 3
CSharp/Platform/Common/Base/TimerManager.cs

@@ -21,6 +21,8 @@ namespace Common.Base
 		/// </summary>
 		private readonly MultiMap<long, ObjectId> timeGuid = new MultiMap<long, ObjectId>();
 
+		private readonly Queue<long> timeoutTimer = new Queue<long>();
+
 		public ObjectId Add(long time, Action action)
 		{
 			Timer timer = new Timer { Id = ObjectId.GenerateNewId(), Time = time, Action = action };
@@ -56,18 +58,18 @@ namespace Common.Base
 		public void Refresh()
 		{
 			long timeNow = TimeHelper.Now();
-			var timeoutTimer = new List<long>();
 			foreach (long time in this.timeGuid.Keys)
 			{
 				if (time > timeNow)
 				{
 					break;
 				}
-				timeoutTimer.Add(time);
+				timeoutTimer.Enqueue(time);
 			}
 
-			foreach (long key in timeoutTimer)
+			while (timeoutTimer.Count > 0)
 			{
+				long key = timeoutTimer.Dequeue();
 				ObjectId[] timeoutIds = this.timeGuid.GetAll(key);
 				foreach (ObjectId id in timeoutIds)
 				{