Skip to content

Commit 87fe37a

Browse files
Avoid intermediate String allocation when cloning Characters nodes
Add a getBuffer() accessor to CharBufferNode so that deepCloneNode() can copy directly from char[] to char[] via the Characters constructor, instead of going through toString() and toCharArray().
1 parent 08bbac6 commit 87fe37a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/nu/validator/htmlparser/sax/SAXTreeBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ private void deepCloneNode(Node node, ParentNode destination) throws SAXExceptio
236236
break;
237237
case CHARACTERS:
238238
// Clone the characters
239-
String text = node.toString();
240-
Characters cloneChars = new Characters(null, text.toCharArray(), 0, text.length());
239+
Characters srcChars = (Characters) node;
240+
char[] buf = srcChars.getBuffer();
241+
Characters cloneChars = new Characters(null, buf, 0, buf.length);
241242
destination.appendChild(cloneChars);
242243
break;
243244
default:

src/nu/validator/saxtree/CharBufferNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public abstract class CharBufferNode extends Node {
5050
System.arraycopy(buf, start, buffer, 0, length);
5151
}
5252

53+
/**
54+
* Returns the buffer.
55+
* @return the buffer
56+
*/
57+
public char[] getBuffer() {
58+
return buffer;
59+
}
60+
5361
/**
5462
* Returns the wrapped buffer as a string.
5563
*

0 commit comments

Comments
 (0)