aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0391-Optimize-DataFixer-Bootstrap-to-prebuild-faster.patch
blob: f1b36aff3e9303be6031cbaa2305842d818f4148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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