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
1 changed files with 13 additions and 10 deletions

View File

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