From 9d641c89eb4db2e67bfbae62c5cf1e258cbb17ca Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Mon, 23 Dec 2024 15:03:38 +0100 Subject: [PATCH] Reduce number of network events during init to improve performance, #1439 --- .../api/part/write/IPartStateWriter.java | 10 ++++++---- .../integrateddynamics/core/network/Network.java | 4 ++++ .../part/panel/PartTypePanelVariableDriven.java | 14 -------------- .../core/part/write/PartStateWriterBase.java | 9 +++++++-- .../core/part/write/PartTypeWriteBase.java | 12 ++++++------ 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/cyclops/integrateddynamics/api/part/write/IPartStateWriter.java b/src/main/java/org/cyclops/integrateddynamics/api/part/write/IPartStateWriter.java index 8fdf8d74045..ca09dbeb0a0 100644 --- a/src/main/java/org/cyclops/integrateddynamics/api/part/write/IPartStateWriter.java +++ b/src/main/java/org/cyclops/integrateddynamics/api/part/write/IPartStateWriter.java @@ -45,11 +45,13 @@ public interface IPartStateWriter

extends IPartState< /** * Indicate that this state should eventually recheck its aspect info because something might have changed what can * cause the active variable to be referring to something else. - * @param partType The part type. - * @param target The target. - * @param newAspect The new active aspect, can be null. + * + * @param partType The part type. + * @param target The target. + * @param newAspect The new active aspect, can be null. + * @param isNetworkInitializing If this method is being invoked during network (de)initializing. */ - public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect); + public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect, boolean isNetworkInitializing); /** * Called when this part should refresh its state. diff --git a/src/main/java/org/cyclops/integrateddynamics/core/network/Network.java b/src/main/java/org/cyclops/integrateddynamics/core/network/Network.java index 0003cae9389..34b04e8c3ae 100644 --- a/src/main/java/org/cyclops/integrateddynamics/core/network/Network.java +++ b/src/main/java/org/cyclops/integrateddynamics/core/network/Network.java @@ -34,6 +34,7 @@ import org.cyclops.integrateddynamics.core.network.event.NetworkElementAddEvent; import org.cyclops.integrateddynamics.core.network.event.NetworkElementRemoveEvent; import org.cyclops.integrateddynamics.core.network.event.NetworkEventBus; +import org.cyclops.integrateddynamics.core.network.event.VariableContentsUpdatedEvent; import org.cyclops.integrateddynamics.core.path.Cluster; import org.cyclops.integrateddynamics.core.path.PathFinder; import org.cyclops.integrateddynamics.core.persist.world.NetworkWorldStorage; @@ -322,6 +323,9 @@ protected void initialize(boolean silent) { } element.afterNetworkReAlive(this); } + + // Once all elements are alive, send a single variable contents updated event. + this.getEventBus().post(new VariableContentsUpdatedEvent(this)); } @Override diff --git a/src/main/java/org/cyclops/integrateddynamics/core/part/panel/PartTypePanelVariableDriven.java b/src/main/java/org/cyclops/integrateddynamics/core/part/panel/PartTypePanelVariableDriven.java index 358222d2e0a..3ba8e634f01 100644 --- a/src/main/java/org/cyclops/integrateddynamics/core/part/panel/PartTypePanelVariableDriven.java +++ b/src/main/java/org/cyclops/integrateddynamics/core/part/panel/PartTypePanelVariableDriven.java @@ -50,7 +50,6 @@ import org.cyclops.integrateddynamics.core.helper.NetworkHelpers; import org.cyclops.integrateddynamics.core.helper.PartHelpers; import org.cyclops.integrateddynamics.core.helper.WrenchHelpers; -import org.cyclops.integrateddynamics.core.network.event.NetworkElementAddEvent; import org.cyclops.integrateddynamics.core.network.event.VariableContentsUpdatedEvent; import org.cyclops.integrateddynamics.core.part.PartStateActiveVariableBase; import org.cyclops.integrateddynamics.core.part.PartTypeBase; @@ -82,7 +81,6 @@ protected Map, IEventAction> constructNetworkEven IEventAction updateEventListener = (network, target, state, event) -> NetworkHelpers .getPartNetwork(network).ifPresent(partNetwork -> onVariableContentsUpdated(partNetwork, target, state)); actions.put(VariableContentsUpdatedEvent.class, updateEventListener); - actions.put(NetworkElementAddEvent.Post.class, updateEventListener); return actions; } @@ -99,18 +97,6 @@ public void addDrops(PartTarget target, S state, List itemStacks, boo super.addDrops(target, state, itemStacks, dropMainElement, saveState); } - @Override - public void beforeNetworkKill(INetwork network, IPartNetwork partNetwork, PartTarget target, S state) { - super.beforeNetworkKill(network, partNetwork, target, state); - state.onVariableContentsUpdated((P) this, target); - } - - @Override - public void afterNetworkAlive(INetwork network, IPartNetwork partNetwork, PartTarget target, S state) { - super.afterNetworkAlive(network, partNetwork, target, state); - state.onVariableContentsUpdated((P) this, target); - } - @Override public boolean isUpdate(S state) { return true; diff --git a/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartStateWriterBase.java b/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartStateWriterBase.java index 0d91a1ef839..4e702fecd37 100644 --- a/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartStateWriterBase.java +++ b/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartStateWriterBase.java @@ -75,8 +75,13 @@ public boolean hasVariable() { } @Override - public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect) { - onVariableContentsUpdated(partType, target); + public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect, boolean isNetworkInitializing) { + if (!isNetworkInitializing) { + // We skip network content updates during network init, + // as it will be called once for all parts right after network init. + // This is to avoid re-updating variable contents many times during network init, which can get expensive. + onVariableContentsUpdated(partType, target); + } IAspectWrite activeAspect = getActiveAspect(); if(activeAspect != null && activeAspect != newAspect) { activeAspect.onDeactivate(partType, target, this); diff --git a/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartTypeWriteBase.java b/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartTypeWriteBase.java index cd28c2d2a38..62c209cc0e4 100644 --- a/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartTypeWriteBase.java +++ b/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartTypeWriteBase.java @@ -37,7 +37,6 @@ import org.cyclops.integrateddynamics.core.helper.L10NValues; import org.cyclops.integrateddynamics.core.helper.NetworkHelpers; import org.cyclops.integrateddynamics.core.helper.PartHelpers; -import org.cyclops.integrateddynamics.core.network.event.NetworkElementAddEvent; import org.cyclops.integrateddynamics.core.network.event.VariableContentsUpdatedEvent; import org.cyclops.integrateddynamics.core.part.PartTypeAspects; import org.cyclops.integrateddynamics.core.part.PartTypeBase; @@ -73,7 +72,6 @@ protected Map, IEventAction> constructNetworkEven IEventAction updateEventListener = (network, target, state, event) -> NetworkHelpers .getPartNetwork(network).ifPresent(partNetwork -> onVariableContentsUpdated(partNetwork, target, state)); actions.put(VariableContentsUpdatedEvent.class, updateEventListener); - actions.put(NetworkElementAddEvent.Post.class, updateEventListener); return actions; } @@ -113,14 +111,14 @@ public void addDrops(PartTarget target, S state, List itemStacks, boo } } state.getInventory().clearContent(); - state.triggerAspectInfoUpdate((P) this, target, null); + state.triggerAspectInfoUpdate((P) this, target, null, false); super.addDrops(target, state, itemStacks, dropMainElement, saveState); } @Override public void beforeNetworkKill(INetwork network, IPartNetwork partNetwork, PartTarget target, S state) { super.beforeNetworkKill(network, partNetwork, target, state); - state.triggerAspectInfoUpdate((P) this, target, null); + state.triggerAspectInfoUpdate((P) this, target, null, true); } @Override @@ -154,6 +152,8 @@ public IAspectWrite getActiveAspect(PartTarget target, S partState) { @Override public void updateActivation(PartTarget target, S partState, @Nullable Player player) { + boolean isNetworkInitializing = player == null; // TODO: in next major, also add isNetworkInitializing param to updateActivation so we don't need this hack! + // Check inside the inventory for a variable item and determine everything with that. int activeIndex = -1; for(int i = 0 ; i < partState.getInventory().getContainerSize(); i++) { @@ -163,7 +163,7 @@ public void updateActivation(PartTarget target, S partState, @Nullable Player pl } } IAspectWrite aspect = activeIndex == -1 ? null : getWriteAspects().get(activeIndex); - partState.triggerAspectInfoUpdate((P) this, target, aspect); + partState.triggerAspectInfoUpdate((P) this, target, aspect, isNetworkInitializing); INetwork network = NetworkHelpers.getNetwork(target.getCenter()).orElse(null); if (network != null && aspect != null) { @@ -173,7 +173,7 @@ public void updateActivation(PartTarget target, S partState, @Nullable Player pl partState, player, aspect, partState.getInventory().getItem(activeIndex))); } } - if (network != null) { + if (network != null && !isNetworkInitializing) { network.getEventBus().post(new VariableContentsUpdatedEvent(network)); } }