From bab66d67c91e5cc35c1fce8976184084d59f9cb0 Mon Sep 17 00:00:00 2001 From: aget Date: Tue, 24 Dec 2024 19:10:01 +0300 Subject: [PATCH 1/4] add OptimizeMojo.OptSpy and OptimizeMojo.OptTrain, chand implemented interface from Optimization to Function, replaced optimization package's classes to new inner classes in OptimizeMojo --- .../java/org/eolang/maven/OptimizeMojo.java | 156 ++++++++++++++++-- 1 file changed, 146 insertions(+), 10 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index b901f3684b..2bd212af9f 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -27,20 +27,25 @@ import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import com.yegor256.xsline.Shift; +import com.yegor256.xsline.StClasspath; +import com.yegor256.xsline.TrClasspath; +import com.yegor256.xsline.TrDefault; +import com.yegor256.xsline.TrFast; +import com.yegor256.xsline.TrLambda; import com.yegor256.xsline.Train; +import com.yegor256.xsline.Xsline; import java.nio.file.Path; import java.util.Collection; +import java.util.function.Function; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.cactoos.iterable.Filtered; import org.eolang.maven.footprint.FpDefault; -import org.eolang.maven.optimization.OptSpy; -import org.eolang.maven.optimization.OptTrain; -import org.eolang.maven.optimization.Optimization; import org.eolang.maven.tojos.ForeignTojo; import org.eolang.maven.tojos.TojoHash; import org.eolang.maven.util.Threaded; +import org.eolang.parser.StEoLogged; import org.eolang.parser.TrParsing; /** @@ -88,13 +93,12 @@ public final class OptimizeMojo extends SafeMojo { public void exec() { final long start = System.currentTimeMillis(); final Collection tojos = this.scopedTojos().withXmir(); - final Optimization optimization = this.optimization(); final int total = new Threaded<>( new Filtered<>( ForeignTojo::notOptimized, tojos ), - tojo -> this.optimized(tojo, optimization) + tojo -> this.optimized(tojo, this.optimization()) ).total(); if (total > 0) { Logger.info( @@ -115,7 +119,7 @@ public void exec() { * @return Amount of optimized XMIR files * @throws Exception If fails */ - private int optimized(final ForeignTojo tojo, final Optimization optimization) + private int optimized(final ForeignTojo tojo, final Function optimization) throws Exception { final Path source = tojo.xmir(); final XML xmir = new XMLDocument(source); @@ -138,18 +142,150 @@ private int optimized(final ForeignTojo tojo, final Optimization optimization) * Common optimization for all tojos. * @return Optimization for all tojos. */ - private Optimization optimization() { - final Optimization opt; + private Function optimization() { + final Function opt; final Train train = this.measured(new TrParsing()); if (this.trackOptimizationSteps) { - opt = new OptSpy( + opt = new OptimizeMojo.OptSpy( train, this.targetDir.toPath().resolve(OptimizeMojo.STEPS) ); } else { - opt = new OptTrain(train); + opt = new OptimizeMojo.OptTrain(train); } return opt; } + /** + * Optimization that spies. + * @since 0.68.0 + */ + public static final class OptSpy implements Function { + /** + * Optimizations train. + */ + private final Train train; + + /** + * Where to track optimization steps. + */ + private final Path target; + + /** + * Ctor. + * @param target Where to track optimization steps. + */ + public OptSpy(final Path target) { + this(OptTrain.DEFAULT_TRAIN, target); + } + + /** + * The main constructor. + * @param trn Optimizations train. + * @param target Where to track optimization steps. + */ + public OptSpy(final Train trn, final Path target) { + this.train = trn; + this.target = target; + } + + @Override + public XML apply(final XML xml) { + final Path dir = new Place(xml.xpath("/program/@name").get(0)).make(this.target, ""); + Logger.debug(this, "Optimization steps will be tracked to %[file]s", dir); + return new OptTrain(new SpyTrain(this.train, dir)).apply(xml); + } + } + + /** + * Optimisation train of XLS`s. + * @since 0.68.0 + * @todo #3115:30min Return constant-folding.xsl when it's ready. + * This optimization was removed from the train because it's not really + * ready and works only with `bool` object which was removed. We + * need to make this optimization great again and add to the train. + */ + public static final class OptTrain implements Function { + + /** + * Parsing train with XSLs. + * @implNote The list of applied XSLs is adjusted during execution. + *
Separate instance of the train is used of each optimization + * thread since {@link com.jcabi.xml.XSLDocument}, which is used under + * the hood in {@link com.yegor256.xsline.TrClasspath}, is not thread-safe. + */ + static final Train DEFAULT_TRAIN = new TrFast( + new TrLambda( + new TrClasspath<>( + "/org/eolang/parser/optimize/globals-to-abstracts.xsl", + "/org/eolang/parser/optimize/remove-refs.xsl", + "/org/eolang/parser/optimize/abstracts-float-up.xsl", + "/org/eolang/parser/optimize/remove-levels.xsl", + "/org/eolang/parser/add-refs.xsl", + "/org/eolang/parser/optimize/fix-missed-names.xsl", + "/org/eolang/parser/add-refs.xsl", + "/org/eolang/parser/set-locators.xsl", + "/org/eolang/parser/blank-xsd-schema.xsl" + ).back(), + StEoLogged::new + ), + TrFast.class, + 500L + ); + + /** + * Delegate. + */ + private final Function delegate; + + /** + * Shifts that we are going to apply. + */ + private final Train shifts; + + /** + * The default constructor with the default preset of xsl optimizations. + */ + public OptTrain() { + this(OptTrain.DEFAULT_TRAIN); + } + + /** + * Constructor that accepts train of shifts. + * @param shifts XLS shifts. + */ + public OptTrain(final Train shifts) { + this(xml -> xml, shifts); + } + + /** + * Constructor for single {@link com.yegor256.xsline.StClasspath} optimization. + * @param delegate Optimizations that have to be done before. + * @param xls File from classpath. + */ + public OptTrain(final Function delegate, final String xls) { + this( + delegate, + new TrDefault().with(new StClasspath(xls)) + ); + } + + /** + * The default constructor. + * @param delegate Optimizations that have to be done before. + * @param shifts To apply + */ + public OptTrain( + final Function delegate, + final Train shifts + ) { + this.delegate = delegate; + this.shifts = shifts; + } + + @Override + public XML apply(final XML xml) { + return new Xsline(this.shifts).pass(this.delegate.apply(xml)); + } + } } From dd080046045adbb870b6fdec7a1694e2a0b8cde2 Mon Sep 17 00:00:00 2001 From: aget Date: Tue, 24 Dec 2024 19:24:30 +0300 Subject: [PATCH 2/4] replace optimization package classes to OptimizeMojo.* in SnakeMojo and TranspileMojo --- .../main/java/org/eolang/maven/ShakeMojo.java | 22 ++++++++++--------- .../java/org/eolang/maven/TranspileMojo.java | 14 +++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java index 995a2b8a6a..2dec292c99 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java @@ -28,14 +28,12 @@ import com.jcabi.xml.XMLDocument; import java.nio.file.Path; import java.util.Collection; +import java.util.function.Function; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.cactoos.iterable.Filtered; import org.eolang.maven.footprint.FpDefault; -import org.eolang.maven.optimization.OptSpy; -import org.eolang.maven.optimization.OptTrain; -import org.eolang.maven.optimization.Optimization; import org.eolang.maven.tojos.ForeignTojo; import org.eolang.maven.tojos.TojoHash; import org.eolang.maven.util.Threaded; @@ -80,13 +78,12 @@ public final class ShakeMojo extends SafeMojo { void exec() { final long start = System.currentTimeMillis(); final Collection tojos = this.scopedTojos().withOptimized(); - final Optimization optimization = this.optimization(); final int total = new Threaded<>( new Filtered<>( ForeignTojo::notShaken, tojos ), - tojo -> this.shaken(tojo, optimization) + tojo -> this.shaken(tojo, this.optimization()) ).total(); if (total > 0) { Logger.info( @@ -107,7 +104,10 @@ void exec() { * @return Amount of optimized XMIR files * @throws Exception If fails */ - private int shaken(final ForeignTojo tojo, final Optimization optimization) throws Exception { + private int shaken( + final ForeignTojo tojo, + final Function optimization + ) throws Exception { final Path source = tojo.optimized(); final XML xmir = new XMLDocument(source); final String name = xmir.xpath("/program/@name").get(0); @@ -130,12 +130,14 @@ private int shaken(final ForeignTojo tojo, final Optimization optimization) thro * * @return Shake optimizations */ - private Optimization optimization() { - final Optimization opt; + private Function optimization() { + final Function opt; if (this.trackOptimizationSteps) { - opt = new OptSpy(this.targetDir.toPath().resolve(ShakeMojo.STEPS)); + opt = new OptimizeMojo.OptSpy( + this.targetDir.toPath().resolve(ShakeMojo.STEPS) + ); } else { - opt = new OptTrain(); + opt = new OptimizeMojo.OptTrain(); } return opt; } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java index c67aff0323..5f6f8c7a80 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java @@ -41,6 +41,7 @@ import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import java.util.function.Supplier; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -57,8 +58,6 @@ import org.eolang.maven.footprint.FpIgnore; import org.eolang.maven.footprint.FpUpdateBoth; import org.eolang.maven.footprint.FpUpdateFromCache; -import org.eolang.maven.optimization.OptSpy; -import org.eolang.maven.optimization.Optimization; import org.eolang.maven.tojos.AttributeNotFoundException; import org.eolang.maven.tojos.ForeignTojo; import org.eolang.maven.tojos.TojoHash; @@ -163,10 +162,9 @@ public final class TranspileMojo extends SafeMojo { @Override public void exec() { final Collection sources = this.scopedTojos().withShaken(); - final Optimization optimization = this.transpilation(); final int saved = new Threaded<>( sources, - tojo -> this.transpiled(tojo, optimization) + tojo -> this.transpiled(tojo, this.transpilation()) ).total(); Logger.info( this, "Transpiled %d XMIRs, created %d Java files in %[file]s", @@ -195,12 +193,12 @@ public void exec() { * @return Number of transpiled files. * @throws java.io.IOException If any issues with I/O */ - private int transpiled(final ForeignTojo tojo, final Optimization transpilation) + private int transpiled(final ForeignTojo tojo, final Function transpilation) throws IOException { final Path source; try { source = tojo.shaken(); - } catch (final AttributeNotFoundException exception) { + } catch (final AttributeNotFoundException exception) { throw new IllegalStateException( "You should check that 'Verify' goal of the plugin was run first", exception @@ -229,8 +227,8 @@ private int transpiled(final ForeignTojo tojo, final Optimization transpilation) * Transpile optimization. * @return Optimization that transpiles */ - private Optimization transpilation() { - return new OptSpy( + private Function transpilation() { + return new OptimizeMojo.OptSpy( this.measured(TranspileMojo.TRAIN), this.targetDir.toPath().resolve(TranspileMojo.PRE) ); From 7e88ff229e4634f70f214e0900d9899ac956bf2c Mon Sep 17 00:00:00 2001 From: aget Date: Tue, 24 Dec 2024 19:29:16 +0300 Subject: [PATCH 3/4] remove redundant optimization package --- .../org/eolang/maven/optimization/OptSpy.java | 73 ---------- .../eolang/maven/optimization/OptTrain.java | 129 ------------------ .../maven/optimization/Optimization.java | 36 ----- .../maven/optimization/package-info.java | 32 ----- 4 files changed, 270 deletions(-) delete mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java delete mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java delete mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java delete mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/optimization/package-info.java diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java deleted file mode 100644 index ca6999a621..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven.optimization; - -import com.jcabi.log.Logger; -import com.jcabi.xml.XML; -import com.yegor256.xsline.Shift; -import com.yegor256.xsline.Train; -import java.nio.file.Path; -import org.eolang.maven.Place; -import org.eolang.maven.SpyTrain; - -/** - * Optimization that spies. - * @since 0.28.12 - */ -public final class OptSpy implements Optimization { - /** - * Optimizations train. - */ - private final Train train; - - /** - * Where to track optimization steps. - */ - private final Path target; - - /** - * Ctor. - * @param target Where to track optimization steps. - */ - public OptSpy(final Path target) { - this(OptTrain.DEFAULT_TRAIN, target); - } - - /** - * The main constructor. - * @param trn Optimizations train. - * @param target Where to track optimization steps. - */ - public OptSpy(final Train trn, final Path target) { - this.train = trn; - this.target = target; - } - - @Override - public XML apply(final XML xml) { - final Path dir = new Place(xml.xpath("/program/@name").get(0)).make(this.target, ""); - Logger.debug(this, "Optimization steps will be tracked to %[file]s", dir); - return new OptTrain(new SpyTrain(this.train, dir)).apply(xml); - } -} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java deleted file mode 100644 index 91493c413c..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven.optimization; - -import com.jcabi.xml.XML; -import com.yegor256.xsline.Shift; -import com.yegor256.xsline.StClasspath; -import com.yegor256.xsline.TrClasspath; -import com.yegor256.xsline.TrDefault; -import com.yegor256.xsline.TrFast; -import com.yegor256.xsline.TrLambda; -import com.yegor256.xsline.Train; -import com.yegor256.xsline.Xsline; -import org.eolang.parser.StEoLogged; - -/** - * Optimisation train of XLS`s. - * @since 0.28.12 - * @todo #3115:30min Return constant-folding.xsl when it's ready. This optimization was removed from - * the train because it's not really ready and works only with `bool` object which was removed. We - * need to make this optimization great again and add to the train. - */ -public final class OptTrain implements Optimization { - - /** - * Parsing train with XSLs. - * - * @implNote The list of applied XSLs is adjusted during execution. - *
Separate instance of the train is used of each optimization - * thread since {@link com.jcabi.xml.XSLDocument}, which is used under - * the hood in {@link TrClasspath}, is not thread-safe. - */ - static final Train DEFAULT_TRAIN = new TrFast( - new TrLambda( - new TrClasspath<>( - "/org/eolang/parser/optimize/globals-to-abstracts.xsl", - "/org/eolang/parser/optimize/remove-refs.xsl", - "/org/eolang/parser/optimize/abstracts-float-up.xsl", - "/org/eolang/parser/optimize/remove-levels.xsl", - "/org/eolang/parser/add-refs.xsl", - "/org/eolang/parser/optimize/fix-missed-names.xsl", - "/org/eolang/parser/add-refs.xsl", - "/org/eolang/parser/set-locators.xsl", - "/org/eolang/parser/blank-xsd-schema.xsl" - ).back(), - StEoLogged::new - ), - TrFast.class, - 500L - ); - - /** - * Delegate. - */ - private final Optimization delegate; - - /** - * Shifts that we are going to apply. - */ - private final Train shifts; - - /** - * The default constructor with the default preset of xsl optimizations. - */ - public OptTrain() { - this(OptTrain.DEFAULT_TRAIN); - } - - /** - * Constructor that accepts train of shifts. - * - * @param shifts XLS shifts. - */ - public OptTrain(final Train shifts) { - this(xml -> xml, shifts); - } - - /** - * Constructor for single {@link StClasspath} optimization. - * - * @param delegate Optimizations that have to be done before. - * @param xls File from classpath. - */ - public OptTrain(final Optimization delegate, final String xls) { - this( - delegate, - new TrDefault().with(new StClasspath(xls)) - ); - } - - /** - * The default constructor. - * @param delegate Optimizations that have to be done before. - * @param shifts To apply - */ - public OptTrain( - final Optimization delegate, - final Train shifts - ) { - this.delegate = delegate; - this.shifts = shifts; - } - - @Override - public XML apply(final XML xml) { - return new Xsline(this.shifts).pass(this.delegate.apply(xml)); - } -} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java deleted file mode 100644 index 78efed87ba..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven.optimization; - -import com.jcabi.xml.XML; -import java.util.function.Function; - -/** - * Abstraction for XML optimizations. - * - * @since 0.28.11 - */ -@FunctionalInterface -public interface Optimization extends Function { -} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/package-info.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/package-info.java deleted file mode 100644 index 65a3637239..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/package-info.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -/** - * Optimizations package. - * The main purpose of classes under the package is to provide different implementations - * of xml optimizations: - * - {@link org.eolang.maven.optimization.OptCached} - looks for optimization in cache first - * - {@link org.eolang.maven.optimization.OptSpy} - makes XLS optimizations that are tracked - * - {@link org.eolang.maven.optimization.OptTrain} - makes XLS optimizations directly - */ -package org.eolang.maven.optimization; From 61c4c107e366016eb860faf78475d0cee576fce7 Mon Sep 17 00:00:00 2001 From: aget Date: Wed, 25 Dec 2024 13:33:08 +0300 Subject: [PATCH 4/4] remove optimization instantiation at every call in threaded, encapsulated Xsline in OptTrain, made Opt** package private --- .../java/org/eolang/maven/OptimizeMojo.java | 46 +++++++++++++------ .../main/java/org/eolang/maven/ShakeMojo.java | 3 +- .../java/org/eolang/maven/TranspileMojo.java | 3 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 2bd212af9f..d96126d143 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -93,12 +93,13 @@ public final class OptimizeMojo extends SafeMojo { public void exec() { final long start = System.currentTimeMillis(); final Collection tojos = this.scopedTojos().withXmir(); + final Function optimization = this.optimization(); final int total = new Threaded<>( new Filtered<>( ForeignTojo::notOptimized, tojos ), - tojo -> this.optimized(tojo, this.optimization()) + tojo -> this.optimized(tojo, optimization) ).total(); if (total > 0) { Logger.info( @@ -160,7 +161,7 @@ private Function optimization() { * Optimization that spies. * @since 0.68.0 */ - public static final class OptSpy implements Function { + static final class OptSpy implements Function { /** * Optimizations train. */ @@ -175,7 +176,7 @@ public static final class OptSpy implements Function { * Ctor. * @param target Where to track optimization steps. */ - public OptSpy(final Path target) { + OptSpy(final Path target) { this(OptTrain.DEFAULT_TRAIN, target); } @@ -184,7 +185,7 @@ public OptSpy(final Path target) { * @param trn Optimizations train. * @param target Where to track optimization steps. */ - public OptSpy(final Train trn, final Path target) { + OptSpy(final Train trn, final Path target) { this.train = trn; this.target = target; } @@ -205,7 +206,7 @@ public XML apply(final XML xml) { * ready and works only with `bool` object which was removed. We * need to make this optimization great again and add to the train. */ - public static final class OptTrain implements Function { + static final class OptTrain implements Function { /** * Parsing train with XSLs. @@ -239,14 +240,14 @@ public static final class OptTrain implements Function { private final Function delegate; /** - * Shifts that we are going to apply. + * Xsline with applied shifts. */ - private final Train shifts; + private final Xsline xsline; /** * The default constructor with the default preset of xsl optimizations. */ - public OptTrain() { + OptTrain() { this(OptTrain.DEFAULT_TRAIN); } @@ -254,7 +255,7 @@ public OptTrain() { * Constructor that accepts train of shifts. * @param shifts XLS shifts. */ - public OptTrain(final Train shifts) { + OptTrain(final Train shifts) { this(xml -> xml, shifts); } @@ -263,7 +264,7 @@ public OptTrain(final Train shifts) { * @param delegate Optimizations that have to be done before. * @param xls File from classpath. */ - public OptTrain(final Function delegate, final String xls) { + OptTrain(final Function delegate, final String xls) { this( delegate, new TrDefault().with(new StClasspath(xls)) @@ -271,21 +272,38 @@ public OptTrain(final Function delegate, final String xls) { } /** - * The default constructor. + * Ctor that accepts train of shifts to apply with {@link com.yegor256.xsline.Xsline}. * @param delegate Optimizations that have to be done before. * @param shifts To apply */ - public OptTrain( + OptTrain( final Function delegate, final Train shifts + ) { + this( + delegate, + new Xsline(shifts) + ); + } + + /** + * Main ctor. + * @param delegate Optimizations that have to be done before. + * @param xsline Xsline with applied shifts. + */ + OptTrain( + final Function delegate, + final Xsline xsline ) { this.delegate = delegate; - this.shifts = shifts; + this.xsline = xsline; } @Override public XML apply(final XML xml) { - return new Xsline(this.shifts).pass(this.delegate.apply(xml)); + return this.xsline.pass( + this.delegate.apply(xml) + ); } } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java index 2dec292c99..a98476213f 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java @@ -78,12 +78,13 @@ public final class ShakeMojo extends SafeMojo { void exec() { final long start = System.currentTimeMillis(); final Collection tojos = this.scopedTojos().withOptimized(); + final Function optimization = this.optimization(); final int total = new Threaded<>( new Filtered<>( ForeignTojo::notShaken, tojos ), - tojo -> this.shaken(tojo, this.optimization()) + tojo -> this.shaken(tojo, optimization) ).total(); if (total > 0) { Logger.info( diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java index 5f6f8c7a80..77d111d198 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java @@ -162,9 +162,10 @@ public final class TranspileMojo extends SafeMojo { @Override public void exec() { final Collection sources = this.scopedTojos().withShaken(); + final Function optimization = this.transpilation(); final int saved = new Threaded<>( sources, - tojo -> this.transpiled(tojo, this.transpilation()) + tojo -> this.transpiled(tojo, optimization) ).total(); Logger.info( this, "Transpiled %d XMIRs, created %d Java files in %[file]s",