From 5401d2d8e9def937a73f540ea0e4e7fd05cdcee8 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 28 Oct 2023 15:45:27 +0200 Subject: [PATCH] Reduce allocation slightly further --- .../java/org/syndicate_lang/actors/Turn.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/syndicate_lang/actors/Turn.java b/src/main/java/org/syndicate_lang/actors/Turn.java index 06e3ecd..3105820 100644 --- a/src/main/java/org/syndicate_lang/actors/Turn.java +++ b/src/main/java/org/syndicate_lang/actors/Turn.java @@ -8,10 +8,9 @@ import java.util.logging.Logger; public class Turn { private final static AtomicLong nextHandle = new AtomicLong(0); - private final static record SinglePending(Actor ac, List> actions) {} - private final Actor _actor; - private SinglePending _singlePending = null; + private Actor _pendingTarget = null; + private List> _pendingQ = null; private Map>> _pending = null; private boolean _complete = false; @@ -45,10 +44,11 @@ public class Turn { if (_pending != null) { _pending.forEach((ac, q) -> ac.execute(() -> Turn.forActor(ac, t -> q.forEach(f -> f.accept(t))))); _pending = null; - } else if (_singlePending != null) { - var ac = _singlePending.ac(); - var q = _singlePending.actions(); - _singlePending = null; + } else if (_pendingTarget != null) { + var ac = _pendingTarget; + var q = _pendingQ; + _pendingTarget = null; + _pendingQ = null; ac.execute(() -> Turn.forActor(ac, t -> q.forEach(f -> f.accept(t)))); } _complete = true; @@ -63,17 +63,19 @@ public class Turn { private void enqueue(Actor target, Consumer action) { if (_complete) throw new IllegalStateException("Attempt to reuse a committed Turn"); if (_pending == null) { - if (_singlePending == null) { - _singlePending = new SinglePending(target, singletonList(action)); + if (_pendingTarget == null) { + _pendingTarget = target; + _pendingQ = singletonList(action); return; } - if (_singlePending.ac() == target) { - _singlePending.actions().add(action); + if (_pendingTarget == target) { + _pendingQ.add(action); return; } _pending = new HashMap<>(); - _pending.put(_singlePending.ac(), _singlePending.actions()); - _singlePending = null; + _pending.put(_pendingTarget, _pendingQ); + _pendingTarget = null; + _pendingQ = null; } _pending.computeIfAbsent(target, k -> new LinkedList<>()).add(action); }