From 089b919ea7ffcc4e96e3b0e7e6dfc2d3a2588f9d Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Sun, 25 Aug 2024 19:05:12 -0500 Subject: [PATCH] Perform a few more optimizations to ConfigHolder#getSlotInfo: - Don't check if we have a cached slot info if we just cleared the cache due to the side changing - Lazily cache the ConfigInfo object as that doesn't change and allows us to skip a handful of checks on subsequent runs --- .../capabilities/holder/ConfigHolder.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/mekanism/common/capabilities/holder/ConfigHolder.java b/src/main/java/mekanism/common/capabilities/holder/ConfigHolder.java index d0d26f47671..960a93d4079 100644 --- a/src/main/java/mekanism/common/capabilities/holder/ConfigHolder.java +++ b/src/main/java/mekanism/common/capabilities/holder/ConfigHolder.java @@ -62,7 +62,10 @@ public String toString() { protected final List slots = new ArrayList<>(); @Nullable private Direction lastDirection; - private boolean listenerAdded; + + @Nullable + private ConfigInfo lazyConfig; + private boolean retrievedConfig; protected ConfigHolder(ISideConfiguration sideConfiguration) { this.sideConfiguration = sideConfiguration; @@ -115,30 +118,31 @@ private ISlotInfo getSlotInfo(Direction side) { //Invalid entire cache and update what direction we had as last if our last direction doesn't match the one we currently are facing cachedSlotInfo.clear(); lastDirection = direction; - } - if (cachedSlotInfo.containsKey(side)) { + } else if (cachedSlotInfo.containsKey(side)) { + //If the direction changed, we know it isn't cached so can skip checking return cachedSlotInfo.get(side); } - ISlotInfo slotInfo; - TileComponentConfig config = sideConfiguration.getConfig(); - if (config == null) { - slotInfo = NO_CONFIG; - } else { - TransmissionType transmissionType = getTransmissionType(); - ConfigInfo configInfo = config.getConfig(transmissionType); - if (configInfo == null) { - slotInfo = NO_CONFIG; - } else { - if (!listenerAdded) { + if (!retrievedConfig) { + retrievedConfig = true; + TileComponentConfig config = sideConfiguration.getConfig(); + if (config != null) { + TransmissionType transmissionType = getTransmissionType(); + lazyConfig = config.getConfig(transmissionType); + if (lazyConfig != null) { //If we haven't added a listener to our config yet add one to remove the cached info we have for that side - listenerAdded = true; + //Note: We know we haven't done this if we haven't retrieved the config yet, as we only retrieve the config a single time config.addConfigChangeListener(transmissionType, cachedSlotInfo::remove); } - slotInfo = configInfo.getSlotInfo(RelativeSide.fromDirections(direction, side)); - if (slotInfo != null && !slotInfo.isEnabled()) { - //If we have a slot info, but it is not actually enabled, just store it as null to avoid having to recheck if it is enabled later - slotInfo = null; - } + } + } + ISlotInfo slotInfo; + if (lazyConfig == null) { + slotInfo = NO_CONFIG; + } else { + slotInfo = lazyConfig.getSlotInfo(RelativeSide.fromDirections(direction, side)); + if (slotInfo != null && !slotInfo.isEnabled()) { + //If we have a slot info, but it is not actually enabled, just store it as null to avoid having to recheck if it is enabled later + slotInfo = null; } } cachedSlotInfo.put(side, slotInfo);