| 1 | package net.minecraft.util.thread;
|
| 2 |
|
| 3 | import java.util.HashMap;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.Map.Entry;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import java.util.concurrent.Executor;
|
| 8 | import java.util.function.BiFunction;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class ParallelMapTransform {
|
| 14 | private static final int DEFAULT_TASKS_PER_THREAD = 16;
|
| 15 |
|
| 16 | public static <K, U, V> CompletableFuture<Map<K, V>> schedule(
|
| 17 | final Map<K, U> input, final BiFunction<K, U, @Nullable V> operation, final int maxTaskCount, final Executor executor
|
| 18 | ) {
|
| 19 | int inputSize = input.size();
|
| 20 | if (inputSize == 0) {
|
| 21 | return CompletableFuture.completedFuture(Map.of());
|
| 22 | } else if (inputSize == 1) {
|
| 23 | Entry<K, U> element = input.entrySet().iterator().next();
|
| 24 | K key = element.getKey();
|
| 25 | U value = element.getValue();
|
| 26 | return CompletableFuture.supplyAsync(() -> {
|
| 27 | V result = operation.apply(key, value);
|
| 28 | return result != null ? Map.of(key, result) : Map.of();
|
| 29 | }, executor);
|
| 30 | } else {
|
| 31 | ParallelMapTransform.SplitterBase<K, U, V> splitter = inputSize <= maxTaskCount
|
| 32 | ? new ParallelMapTransform.SingleTaskSplitter<>(operation, inputSize)
|
| 33 | : new ParallelMapTransform.BatchedTaskSplitter<>(operation, inputSize, maxTaskCount);
|
| 34 | return splitter.scheduleTasks(input, executor);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | public static <K, U, V> CompletableFuture<Map<K, V>> schedule(final Map<K, U> input, final BiFunction<K, U, @Nullable V> operation, final Executor executor) {
|
| 39 | int maxTaskCount = Util.maxAllowedExecutorThreads() * 16;
|
| 40 | return schedule(input, operation, maxTaskCount, executor);
|
| 41 | }
|
| 42 |
|
| 43 | private static class BatchedTaskSplitter<K, U, V> extends ParallelMapTransform.SplitterBase<K, U, V> {
|
| 44 | private final Map<K, V> result;
|
| 45 | private final int batchSize;
|
| 46 | private final int firstUndersizedBatchIndex;
|
| 47 |
|
| 48 | private BatchedTaskSplitter(final BiFunction<K, U, V> operation, final int size, final int maxTasks) {
|
| 49 | super(operation, size, maxTasks);
|
| 50 | this.result = new HashMap<>(size);
|
| 51 | this.batchSize = Mth.positiveCeilDiv(size, maxTasks);
|
| 52 | int fullCapacity = this.batchSize * maxTasks;
|
| 53 | int leftoverCapacity = fullCapacity - size;
|
| 54 | this.firstUndersizedBatchIndex = maxTasks - leftoverCapacity;
|
| 55 | assert this.firstUndersizedBatchIndex > 0 && this.firstUndersizedBatchIndex <= maxTasks;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected CompletableFuture<?> scheduleBatch(
|
| 60 | final ParallelMapTransform.Container<K, U, V> container, final int startIndex, final int endIndex, final Executor executor
|
| 61 | ) {
|
| 62 | int batchSize = endIndex - startIndex;
|
| 63 | assert batchSize == this.batchSize || batchSize == this.batchSize - 1;
|
| 64 | return CompletableFuture.runAsync(createTask(this.result, startIndex, endIndex, container), executor);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected int batchSize(final int index) {
|
| 69 | return index < this.firstUndersizedBatchIndex ? this.batchSize : this.batchSize - 1;
|
| 70 | }
|
| 71 |
|
| 72 | private static <K, U, V> Runnable createTask(
|
| 73 | final Map<K, V> result, final int startIndex, final int endIndex, final ParallelMapTransform.Container<K, U, V> container
|
| 74 | ) {
|
| 75 | return () -> {
|
| 76 | for (int i = startIndex; i < endIndex; i++) {
|
| 77 | container.applyOperation(i);
|
| 78 | }
|
| 79 |
|
| 80 | synchronized (result) {
|
| 81 | for (int i = startIndex; i < endIndex; i++) {
|
| 82 | container.copyOut(i, result);
|
| 83 | }
|
| 84 | }
|
| 85 | };
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected CompletableFuture<Map<K, V>> scheduleFinalOperation(
|
| 90 | final CompletableFuture<?> allTasksDone, final ParallelMapTransform.Container<K, U, V> container
|
| 91 | ) {
|
| 92 | Map<K, V> result = this.result;
|
| 93 | return allTasksDone.thenApply(ignored -> result);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private record Container<K, U, V>(BiFunction<K, U, V> operation, @Nullable Object[] keys, @Nullable Object[] values) {
|
| 98 | public Container(final BiFunction<K, U, V> operation, final int size) {
|
| 99 | this(operation, new Object[size], new Object[size]);
|
| 100 | }
|
| 101 |
|
| 102 | public void put(final int index, final K key, final U input) {
|
| 103 | this.keys[index] = key;
|
| 104 | this.values[index] = input;
|
| 105 | }
|
| 106 |
|
| 107 | private @Nullable K key(final int index) {
|
| 108 | return (K)this.keys[index];
|
| 109 | }
|
| 110 |
|
| 111 | private @Nullable V output(final int index) {
|
| 112 | return (V)this.values[index];
|
| 113 | }
|
| 114 |
|
| 115 | private @Nullable U input(final int index) {
|
| 116 | return (U)this.values[index];
|
| 117 | }
|
| 118 |
|
| 119 | public void applyOperation(final int index) {
|
| 120 | this.values[index] = this.operation.apply(this.key(index), this.input(index));
|
| 121 | }
|
| 122 |
|
| 123 | public void copyOut(final int index, final Map<K, V> output) {
|
| 124 | V value = this.output(index);
|
| 125 | if (value != null) {
|
| 126 | K key = this.key(index);
|
| 127 | output.put(key, value);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | public int size() {
|
| 132 | return this.keys.length;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private static class SingleTaskSplitter<K, U, V> extends ParallelMapTransform.SplitterBase<K, U, V> {
|
| 137 | private SingleTaskSplitter(final BiFunction<K, U, V> operation, final int size) {
|
| 138 | super(operation, size, size);
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected int batchSize(final int index) {
|
| 143 | return 1;
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected CompletableFuture<?> scheduleBatch(
|
| 148 | final ParallelMapTransform.Container<K, U, V> container, final int startIndex, final int endIndex, final Executor executor
|
| 149 | ) {
|
| 150 | assert startIndex + 1 == endIndex;
|
| 151 | return CompletableFuture.runAsync(() -> container.applyOperation(startIndex), executor);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected CompletableFuture<Map<K, V>> scheduleFinalOperation(
|
| 156 | final CompletableFuture<?> allTasksDone, final ParallelMapTransform.Container<K, U, V> container
|
| 157 | ) {
|
| 158 | return allTasksDone.thenApply(ignored -> {
|
| 159 | Map<K, V> result = new HashMap<>(container.size());
|
| 160 |
|
| 161 | for (int i = 0; i < container.size(); i++) {
|
| 162 | container.copyOut(i, result);
|
| 163 | }
|
| 164 |
|
| 165 | return result;
|
| 166 | });
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | private abstract static class SplitterBase<K, U, V> {
|
| 171 | private int lastScheduledIndex;
|
| 172 | private int currentIndex;
|
| 173 | private final CompletableFuture<?>[] tasks;
|
| 174 | private int batchIndex;
|
| 175 | private final ParallelMapTransform.Container<K, U, V> container;
|
| 176 |
|
| 177 | private SplitterBase(final BiFunction<K, U, V> operation, final int size, final int taskCount) {
|
| 178 | this.container = new ParallelMapTransform.Container<>(operation, size);
|
| 179 | this.tasks = new CompletableFuture[taskCount];
|
| 180 | }
|
| 181 |
|
| 182 | private int pendingBatchSize() {
|
| 183 | return this.currentIndex - this.lastScheduledIndex;
|
| 184 | }
|
| 185 |
|
| 186 | public CompletableFuture<Map<K, V>> scheduleTasks(final Map<K, U> input, final Executor executor) {
|
| 187 | input.forEach((key, inputValue) -> {
|
| 188 | this.container.put(this.currentIndex++, (K)key, (U)inputValue);
|
| 189 | if (this.pendingBatchSize() == this.batchSize(this.batchIndex)) {
|
| 190 | this.tasks[this.batchIndex++] = this.scheduleBatch(this.container, this.lastScheduledIndex, this.currentIndex, executor);
|
| 191 | this.lastScheduledIndex = this.currentIndex;
|
| 192 | }
|
| 193 | });
|
| 194 | assert this.currentIndex == this.container.size();
|
| 195 | assert this.lastScheduledIndex == this.currentIndex;
|
| 196 | assert this.batchIndex == this.tasks.length;
|
| 197 | return this.scheduleFinalOperation(CompletableFuture.allOf(this.tasks), this.container);
|
| 198 | }
|
| 199 |
|
| 200 | protected abstract int batchSize(int index);
|
| 201 |
|
| 202 | protected abstract CompletableFuture<?> scheduleBatch(ParallelMapTransform.Container<K, U, V> container, int startIndex, int endIndex, Executor executor);
|
| 203 |
|
| 204 | protected abstract CompletableFuture<Map<K, V>> scheduleFinalOperation(CompletableFuture<?> allTasksDone, ParallelMapTransform.Container<K, U, V> container);
|
| 205 | }
|
| 206 | }
|
| 207 |
|