From 4146ce069e7e2b69e6ddc2001087273d92375c68 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Mon, 10 Jul 2023 13:41:25 +0300 Subject: [PATCH] #2208: Parallel compiling --- .../java/org/eolang/maven/BinarizeMojo.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java index 69945b8062..23c456b4c7 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java @@ -34,6 +34,9 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.cactoos.experimental.Threads; +import org.cactoos.iterable.Mapped; +import org.cactoos.number.SumOf; import org.eolang.maven.rust.BuildFailureException; /** @@ -45,9 +48,6 @@ * Now it copies cargo project to cache directory in the end of every * compilation. It is better to copy the project only if it was changed * with the last compilation. - * @todo #2195:90min Make cargo compilation in parallel. Now cargo - * projects are being built consistently which is too long. - * It is much better to build them in parallel to reduce time. */ @Mojo( name = "binarize", @@ -77,11 +77,26 @@ public final class BinarizeMojo extends SafeMojo { @Override public void exec() throws IOException { new Moja<>(BinarizeParseMojo.class).copy(this).execute(); - for (final File project: targetDir.toPath().resolve("Lib").toFile().listFiles()) { - if (project.isDirectory() && project.toPath().resolve("Cargo.toml").toFile().exists()) { - this.build(project); - } - } + int total = new SumOf( + new Threads<>( + Runtime.getRuntime().availableProcessors(), + new Mapped<>( + project -> () -> { + if (project.isDirectory() && project.toPath().resolve("Cargo.toml").toFile().exists()) { + this.build(project); + return 1; + } else { + return 0; + } + }, + targetDir.toPath().resolve("Lib").toFile().listFiles() + ) + ) + ).intValue(); + Logger.info( + this, + String.format("Built in total %d cargo projects", total) + ); } /** @@ -117,16 +132,6 @@ private void build(final File project) throws IOException { ) ) { proc.stdout(); - proc.waitFor(); - } catch (final InterruptedException exception) { - Thread.currentThread().interrupt(); - throw new BuildFailureException( - String.format( - "Interrupted while building %s", - project - ), - exception - ); } catch (final IllegalArgumentException exc) { throw new BuildFailureException( String.format(