Skip to content

Commit

Permalink
remove optimization instantiation at every call in threaded, encapsul…
Browse files Browse the repository at this point in the history
…ated Xsline in OptTrain, made Opt** package private
  • Loading branch information
ArtemGet committed Dec 25, 2024
1 parent 7e88ff2 commit 61c4c10
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
46 changes: 32 additions & 14 deletions eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ public final class OptimizeMojo extends SafeMojo {
public void exec() {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withXmir();
final Function<XML, XML> 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(
Expand Down Expand Up @@ -160,7 +161,7 @@ private Function<XML, XML> optimization() {
* Optimization that spies.
* @since 0.68.0
*/
public static final class OptSpy implements Function<XML, XML> {
static final class OptSpy implements Function<XML, XML> {
/**
* Optimizations train.
*/
Expand All @@ -175,7 +176,7 @@ public static final class OptSpy implements Function<XML, XML> {
* Ctor.
* @param target Where to track optimization steps.
*/
public OptSpy(final Path target) {
OptSpy(final Path target) {
this(OptTrain.DEFAULT_TRAIN, target);
}

Expand All @@ -184,7 +185,7 @@ public OptSpy(final Path target) {
* @param trn Optimizations train.
* @param target Where to track optimization steps.
*/
public OptSpy(final Train<Shift> trn, final Path target) {
OptSpy(final Train<Shift> trn, final Path target) {
this.train = trn;
this.target = target;
}
Expand All @@ -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<XML, XML> {
static final class OptTrain implements Function<XML, XML> {

/**
* Parsing train with XSLs.
Expand Down Expand Up @@ -239,22 +240,22 @@ public static final class OptTrain implements Function<XML, XML> {
private final Function<XML, XML> delegate;

/**
* Shifts that we are going to apply.
* Xsline with applied shifts.
*/
private final Train<Shift> shifts;
private final Xsline xsline;

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

/**
* Constructor that accepts train of shifts.
* @param shifts XLS shifts.
*/
public OptTrain(final Train<Shift> shifts) {
OptTrain(final Train<Shift> shifts) {
this(xml -> xml, shifts);
}

Expand All @@ -263,29 +264,46 @@ public OptTrain(final Train<Shift> shifts) {
* @param delegate Optimizations that have to be done before.
* @param xls File from classpath.
*/
public OptTrain(final Function<XML, XML> delegate, final String xls) {
OptTrain(final Function<XML, XML> delegate, final String xls) {
this(
delegate,
new TrDefault<Shift>().with(new StClasspath(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<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.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)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ public final class ShakeMojo extends SafeMojo {
void exec() {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withOptimized();
final Function<XML, XML> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ public final class TranspileMojo extends SafeMojo {
@Override
public void exec() {
final Collection<ForeignTojo> sources = this.scopedTojos().withShaken();
final Function<XML, XML> 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",
Expand Down

0 comments on commit 61c4c10

Please sign in to comment.