From 1038b76d38f2bb4534eb86a7355306aed0b77d4c Mon Sep 17 00:00:00 2001 From: Qther Date: Thu, 18 Jul 2024 16:24:50 +0800 Subject: [PATCH] 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 --- .../minecart/CouplingHandler.java | 11 +++++----- .../CapabilityMinecartController.java | 22 +++++++++---------- .../schematics/ServerSchematicLoader.java | 14 +++++++----- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/minecart/CouplingHandler.java b/src/main/java/com/simibubi/create/content/contraptions/minecart/CouplingHandler.java index 22abc9b40..b6a83a60a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/minecart/CouplingHandler.java +++ b/src/main/java/com/simibubi/create/content/contraptions/minecart/CouplingHandler.java @@ -45,14 +45,15 @@ public class CouplingHandler { event.setResult(Result.DENY); } } - + public static void forEachLoadedCoupling(Level world, Consumer> consumer) { if (world == null) return; Set cartsWithCoupling = CapabilityMinecartController.loadedMinecartsWithCoupling.get(world); if (cartsWithCoupling == null) return; - cartsWithCoupling.forEach(id -> { + + for (UUID id : cartsWithCoupling) { MinecartController controller = CapabilityMinecartController.getIfPresent(world, id); if (controller == null) return; @@ -63,7 +64,7 @@ public class CouplingHandler { if (coupledController == null) return; consumer.accept(Couple.create(controller, coupledController)); - }); + }; } public static boolean tryToCoupleCarts(@Nullable Player player, Level world, int cartId1, int cartId2) { @@ -83,13 +84,13 @@ public class CouplingHandler { int distanceTo = (int) entity1.position() .distanceTo(entity2.position()); boolean contraptionCoupling = player == null; - + if (distanceTo < 2) { if (contraptionCoupling) return false; // dont allow train contraptions with <2 distance distanceTo = 2; } - + if (distanceTo > AllConfigs.server().kinetics.maxCartCouplingLength.get()) { status(player, tooFar); return false; diff --git a/src/main/java/com/simibubi/create/content/contraptions/minecart/capability/CapabilityMinecartController.java b/src/main/java/com/simibubi/create/content/contraptions/minecart/capability/CapabilityMinecartController.java index 2f185ffb1..0bc0538d6 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/minecart/capability/CapabilityMinecartController.java +++ b/src/main/java/com/simibubi/create/content/contraptions/minecart/capability/CapabilityMinecartController.java @@ -1,6 +1,5 @@ package com.simibubi.create.content.contraptions.minecart.capability; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -86,15 +85,16 @@ public class CapabilityMinecartController implements ICapabilitySerializable toRemove = new ArrayList<>(); Map carts = loadedMinecartsByUUID.get(world); List queued = queuedAdditions.get(world); List queuedRemovals = queuedUnloads.get(world); Set cartsWithCoupling = loadedMinecartsWithCoupling.get(world); Set keySet = carts.keySet(); - keySet.removeAll(queuedRemovals); - cartsWithCoupling.removeAll(queuedRemovals); + for (UUID removal : queuedRemovals) { + keySet.remove(removal); + cartsWithCoupling.remove(removal); + } for (AbstractMinecart cart : queued) { UUID uniqueID = cart.getUUID(); @@ -115,10 +115,12 @@ public class CapabilityMinecartController implements ICapabilitySerializable { - if (mc.isLeadingCoupling()) + if (capability.isPresent()) { + MinecartController mc = capability.orElse(null); + if (mc.isLeadingCoupling()) { cartsWithCoupling.add(uniqueID); - }); + } + } if (!world.isClientSide && controller != null) controller.sendData(); } @@ -134,11 +136,9 @@ public class CapabilityMinecartController implements ICapabilitySerializable deadEntries = ObjectArrayList.of(); + public void tick() { // Detect Timed out Uploads - Set deadEntries = new HashSet<>(); + int timeout = getConfig().schematicIdleTimeout.get(); for (String upload : activeUploads.keySet()) { SchematicUploadEntry entry = activeUploads.get(upload); - if (entry.idleTime++ > getConfig().schematicIdleTimeout.get()) { + if (entry.idleTime++ > timeout) { Create.LOGGER.warn("Schematic Upload timed out: " + upload); deadEntries.add(upload); } - } // Remove Timed out Uploads - deadEntries.forEach(this::cancelUpload); + for (String toRemove : deadEntries) { + this.cancelUpload(toRemove); + } + deadEntries.clear(); } public void shutdown() {