Skip to content

Commit

Permalink
Reduce number of network events during init to improve performance, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 23, 2024
1 parent b05b672 commit 9d641c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public interface IPartStateWriter<P extends IPartTypeWriter> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +81,6 @@ protected Map<Class<? extends INetworkEvent>, IEventAction> constructNetworkEven
IEventAction<P, S, INetworkEvent> 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;
}

Expand All @@ -99,18 +97,6 @@ public void addDrops(PartTarget target, S state, List<ItemStack> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +72,6 @@ protected Map<Class<? extends INetworkEvent>, IEventAction> constructNetworkEven
IEventAction<P, S, INetworkEvent> 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;
}

Expand Down Expand Up @@ -113,14 +111,14 @@ public void addDrops(PartTarget target, S state, List<ItemStack> 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
Expand Down Expand Up @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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));
}
}
Expand Down

0 comments on commit 9d641c8

Please sign in to comment.