split outstanding coroutines into two lists to improve performance slightly

This commit is contained in:
Ell 2021-03-21 01:58:16 +01:00
parent 4280ea2db4
commit 0de8a0dd2d

View file

@ -13,7 +13,8 @@ namespace Coroutine {
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>(); private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
private readonly Dictionary<Event, List<ActiveCoroutine>> eventCoroutines = new Dictionary<Event, List<ActiveCoroutine>>(); private readonly Dictionary<Event, List<ActiveCoroutine>> eventCoroutines = new Dictionary<Event, List<ActiveCoroutine>>();
private readonly HashSet<ActiveCoroutine> eventCoroutinesToRemove = new HashSet<ActiveCoroutine>(); private readonly HashSet<ActiveCoroutine> eventCoroutinesToRemove = new HashSet<ActiveCoroutine>();
private readonly HashSet<ActiveCoroutine> outstandingCoroutines = new HashSet<ActiveCoroutine>(); private readonly HashSet<ActiveCoroutine> outstandingEventCoroutines = new HashSet<ActiveCoroutine>();
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
private readonly Stopwatch stopwatch = new Stopwatch(); private readonly Stopwatch stopwatch = new Stopwatch();
/// <summary> /// <summary>
@ -47,7 +48,7 @@ namespace Coroutine {
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) { public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch); var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch);
if (inst.MoveNext()) if (inst.MoveNext())
this.outstandingCoroutines.Add(inst); this.GetOutstandingCoroutines(inst.IsWaitingForEvent).Add(inst);
return inst; return inst;
} }
@ -74,7 +75,7 @@ namespace Coroutine {
if (c.Tick(deltaSeconds)) { if (c.Tick(deltaSeconds)) {
return true; return true;
} else if (c.IsWaitingForEvent) { } else if (c.IsWaitingForEvent) {
this.outstandingCoroutines.Add(c); this.outstandingEventCoroutines.Add(c);
return true; return true;
} }
return false; return false;
@ -106,7 +107,7 @@ namespace Coroutine {
this.eventCoroutinesToRemove.Add(c); this.eventCoroutinesToRemove.Add(c);
} else if (!c.IsWaitingForEvent) { } else if (!c.IsWaitingForEvent) {
this.eventCoroutinesToRemove.Add(c); this.eventCoroutinesToRemove.Add(c);
this.outstandingCoroutines.Add(c); this.outstandingTickingCoroutines.Add(c);
} }
} }
} }
@ -128,12 +129,10 @@ namespace Coroutine {
return true; return true;
}); });
} }
if (this.outstandingCoroutines.Count > 0) { var coroutines = this.GetOutstandingCoroutines(evt);
this.outstandingCoroutines.RemoveWhere(c => { if (coroutines.Count > 0) {
// we only want to enqueue coroutines that relate to the current type coroutines.RemoveWhere(c => {
if (c.IsWaitingForEvent != evt) var list = c.IsWaitingForEvent ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines;
return false;
var list = evt ? 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;
@ -141,6 +140,10 @@ namespace Coroutine {
} }
} }
private HashSet<ActiveCoroutine> GetOutstandingCoroutines(bool evt) {
return evt ? this.outstandingEventCoroutines : this.outstandingTickingCoroutines;
}
private List<ActiveCoroutine> GetEventCoroutines(Event evt, bool create) { private List<ActiveCoroutine> GetEventCoroutines(Event evt, bool create) {
if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) { if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) {
ret = new List<ActiveCoroutine>(); ret = new List<ActiveCoroutine>();