9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0122-Faster-sequencing-of-futures-for-chunk-structure-gen.patch

79 lines
4.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Tue, 2 Jan 2024 21:13:53 -0500
Subject: [PATCH] Faster sequencing of futures for chunk structure gen
Replace `thenApply` with `thenCompose`. Once one task is completed then the next task starts immediately,
to prevent blocking threads while waiting to complete all tasks. But may cause the sequence of future compose disorder.
diff --git a/net/minecraft/Util.java b/net/minecraft/Util.java
index d1fcc73f579d1c4ac79213ad039c8d803ff51b1a..9918572306e983281d05c6d28c8a5d843348ad2d 100644
--- a/net/minecraft/Util.java
+++ b/net/minecraft/Util.java
@@ -620,17 +620,39 @@ public class Util {
return Maps.transformValues(map, mapper);
}
+ // Leaf start - Faster sequencing of futures for chunk structure gen
public static <V> CompletableFuture<List<V>> sequence(List<? extends CompletableFuture<V>> futures) {
+ return sequence(futures, false);
+ }
+ public static <V> CompletableFuture<List<V>> sequence(List<? extends CompletableFuture<V>> futures, boolean useFaster) {
+ // Leaf end - Faster sequencing of futures for chunk structure gen
if (futures.isEmpty()) {
return CompletableFuture.completedFuture(List.of());
} else if (futures.size() == 1) {
return futures.get(0).thenApply(List::of);
} else {
CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
+ if (useFaster) return sequenceFaster(futures, completableFuture); // Leaf - Faster sequencing of futures for chunk structure gen
return completableFuture.thenApply(_void -> futures.stream().map(CompletableFuture::join).toList());
}
}
+ // Leaf start - Faster sequencing of futures for chunk structure gen
+ private static <V> CompletableFuture<List<V>> sequenceFaster(List<? extends CompletableFuture<V>> futures, CompletableFuture<Void> completableFuture) {
+ return completableFuture.thenCompose($ ->
+ CompletableFuture.supplyAsync(() -> {
+ List<V> list = new java.util.ArrayList<>();
+
+ for (CompletableFuture<V> future : futures) {
+ list.add(future.join());
+ }
+
+ return list;
+ }
+ ));
+ }
+ // Leaf end - Faster sequencing of futures for chunk structure gen
+
public static <V> CompletableFuture<List<V>> sequenceFailFast(List<? extends CompletableFuture<? extends V>> completableFutures) {
CompletableFuture<List<V>> completableFuture = new CompletableFuture<>();
return fallibleSequence(completableFutures, completableFuture::completeExceptionally).applyToEither(completableFuture, Function.identity());
diff --git a/net/minecraft/server/ReloadableServerRegistries.java b/net/minecraft/server/ReloadableServerRegistries.java
index d8c472b8c6aadcaadef14abd8ab43f466e94417e..bc079b6c3d751f2a63d089bf209cf7d8e0da76e8 100644
--- a/net/minecraft/server/ReloadableServerRegistries.java
+++ b/net/minecraft/server/ReloadableServerRegistries.java
@@ -52,7 +52,7 @@ public class ReloadableServerRegistries {
List<CompletableFuture<WritableRegistry<?>>> list1 = LootDataType.values()
.map(lootDataType -> scheduleRegistryLoad((LootDataType<?>)lootDataType, registryOps, resourceManager, backgroundExecutor, conversions)) // Paper
.toList();
- CompletableFuture<List<WritableRegistry<?>>> completableFuture = Util.sequence(list1);
+ CompletableFuture<List<WritableRegistry<?>>> completableFuture = Util.sequence(list1, false); // Leaf - Faster sequencing of futures for chunk structure gen
return completableFuture.thenApplyAsync(
list2 -> createAndValidateFullContext(registryAccess, provider, (List<WritableRegistry<?>>)list2), backgroundExecutor
);
diff --git a/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java b/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java
index 619b98e42e254c0c260c171a26a2472ddf59b885..f07a5416e5dc7e9a798a78ce9573a0c42bc59d04 100644
--- a/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java
+++ b/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java
@@ -255,7 +255,7 @@ public class ChunkGeneratorStructureState {
}
}
- return Util.sequence(list).thenApply(completed -> {
+ return Util.sequence(list, org.dreeam.leaf.config.modules.opt.FasterStructureGenFutureSequencing.enabled).thenApply(completed -> { // Leaf - Faster sequencing of futures for chunk structure gen
double d2 = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS) / 1000.0;
LOGGER.debug("Calculation for {} took {}s", structureSet, d2);
return completed;