diff --git a/Server/Model/Base/MultiMap.cs b/Server/Model/Base/MultiMap.cs deleted file mode 100644 index 1e45eabe57fcf2f096c75ace9bd2ca3e600fb97a..0000000000000000000000000000000000000000 --- a/Server/Model/Base/MultiMap.cs +++ /dev/null @@ -1,159 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace ETModel -{ - public class MultiMap - { - private readonly SortedDictionary> dictionary = new SortedDictionary>(); - - // 重用list - private readonly Queue> queue = new Queue>(); - - public SortedDictionary> GetDictionary() - { - return this.dictionary; - } - - public void Add(T t, K k) - { - List list; - this.dictionary.TryGetValue(t, out list); - if (list == null) - { - list = this.FetchList(); - } - list.Add(k); - this.dictionary[t] = list; - } - - public KeyValuePair> First() - { - return this.dictionary.First(); - } - - public T FirstKey() - { - return this.dictionary.Keys.First(); - } - - public int Count - { - get - { - return this.dictionary.Count; - } - } - - private List FetchList() - { - if (this.queue.Count > 0) - { - List list = this.queue.Dequeue(); - list.Clear(); - return list; - } - return new List(); - } - - private void RecycleList(List list) - { - // 防止暴涨 - if (this.queue.Count > 100) - { - return; - } - list.Clear(); - this.queue.Enqueue(list); - } - - public bool Remove(T t, K k) - { - List list; - this.dictionary.TryGetValue(t, out list); - if (list == null) - { - return false; - } - if (!list.Remove(k)) - { - return false; - } - if (list.Count == 0) - { - this.RecycleList(list); - this.dictionary.Remove(t); - } - return true; - } - - public bool Remove(T t) - { - List list = null; - this.dictionary.TryGetValue(t, out list); - if (list != null) - { - this.RecycleList(list); - } - return this.dictionary.Remove(t); - } - - /// - /// 不返回内部的list,copy一份出来 - /// - /// - /// - public K[] GetAll(T t) - { - List list; - this.dictionary.TryGetValue(t, out list); - if (list == null) - { - return new K[0]; - } - return list.ToArray(); - } - - /// - /// 返回内部的list - /// - /// - /// - public List this[T t] - { - get - { - List list; - this.dictionary.TryGetValue(t, out list); - return list; - } - } - - public K GetOne(T t) - { - List list; - this.dictionary.TryGetValue(t, out list); - if (list != null && list.Count > 0) - { - return list[0]; - } - return default(K); - } - - public bool Contains(T t, K k) - { - List list; - this.dictionary.TryGetValue(t, out list); - if (list == null) - { - return false; - } - return list.Contains(k); - } - - public bool ContainsKey(T t) - { - return this.dictionary.ContainsKey(t); - } - } -} \ No newline at end of file diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index c89e2b112220a1bf7ac7a543b90349e66bfe8d6d..4ec03a65ccb03c64d3e6ca5e89a6fd01e2cdb3f6 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -34,6 +34,7 @@ + diff --git a/Unity/Assets/Scripts/Base/MultiMap.cs b/Unity/Assets/Scripts/Base/MultiMap.cs index 6ec87c5c70c0f0ff6ab266d62ae6365071ac6b4b..2e12797115eba65265c49d4acfd93dca3ebbac97 100644 --- a/Unity/Assets/Scripts/Base/MultiMap.cs +++ b/Unity/Assets/Scripts/Base/MultiMap.cs @@ -5,13 +5,14 @@ namespace ETModel { public class MultiMap { - // 客户端用SortedList,因为unity用SortedDictionary在取firstkey的时候有gc,再者客户端的插入删除并不多 - private readonly SortedList> dictionary = new SortedList>(); + private readonly SortedDictionary> dictionary = new SortedDictionary>(); // 重用list private readonly Queue> queue = new Queue>(); - public SortedList> GetDictionary() + private T firstKey; + + public SortedDictionary> GetDictionary() { return this.dictionary; } @@ -35,7 +36,7 @@ namespace ETModel public T FirstKey() { - return this.dictionary.Keys[0]; + return this.dictionary.Keys.First(); } public int Count diff --git a/Unity/Assets/Scripts/Component/TimerComponent.cs b/Unity/Assets/Scripts/Component/TimerComponent.cs index 72c7c2b02e0ff5f8889a89e16b1bd205ed68ad15..1dbff5ac1adad9ebd6d2c4a1d2bb311762754b86 100644 --- a/Unity/Assets/Scripts/Component/TimerComponent.cs +++ b/Unity/Assets/Scripts/Component/TimerComponent.cs @@ -29,7 +29,10 @@ namespace ETModel /// private readonly MultiMap timeId = new MultiMap(); - private readonly List timeOutId = new List(); + private readonly List timeOutTime = new List(); + + // 记录最小时间,不用每次都去MultiMap取第一个值 + private long minTime; public void Update() { @@ -40,31 +43,38 @@ namespace ETModel long timeNow = TimeHelper.Now(); - timeOutId.Clear(); + if (timeNow < this.minTime) + { + return; + } + + this.timeOutTime.Clear(); - while (this.timeId.Count > 0) + foreach (KeyValuePair> kv in this.timeId.GetDictionary()) { - long k = this.timeId.FirstKey(); + long k = kv.Key; if (k > timeNow) { + minTime = k; break; } - foreach (long ll in this.timeId[k]) - { - this.timeOutId.Add(ll); - } - this.timeId.Remove(k); + this.timeOutTime.Add(k); } - foreach (long k in this.timeOutId) + foreach (long k in this.timeOutTime) { - Timer timer; - if (!this.timers.TryGetValue(k, out timer)) + foreach (long v in this.timeId[k]) { - continue; + Timer timer; + if (!this.timers.TryGetValue(v, out timer)) + { + continue; + } + this.timers.Remove(v); + timer.tcs.SetResult(true); } - this.timers.Remove(k); - timer.tcs.SetResult(true); + + this.timeId.Remove(k); } } @@ -84,6 +94,10 @@ namespace ETModel Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs }; this.timers[timer.Id] = timer; this.timeId.Add(timer.Time, timer.Id); + if (timer.Time < this.minTime) + { + this.minTime = timer.Time; + } cancellationToken.Register(() => { this.Remove(timer.Id); }); return tcs.Task; } @@ -94,6 +108,10 @@ namespace ETModel Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs }; this.timers[timer.Id] = timer; this.timeId.Add(timer.Time, timer.Id); + if (timer.Time < this.minTime) + { + this.minTime = timer.Time; + } return tcs.Task; } @@ -103,6 +121,10 @@ namespace ETModel Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs }; this.timers[timer.Id] = timer; this.timeId.Add(timer.Time, timer.Id); + if (timer.Time < this.minTime) + { + this.minTime = timer.Time; + } cancellationToken.Register(() => { this.Remove(timer.Id); }); return tcs.Task; } @@ -113,6 +135,10 @@ namespace ETModel Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs }; this.timers[timer.Id] = timer; this.timeId.Add(timer.Time, timer.Id); + if (timer.Time < this.minTime) + { + this.minTime = timer.Time; + } return tcs.Task; } }