Performance improvements in several tick methods (#6697)

* Performance improvements in several tick methods

Avoid capturing lambdas, streams, and Set#removeAll

* Update ServerSchematicLoader to not modify activeUploads while iterating in tick

* Replace iterator with enhanced for loop
This commit is contained in:
Qther 2024-07-18 16:24:50 +08:00 committed by GitHub
parent 1b34e52363
commit 1038b76d38
Failed to generate hash of commit
3 changed files with 26 additions and 21 deletions

View file

@ -52,7 +52,8 @@ public class CouplingHandler {
Set<UUID> cartsWithCoupling = CapabilityMinecartController.loadedMinecartsWithCoupling.get(world); Set<UUID> cartsWithCoupling = CapabilityMinecartController.loadedMinecartsWithCoupling.get(world);
if (cartsWithCoupling == null) if (cartsWithCoupling == null)
return; return;
cartsWithCoupling.forEach(id -> {
for (UUID id : cartsWithCoupling) {
MinecartController controller = CapabilityMinecartController.getIfPresent(world, id); MinecartController controller = CapabilityMinecartController.getIfPresent(world, id);
if (controller == null) if (controller == null)
return; return;
@ -63,7 +64,7 @@ public class CouplingHandler {
if (coupledController == null) if (coupledController == null)
return; return;
consumer.accept(Couple.create(controller, coupledController)); consumer.accept(Couple.create(controller, coupledController));
}); };
} }
public static boolean tryToCoupleCarts(@Nullable Player player, Level world, int cartId1, int cartId2) { public static boolean tryToCoupleCarts(@Nullable Player player, Level world, int cartId1, int cartId2) {

View file

@ -1,6 +1,5 @@
package com.simibubi.create.content.contraptions.minecart.capability; package com.simibubi.create.content.contraptions.minecart.capability;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -86,15 +85,16 @@ public class CapabilityMinecartController implements ICapabilitySerializable<Com
} }
public static void tick(Level world) { public static void tick(Level world) {
List<UUID> toRemove = new ArrayList<>();
Map<UUID, MinecartController> carts = loadedMinecartsByUUID.get(world); Map<UUID, MinecartController> carts = loadedMinecartsByUUID.get(world);
List<AbstractMinecart> queued = queuedAdditions.get(world); List<AbstractMinecart> queued = queuedAdditions.get(world);
List<UUID> queuedRemovals = queuedUnloads.get(world); List<UUID> queuedRemovals = queuedUnloads.get(world);
Set<UUID> cartsWithCoupling = loadedMinecartsWithCoupling.get(world); Set<UUID> cartsWithCoupling = loadedMinecartsWithCoupling.get(world);
Set<UUID> keySet = carts.keySet(); Set<UUID> keySet = carts.keySet();
keySet.removeAll(queuedRemovals); for (UUID removal : queuedRemovals) {
cartsWithCoupling.removeAll(queuedRemovals); keySet.remove(removal);
cartsWithCoupling.remove(removal);
}
for (AbstractMinecart cart : queued) { for (AbstractMinecart cart : queued) {
UUID uniqueID = cart.getUUID(); UUID uniqueID = cart.getUUID();
@ -115,10 +115,12 @@ public class CapabilityMinecartController implements ICapabilitySerializable<Com
capability.addListener(new MinecartRemovalListener(world, cart)); capability.addListener(new MinecartRemovalListener(world, cart));
carts.put(uniqueID, controller); carts.put(uniqueID, controller);
capability.ifPresent(mc -> { if (capability.isPresent()) {
if (mc.isLeadingCoupling()) MinecartController mc = capability.orElse(null);
if (mc.isLeadingCoupling()) {
cartsWithCoupling.add(uniqueID); cartsWithCoupling.add(uniqueID);
}); }
}
if (!world.isClientSide && controller != null) if (!world.isClientSide && controller != null)
controller.sendData(); controller.sendData();
} }
@ -134,11 +136,9 @@ public class CapabilityMinecartController implements ICapabilitySerializable<Com
continue; continue;
} }
} }
toRemove.add(entry.getKey()); cartsWithCoupling.remove(entry.getKey());
keySet.remove(entry.getKey());
} }
cartsWithCoupling.removeAll(toRemove);
keySet.removeAll(toRemove);
} }
public static void onChunkUnloaded(ChunkEvent.Unload event) { public static void onChunkUnloaded(ChunkEvent.Unload event) {

View file

@ -11,7 +11,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
@ -25,6 +24,7 @@ import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.infrastructure.config.AllConfigs; import com.simibubi.create.infrastructure.config.AllConfigs;
import com.simibubi.create.infrastructure.config.CSchematics; import com.simibubi.create.infrastructure.config.CSchematics;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.Util; import net.minecraft.Util;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -64,21 +64,25 @@ public class ServerSchematicLoader {
return "schematics/uploaded"; return "schematics/uploaded";
} }
private final ObjectArrayList<String> deadEntries = ObjectArrayList.of();
public void tick() { public void tick() {
// Detect Timed out Uploads // Detect Timed out Uploads
Set<String> deadEntries = new HashSet<>(); int timeout = getConfig().schematicIdleTimeout.get();
for (String upload : activeUploads.keySet()) { for (String upload : activeUploads.keySet()) {
SchematicUploadEntry entry = activeUploads.get(upload); SchematicUploadEntry entry = activeUploads.get(upload);
if (entry.idleTime++ > getConfig().schematicIdleTimeout.get()) { if (entry.idleTime++ > timeout) {
Create.LOGGER.warn("Schematic Upload timed out: " + upload); Create.LOGGER.warn("Schematic Upload timed out: " + upload);
deadEntries.add(upload); deadEntries.add(upload);
} }
} }
// Remove Timed out Uploads // Remove Timed out Uploads
deadEntries.forEach(this::cancelUpload); for (String toRemove : deadEntries) {
this.cancelUpload(toRemove);
}
deadEntries.clear();
} }
public void shutdown() { public void shutdown() {