mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-02 21:22:05 -07:00
Async command map building
This adds a custom pool inorder to make sure that they are closed without much though, as it doesn't matter if the client is not sent commands if the server is restarting. Using the default async pool caused issues to arise due to the shutdown logic generally being much later.
This commit is contained in:
@@ -188,7 +188,7 @@
|
||||
}
|
||||
} else {
|
||||
callback.accept(executioncontext);
|
||||
@@ -377,11 +452,37 @@
|
||||
@@ -377,22 +452,77 @@
|
||||
}
|
||||
|
||||
public void sendCommands(ServerPlayer player) {
|
||||
@@ -196,6 +196,25 @@
|
||||
+ if ( org.spigotmc.SpigotConfig.tabComplete < 0 ) return; // Spigot
|
||||
+ // CraftBukkit start
|
||||
+ // Register Vanilla commands into builtRoot as before
|
||||
+ // Paper start - Perf: Async command map building
|
||||
+ // Copy root children to avoid concurrent modification during building
|
||||
+ final Collection<CommandNode<CommandSourceStack>> commandNodes = new java.util.ArrayList<>(this.dispatcher.getRoot().getChildren());
|
||||
+ COMMAND_SENDING_POOL.execute(() -> this.sendAsync(player, commandNodes));
|
||||
+ }
|
||||
+
|
||||
+ // Fixed pool, but with discard policy
|
||||
+ public static final java.util.concurrent.ExecutorService COMMAND_SENDING_POOL = new java.util.concurrent.ThreadPoolExecutor(
|
||||
+ 2, 2, 0, java.util.concurrent.TimeUnit.MILLISECONDS,
|
||||
+ new java.util.concurrent.LinkedBlockingQueue<>(),
|
||||
+ new com.google.common.util.concurrent.ThreadFactoryBuilder()
|
||||
+ .setNameFormat("Paper Async Command Builder Thread Pool - %1$d")
|
||||
+ .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER))
|
||||
+ .build(),
|
||||
+ new java.util.concurrent.ThreadPoolExecutor.DiscardPolicy()
|
||||
+ );
|
||||
+
|
||||
+ private void sendAsync(ServerPlayer player, Collection<CommandNode<CommandSourceStack>> dispatcherRootChildren) {
|
||||
+ // Paper end - Perf: Async command map building
|
||||
+ Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> map = Maps.newIdentityHashMap(); // Use identity to prevent aliasing issues
|
||||
+ RootCommandNode vanillaRoot = new RootCommandNode();
|
||||
+
|
||||
@@ -207,13 +226,21 @@
|
||||
RootCommandNode<SharedSuggestionProvider> rootcommandnode = new RootCommandNode();
|
||||
|
||||
map.put(this.dispatcher.getRoot(), rootcommandnode);
|
||||
this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, player.createCommandSourceStack(), map);
|
||||
- this.fillUsableCommands(this.dispatcher.getRoot(), rootcommandnode, player.createCommandSourceStack(), map);
|
||||
+ this.fillUsableCommands(dispatcherRootChildren, rootcommandnode, player.createCommandSourceStack(), map); // Paper - Perf: Async command map building; pass copy of children
|
||||
+
|
||||
+ Collection<String> bukkit = new LinkedHashSet<>();
|
||||
+ for (CommandNode node : rootcommandnode.getChildren()) {
|
||||
+ bukkit.add(node.getName());
|
||||
+ }
|
||||
+ // Paper start - Perf: Async command map building
|
||||
+ net.minecraft.server.MinecraftServer.getServer().execute(() -> {
|
||||
+ runSync(player, bukkit, rootcommandnode);
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ private void runSync(ServerPlayer player, Collection<String> bukkit, RootCommandNode<SharedSuggestionProvider> rootcommandnode) {
|
||||
+ // Paper end - Perf: Async command map building
|
||||
+ PlayerCommandSendEvent event = new PlayerCommandSendEvent(player.getBukkitEntity(), new LinkedHashSet<>(bukkit));
|
||||
+ event.getPlayer().getServer().getPluginManager().callEvent(event);
|
||||
+
|
||||
@@ -227,7 +254,12 @@
|
||||
player.connection.send(new ClientboundCommandsPacket(rootcommandnode));
|
||||
}
|
||||
|
||||
@@ -390,9 +491,10 @@
|
||||
- private void fillUsableCommands(CommandNode<CommandSourceStack> tree, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
|
||||
- Iterator iterator = tree.getChildren().iterator();
|
||||
+ // Paper start - Perf: Async command map building; pass copy of children
|
||||
+ private void fillUsableCommands(Collection<CommandNode<CommandSourceStack>> children, CommandNode<SharedSuggestionProvider> result, CommandSourceStack source, Map<CommandNode<CommandSourceStack>, CommandNode<SharedSuggestionProvider>> resultNodes) {
|
||||
+ Iterator iterator = children.iterator();
|
||||
+ // Paper end - Perf: Async command map building
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
CommandNode<CommandSourceStack> commandnode2 = (CommandNode) iterator.next();
|
||||
@@ -239,7 +271,7 @@
|
||||
|
||||
argumentbuilder.requires((icompletionprovider) -> {
|
||||
return true;
|
||||
@@ -415,7 +517,7 @@
|
||||
@@ -415,12 +545,12 @@
|
||||
argumentbuilder.redirect((CommandNode) resultNodes.get(argumentbuilder.getRedirect()));
|
||||
}
|
||||
|
||||
@@ -248,7 +280,13 @@
|
||||
|
||||
resultNodes.put(commandnode2, commandnode3);
|
||||
result.addChild(commandnode3);
|
||||
@@ -481,7 +583,7 @@
|
||||
if (!commandnode2.getChildren().isEmpty()) {
|
||||
- this.fillUsableCommands(commandnode2, commandnode3, source, resultNodes);
|
||||
+ this.fillUsableCommands(commandnode2.getChildren(), commandnode3, source, resultNodes); // Paper - Perf: Async command map building; pass children directly
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -481,7 +611,7 @@
|
||||
}
|
||||
|
||||
private <T> HolderLookup.RegistryLookup.Delegate<T> createLookup(final HolderLookup.RegistryLookup<T> original) {
|
||||
|
Reference in New Issue
Block a user