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

@ -45,14 +45,15 @@ public class CouplingHandler {
event.setResult(Result.DENY);
}
}
public static void forEachLoadedCoupling(Level world, Consumer<Couple<MinecartController>> consumer) {
if (world == null)
return;
Set<UUID> 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;

View file

@ -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<Com
}
public static void tick(Level world) {
List<UUID> toRemove = new ArrayList<>();
Map<UUID, MinecartController> carts = loadedMinecartsByUUID.get(world);
List<AbstractMinecart> queued = queuedAdditions.get(world);
List<UUID> queuedRemovals = queuedUnloads.get(world);
Set<UUID> cartsWithCoupling = loadedMinecartsWithCoupling.get(world);
Set<UUID> 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<Com
capability.addListener(new MinecartRemovalListener(world, cart));
carts.put(uniqueID, controller);
capability.ifPresent(mc -> {
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<Com
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) {

View file

@ -11,7 +11,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
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.CSchematics;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
@ -64,21 +64,25 @@ public class ServerSchematicLoader {
return "schematics/uploaded";
}
private final ObjectArrayList<String> deadEntries = ObjectArrayList.of();
public void tick() {
// Detect Timed out Uploads
Set<String> 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() {