Paper/Spigot-Server-Patches/0391-Optimize-DataFixer-Bootstrap-to-prebuild-faster.patch
Aikar 04b8268cf1
Improvements to DataFixers for performance
Mojang asked me to make some changes, so applied them here.
2018-10-12 01:29:28 -04:00

66 lines
3.0 KiB
Diff

From 757fd2730e797d31f71492826e972f9a49dbe8ac Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 12 Oct 2018 01:28:32 -0400
Subject: [PATCH] Optimize DataFixer Bootstrap to prebuild faster
diff --git a/src/main/java/com/mojang/datafixers/DataFixerBuilder.java b/src/main/java/com/mojang/datafixers/DataFixerBuilder.java
index 8bc1e3b976..114f958601 100644
--- a/src/main/java/com/mojang/datafixers/DataFixerBuilder.java
+++ b/src/main/java/com/mojang/datafixers/DataFixerBuilder.java
@@ -12,7 +12,9 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
@@ -60,16 +62,37 @@ public class DataFixerBuilder {
final DataFixerUpper fixerUpper = new DataFixerUpper(new Int2ObjectAVLTreeMap<>(schemas), new ArrayList<>(globalList), new IntAVLTreeSet(fixerVersions));
executor.execute(() -> {
- final IntBidirectionalIterator iterator = fixerUpper.fixerVersions().iterator();
+ List<Runnable> taskList = new ArrayList<>();
+ IntBidirectionalIterator iterator = fixerUpper.fixerVersions().iterator();
while (iterator.hasNext()) {
final int versionKey = iterator.nextInt();
final Schema schema = schemas.get(versionKey);
+
for (final String typeName: schema.types()) {
- final Type<?> dataType = schema.getType(() -> typeName);
- final TypeRewriteRule rule = fixerUpper.getRule(DataFixUtils.getVersion(versionKey), dataVersion);
- dataType.rewrite(rule, DataFixerUpper.OPTIMIZATION_RULE);
+ taskList.add(() -> {
+ final Type<?> dataType = schema.getType(() -> typeName);
+ final TypeRewriteRule rule = fixerUpper.getRule(DataFixUtils.getVersion(versionKey), dataVersion);
+ dataType.rewrite(rule, DataFixerUpper.OPTIMIZATION_RULE);
+ });
}
}
+
+ // Divide up into sets of tasks by number of CPU cores
+ // Some tasks are faster than others, randomize it to try to divide it more
+ Collections.shuffle(taskList);
+ List<List<Runnable>> buildQueue = new ArrayList<>();
+ List<Runnable> current = new ArrayList<>();
+ buildQueue.add(current);
+ int maxTasks = (int) Math.max(1, Math.floor(taskList.size() / (float)Math.min(6, Runtime.getRuntime().availableProcessors()-2)));
+ for (Runnable task : taskList) {
+ if (current.size() > maxTasks) {
+ current = new ArrayList<>();
+ buildQueue.add(current);
+ }
+ current.add(task);
+ }
+
+ buildQueue.forEach(queue -> executor.execute(() -> queue.forEach(Runnable::run)));
});
return fixerUpper;
--
2.19.1