Skip to content

Commit

Permalink
feat(objectionary#889): first optimization round
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 29, 2024
1 parent 4980987 commit 20ebb08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
35 changes: 16 additions & 19 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class XmlNode {
/**
* Parent node.
*/
private final XML node;
private final Node node;

/**
* Constructor.
Expand All @@ -56,27 +56,19 @@ public XmlNode(final String xml) {
this(new XMLDocument(xml).node().getFirstChild());
}

/**
* Constructor.
* @param parent XML document
*/
public XmlNode(final Node parent) {
this(new XMLDocument(parent));
}

/**
* Constructor.
* @param parent Parent node.
*/
public XmlNode(final XML parent) {
public XmlNode(final Node parent) {
this.node = parent;
}

@Override
public boolean equals(final Object obj) {
final boolean res;
if (obj instanceof XmlNode) {
res = this.node.equals(((XmlNode) obj).node);
res = new XMLDocument(this.node).equals(new XMLDocument(((XmlNode) obj).node));
} else {
res = false;
}
Expand Down Expand Up @@ -106,7 +98,7 @@ public Stream<XmlNode> children() {
* @return Text content.
*/
public String text() {
return this.node.node().getTextContent();
return this.node.getTextContent();
}

/**
Expand All @@ -116,7 +108,7 @@ public String text() {
*/
public Optional<String> attribute(final String name) {
final Optional<String> result;
final NamedNodeMap attrs = this.node.node().getAttributes();
final NamedNodeMap attrs = this.node.getAttributes();
if (attrs == null) {
result = Optional.empty();
} else {
Expand All @@ -140,7 +132,7 @@ XmlNode child(final String name) {
* @return List of elements.
*/
List<String> xpath(final String xpath) {
return this.node.xpath(xpath);
return new XMLDocument(this.node).xpath(xpath);
}

/**
Expand Down Expand Up @@ -206,7 +198,7 @@ boolean hasAttribute(final String name, final String value) {
* @return Class.
*/
XmlClass toClass() {
return new XmlClass(this.node.node());
return new XmlClass(this.node);
}

/**
Expand All @@ -233,9 +225,14 @@ XmlBytecodeEntry toEntry() {
*/
private Optional<XmlNode> optchild(final String name) {
Optional<XmlNode> result = Optional.empty();
final List<XML> nodes = this.node.nodes(name);
if (!nodes.isEmpty()) {
result = Optional.of(new XmlNode(nodes.get(0)));
final NodeList children = this.node.getChildNodes();
final int length = children.getLength();
for (int index = 0; index < length; ++index) {
final Node current = children.item(index);
if (current.getNodeName().equals(name)) {
result = Optional.of(new XmlNode(current));
break;
}
}
return result;
}
Expand All @@ -260,7 +257,7 @@ private IllegalStateException notFound(final String name) {
* @return Stream of class objects.
*/
private Stream<Node> objects() {
final NodeList children = this.node.node().getChildNodes();
final NodeList children = this.node.getChildNodes();
final List<Node> res = new ArrayList<>(children.getLength());
for (int index = 0; index < children.getLength(); ++index) {
final Node child = children.item(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ void failsToOpenBrokenXmirRepresentationFromFile(@TempDir final Path dir) throws
* This is a performance test, which is disabled by default.
* It is used to measure the performance of the conversion of the EO object
* into the bytecode representation and back.
* Timings before the optimization were: 21s, 23s, 21s, 21s (500 attempts)
* 1) Timings before the optimization were: 21s, 23s, 21s, 21s (500 attempts)
* 2) `XMLDocument` -> `org.w3c.Node` optimization: 11s, 10s, 11s, 10s (500 attempts)
*/
@Test
@Disabled
Expand Down

0 comments on commit 20ebb08

Please sign in to comment.