Skip to content

Commit

Permalink
Merge pull request #3849 from objectionary/3847
Browse files Browse the repository at this point in the history
xnav for better performance in some places
yegor256 authored Jan 25, 2025

Unverified

This user has not yet uploaded their public signing key.
2 parents 6c6cae8 + 23ac779 commit 31c194e
Showing 14 changed files with 377 additions and 214,030 deletions.
29 changes: 17 additions & 12 deletions eo-parser/pom.xml
Original file line number Diff line number Diff line change
@@ -61,6 +61,11 @@ SOFTWARE.
<artifactId>xsline</artifactId>
<!-- version from parent POM -->
</dependency>
<dependency>
<groupId>com.github.volodya-lombrozo</groupId>
<artifactId>xnav</artifactId>
<!-- version from parent POM -->
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
@@ -131,18 +136,6 @@ SOFTWARE.
<!-- version from parent POM -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.37</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.37</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
@@ -173,6 +166,18 @@ SOFTWARE.
<artifactId>mktmp</artifactId>
<!-- version from parent POM -->
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>farea</artifactId>
<version>0.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.15.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
22 changes: 15 additions & 7 deletions eo-parser/src/main/java/org/eolang/parser/Xmir.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
*/
package org.eolang.parser;

import com.github.lombrozo.xnav.Xnav;
import com.jcabi.xml.XML;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
@@ -162,7 +163,8 @@ public Collection<SAXParseException> validate(final XML xsd) {
*/
public String toEO() {
return this.converted(
Xmir.FOR_EO, "/org/eolang/parser/print/to-eo.xsl", "eo/text()"
Xmir.FOR_EO, "/org/eolang/parser/print/to-eo.xsl",
"eo"
);
}

@@ -172,7 +174,8 @@ public String toEO() {
*/
public String toReversedEO() {
return this.converted(
Xmir.FOR_EO, "/org/eolang/parser/print/to-eo-reversed.xsl", "eo/text()"
Xmir.FOR_EO, "/org/eolang/parser/print/to-eo-reversed.xsl",
"eo"
);
}

@@ -201,7 +204,7 @@ public String toPhi(final boolean conservative) {
)
)
),
"program/phi/text()"
"phi"
);
}

@@ -211,7 +214,8 @@ public String toPhi(final boolean conservative) {
*/
public String toSaltyPhi() {
return this.converted(
Xmir.FOR_PHI, "/org/eolang/parser/phi/to-salty-phi.xsl", "program/phi/text()"
Xmir.FOR_PHI, "/org/eolang/parser/phi/to-salty-phi.xsl",
"phi"
);
}

@@ -229,10 +233,14 @@ private String converted(final Train<Shift> train, final String xsl, final Strin
/**
* Converts XMIR.
* @param train Train of transformations that prepares XMIR
* @param xpath Xpath to retrieve the final result
* @param node XML node name
* @return XMIR in other representation as {@link String}.
*/
private String converted(final Train<Shift> train, final String xpath) {
return new Xsline(train).pass(this.xml).xpath(xpath).get(0);
private String converted(final Train<Shift> train, final String node) {
return new Xnav(new Xsline(train).pass(this.xml).inner())
.element("program")
.element(node)
.text()
.get();
}
}
Original file line number Diff line number Diff line change
@@ -37,11 +37,13 @@ SOFTWARE.
<xsl:output method="text" encoding="UTF-8"/>
<!-- PROGRAM -->
<xsl:template match="program">
<eo>
<xsl:apply-templates select="license"/>
<xsl:apply-templates select="metas"/>
<xsl:apply-templates select="objects"/>
</eo>
<program>
<eo>
<xsl:apply-templates select="license"/>
<xsl:apply-templates select="metas"/>
<xsl:apply-templates select="objects"/>
</eo>
</program>
</xsl:template>
<!-- LICENCE -->
<xsl:template match="license">
12 changes: 7 additions & 5 deletions eo-parser/src/main/resources/org/eolang/parser/print/to-eo.xsl
Original file line number Diff line number Diff line change
@@ -37,11 +37,13 @@ SOFTWARE.
<xsl:output method="text" encoding="UTF-8"/>
<!-- PROGRAM -->
<xsl:template match="program">
<eo>
<xsl:apply-templates select="license"/>
<xsl:apply-templates select="metas"/>
<xsl:apply-templates select="objects"/>
</eo>
<program>
<eo>
<xsl:apply-templates select="license"/>
<xsl:apply-templates select="metas"/>
<xsl:apply-templates select="objects"/>
</eo>
</program>
</xsl:template>
<!-- LICENCE -->
<xsl:template match="license">
Original file line number Diff line number Diff line change
@@ -21,68 +21,60 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
package benchmarks;

import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.StClasspath;
import java.io.IOException;
import com.jcabi.xml.XML;
import fixtures.LargeXmir;
import java.util.concurrent.TimeUnit;
import org.cactoos.io.ResourceOf;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.Main;
import org.eolang.parser.Xmir;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Benchmark for XSL transformations.
* Benchmark for XMIR to EO and to Phi transformations.
*
* @since 0.41
* @checkstyle DesignForExtensionCheck (100 lines)
* @checkstyle NonStaticMethodCheck (100 lines)
*/
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@State(Scope.Benchmark)
@Disabled
@SuppressWarnings({
"JTCOP.RuleAllTestsHaveProductionClass",
"PMD.JUnitTestClassShouldBeFinal",
"PMD.JUnit5TestShouldBePackagePrivate"
})
public class XslBenchmarkIT {
@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"})
public class XmirBench {

/**
* Pairs of XSL and worst XMIR for the XSL.
* Large XMIR document.
*/
@Param({
"/org/eolang/parser/shake/add-default-package.xsl|org/eolang/parser/benchmark/native.xmir",
"/org/eolang/parser/shake/build-fqns.xsl|org/eolang/parser/benchmark/native.xmir",
"/org/eolang/parser/shake/explicit-data.xsl|org/eolang/parser/benchmark/native.xmir"
})
private String pairs;
private static final XML XMIR = new LargeXmir("noname", "com/sun/jna/Klass.class").xml();

@Benchmark
public void apply() throws Exception {
final String[] pair = this.pairs.split("\\|");
new StClasspath(pair[0]).apply(
1, new XMLDocument(new ResourceOf(pair[1]).stream())
);
public void xmirToEO() {
new Xmir(XmirBench.XMIR).toEO();
}

@Test
@SuppressWarnings("JTCOP.LineHitterRule")
void measuresXslTransformations() throws IOException {
Main.main(new String[0]);
Assertions.assertTrue(true, "Benchmark should executed");
@Benchmark
public void xmirToReversedEo() {
new Xmir(XmirBench.XMIR).toReversedEO();
}

@Benchmark
public void xmirToPhi() {
new Xmir(XmirBench.XMIR).toPhi();
}

@Benchmark
public void xmirToSaltyPhi() {
new Xmir(XmirBench.XMIR).toSaltyPhi();
}
}
82 changes: 82 additions & 0 deletions eo-parser/src/test/java/benchmarks/XslBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 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 benchmarks;

import com.jcabi.xml.XML;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.Xsline;
import fixtures.LargeXmir;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Benchmark for XSL transformations.
*
* @since 0.41
* @checkstyle NonStaticMethodCheck (100 lines)
*/
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"})
public class XslBench {

/**
* Large XMIR document.
*/
private static final XML INPUT = new LargeXmir().xml();

/**
* All sheets to use.
*/
private static final Xsline LINE = new Xsline(
new TrClasspath<>(
"/org/eolang/parser/parse/move-voids-up.xsl",
"/org/eolang/parser/parse/validate-before-stars.xsl",
"/org/eolang/parser/parse/resolve-before-star.xsl",
"/org/eolang/parser/parse/wrap-method-calls.xsl",
"/org/eolang/parser/parse/const-to-dataized.xsl",
"/org/eolang/parser/parse/stars-to-tuples.xsl",
"/org/eolang/parser/shake/add-default-package.xsl",
"/org/eolang/parser/shake/build-fqns.xsl",
"/org/eolang/parser/shake/explicit-data.xsl"
).back()
);

@Benchmark
public final void manySheetsOnLargeXmir() {
XslBench.LINE.pass(XslBench.INPUT);
}
}
30 changes: 30 additions & 0 deletions eo-parser/src/test/java/benchmarks/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 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.
*/

/**
* Benchmarks.
*
* @since 0.51
*/
package benchmarks;
Loading

2 comments on commit 31c194e

@0pdd
Copy link

@0pdd 0pdd commented on 31c194e Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3706-fb7fb741 disappeared from eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml), that's why I closed #3746. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 31c194e Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3706-5c197f92 discovered in eo-parser/src/test/resources/org/eolang/parser/eo-typos/redundant-parentheses/simple-application.yaml) and submitted as #3851. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.