Skip to content

Commit

Permalink
Don't exit JVM from optimizer threads
Browse files Browse the repository at this point in the history
  • Loading branch information
boq committed May 6, 2024
1 parent 8870960 commit 02782e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
majorMinor: 7.1
majorMinor: 8.0
45 changes: 31 additions & 14 deletions src/main/java/com/mojang/datafixers/DataFixerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.
package com.mojang.datafixers;

import com.google.common.collect.Lists;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.Type;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
Expand All @@ -28,7 +27,7 @@ public class DataFixerBuilder {

private final int dataVersion;
private final Int2ObjectSortedMap<Schema> schemas = new Int2ObjectAVLTreeMap<>();
private final List<DataFix> globalList = Lists.newArrayList();
private final List<DataFix> globalList = new ArrayList<>();
private final IntSortedSet fixerVersions = new IntAVLTreeSet();

public DataFixerBuilder(final int dataVersion) {
Expand Down Expand Up @@ -63,15 +62,19 @@ public void addFixer(final DataFix fix) {
fixerVersions.add(fix.getVersionKey());
}

public DataFixer buildUnoptimized() {
return build();
public Result buildUnoptimized() {
return new Result(
build(),
CompletableFuture.completedFuture(null)
);
}

public DataFixer buildOptimized(final Set<DSL.TypeReference> requiredTypes, final Executor executor) {
public Result buildOptimized(final Set<DSL.TypeReference> requiredTypes, final Executor executor) {
final DataFixerUpper fixerUpper = build();

final Instant started = Instant.now();
final List<CompletableFuture<Void>> futures = Lists.newArrayList();
final List<CompletableFuture<?>> doneFutures = new ArrayList<>();
final List<CompletableFuture<?>> failFutures = new ArrayList<>();

final Set<String> requiredTypeNames = requiredTypes.stream().map(DSL.TypeReference::typeName).collect(Collectors.toSet());

Expand All @@ -83,27 +86,41 @@ public DataFixer buildOptimized(final Set<DSL.TypeReference> requiredTypes, fina
if (!requiredTypeNames.contains(typeName)) {
continue;
}
final CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
final CompletableFuture<Void> doneFuture = CompletableFuture.runAsync(() -> {
final Type<?> dataType = schema.getType(() -> typeName);
final TypeRewriteRule rule = fixerUpper.getRule(DataFixUtils.getVersion(versionKey), dataVersion);
dataType.rewrite(rule, DataFixerUpper.OPTIMIZATION_RULE);
}, executor).exceptionally(e -> {
LOGGER.error("Unable to build datafixers", e);
Runtime.getRuntime().exit(1);
}, executor);
doneFutures.add(doneFuture);

final CompletableFuture<?> failFuture = new CompletableFuture<>();
doneFuture.exceptionally(e -> {
failFuture.completeExceptionally(e);
return null;
});
futures.add(future);
failFutures.add(failFuture);
}
}

CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).thenAccept(ignored -> {
LOGGER.info("{} Datafixer optimizations took {} milliseconds", futures.size(), Duration.between(started, Instant.now()).toMillis());
final CompletableFuture<?> doneFuture = CompletableFuture.allOf(doneFutures.toArray(CompletableFuture[]::new)).thenAccept(ignored -> {
LOGGER.info("{} Datafixer optimizations took {} milliseconds", doneFutures.size(), Duration.between(started, Instant.now()).toMillis());
});

return fixerUpper;
final CompletableFuture<?> failFuture = CompletableFuture.anyOf(failFutures.toArray(CompletableFuture[]::new));

return new Result(
fixerUpper,
CompletableFuture.anyOf(doneFuture, failFuture)
);
}

private DataFixerUpper build() {
return new DataFixerUpper(new Int2ObjectAVLTreeMap<>(schemas), new ArrayList<>(globalList), new IntAVLTreeSet(fixerVersions));
}

public record Result(
DataFixer fixer,
CompletableFuture<?> optimizationResult
) {
}
}

0 comments on commit 02782e6

Please sign in to comment.