More data-structure inlining

This commit is contained in:
Tony Garnock-Jones 2023-10-28 16:09:06 +02:00
parent fd8b445482
commit 7d9174c363
1 changed files with 15 additions and 12 deletions

View File

@ -12,7 +12,9 @@ public class Turn {
private boolean _complete = false;
private Actor _pendingTarget = null;
private Consumer<Turn>[] _pendingQ = null;
private Consumer<Turn> _pending0 = null;
private Consumer<Turn> _pending1 = null;
private Consumer<Turn> _pending2 = null;
private Map<Actor, List<Consumer<Turn>>> _pending = null;
public static void forActor(Actor a, Consumer<Turn> f) {
@ -47,11 +49,13 @@ public class Turn {
_pending = null;
} else if (_pendingTarget != null) {
var ac = _pendingTarget;
var q = _pendingQ;
Consumer<Turn> q0 = _pending0, q1 = _pending1, q2 = _pending2;
_pendingTarget = null;
_pendingQ = null;
_pending0 = _pending1 = _pending2 = null;
ac.execute(() -> Turn.forActor(ac, t -> {
for (var f : q) if (f != null) f.accept(t);
q0.accept(t);
if (q1 != null) q1.accept(t);
if (q2 != null) q2.accept(t);
}));
}
_complete = true;
@ -64,22 +68,21 @@ public class Turn {
_pendingTarget = target;
@SuppressWarnings("unchecked")
var q = (Consumer<Turn>[]) new Consumer<?>[3];
_pendingQ = q;
_pendingQ[0] = action;
_pending0 = action;
return;
}
if (_pendingTarget == target) {
if (_pendingQ[1] == null) { _pendingQ[1] = action; return; }
if (_pendingQ[2] == null) { _pendingQ[2] = action; return; }
if (_pending1 == null) { _pending1 = action; return; }
if (_pending2 == null) { _pending2 = action; return; }
}
_pending = new HashMap<>();
var q = new LinkedList<Consumer<Turn>>();
_pending.put(_pendingTarget, q);
q.add(_pendingQ[0]);
if (_pendingQ[1] != null) q.add(_pendingQ[1]);
if (_pendingQ[2] != null) q.add(_pendingQ[2]);
q.add(_pending0);
if (_pending1 != null) q.add(_pending1);
if (_pending2 != null) q.add(_pending2);
_pendingTarget = null;
_pendingQ = null;
_pending0 = _pending1 = _pending2 = null;
}
_pending.computeIfAbsent(target, k -> new LinkedList<>()).add(action);
}