You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@locus.apache.org on 2000/01/12 22:41:15 UTC
cvs commit: xml-xerces/java/src/org/apache/xerces/dom NodeImpl.java
andyc 00/01/12 13:41:14
Modified: java/src/org/apache/xerces/dom NodeImpl.java
Log:
Added changes to allow caching of node children length and
quicker access to individual items in the children NodeList.
For example, the following loop will see a huge performance
boost:
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
}
Revision Changes Path
1.5 +66 -15 xml-xerces/java/src/org/apache/xerces/dom/NodeImpl.java
Index: NodeImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/NodeImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NodeImpl.java 2000/01/06 23:59:29 1.4
+++ NodeImpl.java 2000/01/12 21:41:14 1.5
@@ -163,6 +163,20 @@
/** Synchronization of data needed. */
protected transient boolean syncData;
+ // other transients
+
+ /** Last change number for index caching. */
+ protected transient int nodeListChanges = -1;
+
+ /** Cached node list length. */
+ protected transient int nodeListLength;
+
+ /** Last requested node. */
+ protected transient NodeImpl nodeListNode;
+
+ /** Last requested node index. */
+ protected transient int nodeListIndex;
+
// internal data
/**
@@ -252,6 +266,22 @@
public NodeImpl() {}
//
+ // Serialization methods
+ //
+
+ /** Deserialize object. */
+ private void readObject(ObjectInputStream ois)
+ throws ClassNotFoundException, IOException {
+
+ // perform default deseralization
+ ois.defaultReadObject();
+
+ // initialize transients
+ nodeListChanges = -1;
+
+ } // readObject(ObjectInputStream)
+
+ //
// Node methods
//
@@ -988,14 +1018,17 @@
*/
public int getLength() {
- // It is assumed that the getChildNodes call synchronized
- // the children. Therefore, we can access the first child
- // reference directly.
- int count = 0;
- for (NodeImpl node = firstChild; node != null; node = node.nextSibling) {
- count++;
- }
- return count;
+ if (nodeListChanges != changes) {
+ nodeListChanges = changes;
+ nodeListLength = 0;
+ nodeListIndex = 0;
+ nodeListNode = firstChild;
+ for (NodeImpl node = firstChild; node != null; node = node.nextSibling) {
+ nodeListLength++;
+ }
+ }
+
+ return nodeListLength;
} // getLength():int
@@ -1006,15 +1039,33 @@
* @param Index int
*/
public Node item(int index) {
+
+ // short way
+ if (nodeListChanges == changes) {
+ if (nodeListIndex < index) {
+ while (nodeListIndex < index && nodeListNode != null) {
+ nodeListIndex++;
+ nodeListNode = nodeListNode.nextSibling;
+ }
+ }
+ else if (nodeListIndex > index) {
+ while (nodeListIndex > index && nodeListNode != null) {
+ nodeListIndex--;
+ nodeListNode = nodeListNode.previousSibling;
+ }
+ }
+ return nodeListNode;
+ }
- // It is assumed that the getChildNodes call synchronized
- // the children. Therefore, we can access the first child
- // reference directly.
- NodeImpl node = firstChild;
- for (int i = 0; i < index && node != null; i++) {
- node = node.nextSibling;
+ // long way
+ nodeListChanges = changes;
+ nodeListNode = firstChild;
+ for (nodeListIndex = 0;
+ nodeListIndex < index && nodeListNode != null;
+ nodeListIndex++) {
+ nodeListNode = nodeListNode.nextSibling;
}
- return node;
+ return nodeListNode;
} // item(int):Node