Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Parallel formatting for Maven plugin #1123

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven;

import static java.util.concurrent.CompletableFuture.supplyAsync;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.PaddedCell;
import com.diffplug.spotless.PaddedCell.DirtyState;

public class FormattingParallelizer {

public static final FormattingParallelizer INSTANCE = new FormattingParallelizer();

private FormattingParallelizer() {}

private final Executor readerExecutor = Executors.newFixedThreadPool(2, daemonThreadFactory());

private final Executor formatterExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors() * 2, daemonThreadFactory());

private final Executor writerExecutor = Executors.newFixedThreadPool(2, daemonThreadFactory());

CompletableFuture<Void> format(File file, Formatter formatter) {
return readFileContent(file)
.thenApplyAsync(raw -> calculateDirtyState(file, formatter, raw), formatterExecutor)
.thenAcceptAsync(dirtyState -> writeFormatted(dirtyState, file), writerExecutor);
}

private CompletableFuture<byte[]> readFileContent(File file) {
return supplyAsync(
() -> {
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
},
readerExecutor);
}

private DirtyState calculateDirtyState(File file, Formatter formatter, byte[] rawBytes) {
try {
return PaddedCell.calculateDirtyState(formatter, file, rawBytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void writeFormatted(DirtyState dirtyState, File file) {
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
try {
dirtyState.writeCanonicalTo(file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

private static ThreadFactory daemonThreadFactory() {
return runnable -> {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
return thread;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,14 @@
package com.diffplug.spotless.maven;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.PaddedCell;
import com.diffplug.spotless.maven.incremental.UpToDateChecker;

/**
Expand All @@ -33,6 +34,8 @@ public class SpotlessApplyMojo extends AbstractSpotlessMojo {

@Override
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
List<CompletableFuture<Void>> futures = new ArrayList<>();

for (File file : files) {
if (upToDateChecker.isUpToDate(file.toPath())) {
if (getLog().isDebugEnabled()) {
Expand All @@ -41,15 +44,12 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
continue;
}

try {
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
dirtyState.writeCanonicalTo(file);
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to format file " + file, e);
}
futures.add(FormattingParallelizer.INSTANCE.format(file, formatter));
}

CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

for (File file : files) {
upToDateChecker.setUpToDate(file.toPath());
}
}
Expand Down