Skip to content

Commit

Permalink
Calculate list of child elements in DomReader only once. Closes #342.
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Sep 26, 2023
1 parent 59b50d8 commit dbe845d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ <h2>Minor changes</h2>
<ul>
<li>GHPR:#331, GHI:#326: Fix handling of empty java.util.concurrent.atomic.AtomicReference (by Alex Blekhman of Atlassian).</li>
<li>GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).</li>
<li>GHI:#342: Optimize internal handling of children in DomReader avoiding O(n²) access times for siblings (by Shiang-Yun Yang).</li>
</ul>

<h1 id="1.4.20">1.4.20</h1>
Expand Down
24 changes: 22 additions & 2 deletions xstream/src/java/com/thoughtworks/xstream/io/xml/DomReader.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (C) 2004, 2005, 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2011, 2014, 2015 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2011, 2014, 2015, 2023 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 07. March 2004 by Joe Walnes
*/
package com.thoughtworks.xstream.io.xml;
Expand All @@ -21,6 +21,7 @@
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.thoughtworks.xstream.core.util.FastStack;
import com.thoughtworks.xstream.io.naming.NameCoder;


Expand All @@ -29,6 +30,7 @@ public class DomReader extends AbstractDocumentReader {
private Element currentElement;
private final StringBuilder textBuffer;
private List<Element> childElements;
private final FastStack<List<Element>> childrenStack;

public DomReader(final Element rootElement) {
this(rootElement, new XmlFriendlyNameCoder());
Expand All @@ -44,6 +46,8 @@ public DomReader(final Document document) {
public DomReader(final Element rootElement, final NameCoder nameCoder) {
super(rootElement, nameCoder);
textBuffer = new StringBuilder();
childrenStack = new FastStack<>(16);
collectChildElements();
}

/**
Expand Down Expand Up @@ -130,6 +134,9 @@ protected int getChildCount() {
@Override
protected void reassignCurrentElement(final Object current) {
currentElement = (Element)current;
}

private void collectChildElements() {
final NodeList childNodes = currentElement.getChildNodes();
childElements = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Expand All @@ -140,6 +147,19 @@ protected void reassignCurrentElement(final Object current) {
}
}

@Override
public void moveDown() {
super.moveDown();
childrenStack.push(childElements);
collectChildElements();
}

@Override
public void moveUp() {
childElements = childrenStack.pop();
super.moveUp();
}

@Override
public String peekNextChild() {
final NodeList childNodes = currentElement.getChildNodes();
Expand Down

0 comments on commit dbe845d

Please sign in to comment.