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/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java index 0e38a641d8e537750166b56c57aca4a90d418af1..815253d03b85a7a476c1efdeca9496fd64afc137 100644 --- a/src/main/java/net/minecraft/Util.java +++ b/src/main/java/net/minecraft/Util.java @@ -490,13 +490,27 @@ public class Util { return object; } + // Leaf start - Faster sequencing of futures for chunk structure gen public static CompletableFuture> sequence(List> futures) { + return sequence(futures, false); + } + // Leaf end - Faster sequencing of futures for chunk structure gen + + public static CompletableFuture> sequence(List> futures, boolean useFaster) { if (futures.isEmpty()) { return CompletableFuture.completedFuture(List.of()); } else if (futures.size() == 1) { return futures.get(0).thenApply(List::of); } else { CompletableFuture completableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); + // Leaf start - Faster sequencing of futures for chunk structure gen + if (org.dreeam.leaf.config.modules.opt.FasterStructureGenFutureSequencing.enabled && useFaster) { + return completableFuture.thenCompose(void_ -> + CompletableFuture.supplyAsync(() -> + futures.stream().map(CompletableFuture::join).toList())); + } + // Leaf end + return completableFuture.thenApply(void_ -> futures.stream().map(CompletableFuture::join).toList()); } } diff --git a/src/main/java/net/minecraft/server/ReloadableServerRegistries.java b/src/main/java/net/minecraft/server/ReloadableServerRegistries.java index 908431652a0fea79b5a0cee1efd0c7a7d524b614..f4386490897549c898c0755e82bac3b387479e93 100644 --- a/src/main/java/net/minecraft/server/ReloadableServerRegistries.java +++ b/src/main/java/net/minecraft/server/ReloadableServerRegistries.java @@ -51,7 +51,7 @@ public class ReloadableServerRegistries { List>> list = LootDataType.values() .map(type -> scheduleElementParse((LootDataType)type, registryOps, resourceManager, prepareExecutor, conversions)) // Paper .toList(); - CompletableFuture>> completableFuture = Util.sequence(list); + CompletableFuture>> completableFuture = Util.sequence(list, false); // Leaf - Faster sequencing of futures for chunk structure gen return completableFuture.thenApplyAsync(registries -> apply(dynamicRegistries, (List>)registries), prepareExecutor); } diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java b/src/main/java/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java index a6b6e5ea191c0e2cd7a2e4f01b89d8af40a83c1b..713fced29fbd819ee6f151c7f3e462f58a21d5e6 100644 --- a/src/main/java/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java +++ b/src/main/java/net/minecraft/world/level/chunk/ChunkGeneratorStructureState.java @@ -270,7 +270,7 @@ public class ChunkGeneratorStructureState { } } - return Util.sequence(list).thenApply((list1) -> { + return Util.sequence(list, true).thenApply((list1) -> { // Leaf - Faster sequencing of futures for chunk structure gen double d2 = (double) stopwatch.stop().elapsed(TimeUnit.MILLISECONDS) / 1000.0D; ChunkGeneratorStructureState.LOGGER.debug("Calculation for {} took {}s", structureSetEntry, d2); diff --git a/src/main/java/org/dreeam/leaf/config/modules/opt/FasterStructureGenFutureSequencing.java b/src/main/java/org/dreeam/leaf/config/modules/opt/FasterStructureGenFutureSequencing.java new file mode 100644 index 0000000000000000000000000000000000000000..44e1e25cc0de1d9de6ba9b66ec7502cd91f92c7b --- /dev/null +++ b/src/main/java/org/dreeam/leaf/config/modules/opt/FasterStructureGenFutureSequencing.java @@ -0,0 +1,19 @@ +package org.dreeam.leaf.config.modules.opt; + +import org.dreeam.leaf.config.ConfigModules; +import org.dreeam.leaf.config.EnumConfigCategory; + +public class FasterStructureGenFutureSequencing extends ConfigModules { + + public String getBasePath() { + return EnumConfigCategory.PERF.getBaseKeyName(); + } + + public static boolean enabled = true; + + @Override + public void onLoaded() { + enabled = config.getBoolean(getBasePath() + ".faster-structure-gen-future-sequencing", enabled, + "May cause the inconsistent order of future compose tasks."); + } +}