use default initial capacity when creating event coroutine collection

This commit is contained in:
Ell 2021-03-20 21:21:05 +01:00
parent 1e8a9e4e6c
commit 19e8a74d2b

View file

@ -96,7 +96,7 @@ namespace Coroutine {
/// <param name="evt">The event to raise</param> /// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) { public void RaiseEvent(Event evt) {
this.MoveOutstandingCoroutines(); this.MoveOutstandingCoroutines();
var coroutines = this.GetEventCoroutines(evt, 0); var coroutines = this.GetEventCoroutines(evt, false);
if (coroutines != null) { if (coroutines != null) {
for (var i = 0; i < coroutines.Count; i++) { for (var i = 0; i < coroutines.Count; i++) {
var c = coroutines[i]; var c = coroutines[i];
@ -123,20 +123,20 @@ namespace Coroutine {
private void MoveOutstandingCoroutines() { private void MoveOutstandingCoroutines() {
// RemoveWhere is twice as fast as iterating and then clearing // RemoveWhere is twice as fast as iterating and then clearing
this.eventCoroutinesToRemove.RemoveWhere(c => { this.eventCoroutinesToRemove.RemoveWhere(c => {
this.GetEventCoroutines(c.Event, 0).Remove(c); this.GetEventCoroutines(c.Event, false).Remove(c);
return true; return true;
}); });
this.outstandingCoroutines.RemoveWhere(c => { this.outstandingCoroutines.RemoveWhere(c => {
var list = c.IsWaitingForEvent ? this.GetEventCoroutines(c.Event, 1) : this.tickingCoroutines; var list = c.IsWaitingForEvent ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines;
var position = list.BinarySearch(c); var position = list.BinarySearch(c);
list.Insert(position < 0 ? ~position : position, c); list.Insert(position < 0 ? ~position : position, c);
return true; return true;
}); });
} }
private List<ActiveCoroutine> GetEventCoroutines(Event evt, int capacity) { private List<ActiveCoroutine> GetEventCoroutines(Event evt, bool create) {
if (!this.eventCoroutines.TryGetValue(evt, out var ret) && capacity > 0) { if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) {
ret = new List<ActiveCoroutine>(capacity); ret = new List<ActiveCoroutine>();
this.eventCoroutines.Add(evt, ret); this.eventCoroutines.Add(evt, ret);
} }
return ret; return ret;