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

feat(#3519): Remove optimization package #3753

Merged
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
172 changes: 163 additions & 9 deletions eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -88,7 +93,7 @@ public final class OptimizeMojo extends SafeMojo {
public void exec() {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withXmir();
final Optimization optimization = this.optimization();
final Function<XML, XML> optimization = this.optimization();
final int total = new Threaded<>(
new Filtered<>(
ForeignTojo::notOptimized,
Expand All @@ -115,7 +120,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<XML, XML> optimization)
throws Exception {
final Path source = tojo.xmir();
final XML xmir = new XMLDocument(source);
Expand All @@ -138,18 +143,167 @@ 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<XML, XML> optimization() {
final Function<XML, XML> opt;
final Train<Shift> 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
*/
static final class OptSpy implements Function<XML, XML> {
/**
* Optimizations train.
*/
private final Train<Shift> train;

/**
* Where to track optimization steps.
*/
private final Path target;

/**
* Ctor.
* @param target Where to track optimization steps.
*/
OptSpy(final Path target) {
this(OptTrain.DEFAULT_TRAIN, target);
}

/**
* The main constructor.
* @param trn Optimizations train.
* @param target Where to track optimization steps.
*/
OptSpy(final Train<Shift> 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.
*/
static final class OptTrain implements Function<XML, XML> {

/**
* Parsing train with XSLs.
* @implNote The list of applied XSLs is adjusted during execution.
* <br>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<Shift> 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<XML, XML> delegate;

/**
* Xsline with applied shifts.
*/
private final Xsline xsline;

/**
* The default constructor with the default preset of xsl optimizations.
*/
OptTrain() {
this(OptTrain.DEFAULT_TRAIN);
}

/**
* Constructor that accepts train of shifts.
* @param shifts XLS shifts.
*/
OptTrain(final Train<Shift> 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.
*/
OptTrain(final Function<XML, XML> delegate, final String xls) {
this(
delegate,
new TrDefault<Shift>().with(new StClasspath(xls))
);
}

/**
* 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
*/
OptTrain(
final Function<XML, XML> delegate,
final Train<Shift> 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<XML, XML> delegate,
final Xsline xsline
) {
this.delegate = delegate;
this.xsline = xsline;
}

@Override
public XML apply(final XML xml) {
return this.xsline.pass(
this.delegate.apply(xml)
);
}
}
}
21 changes: 12 additions & 9 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +78,7 @@ public final class ShakeMojo extends SafeMojo {
void exec() {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withOptimized();
final Optimization optimization = this.optimization();
final Function<XML, XML> optimization = this.optimization();
final int total = new Threaded<>(
new Filtered<>(
ForeignTojo::notShaken,
Expand All @@ -107,7 +105,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<XML, XML> optimization
) throws Exception {
final Path source = tojo.optimized();
final XML xmir = new XMLDocument(source);
final String name = xmir.xpath("/program/@name").get(0);
Expand All @@ -130,12 +131,14 @@ private int shaken(final ForeignTojo tojo, final Optimization optimization) thro
*
* @return Shake optimizations
*/
private Optimization optimization() {
final Optimization opt;
private Function<XML, XML> optimization() {
final Function<XML, XML> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -163,7 +162,7 @@ public final class TranspileMojo extends SafeMojo {
@Override
public void exec() {
final Collection<ForeignTojo> sources = this.scopedTojos().withShaken();
final Optimization optimization = this.transpilation();
final Function<XML, XML> optimization = this.transpilation();
final int saved = new Threaded<>(
sources,
tojo -> this.transpiled(tojo, optimization)
Expand Down Expand Up @@ -195,12 +194,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<XML, XML> 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
Expand Down Expand Up @@ -229,8 +228,8 @@ private int transpiled(final ForeignTojo tojo, final Optimization transpilation)
* Transpile optimization.
* @return Optimization that transpiles
*/
private Optimization transpilation() {
return new OptSpy(
private Function<XML, XML> transpilation() {
return new OptimizeMojo.OptSpy(
this.measured(TranspileMojo.TRAIN),
this.targetDir.toPath().resolve(TranspileMojo.PRE)
);
Expand Down
Loading
Loading