Skip to content

Commit 415f6da

Browse files
ujay68cowtowncoder
authored andcommitted
Replaced slow string concatenation with faster StringBuilder (#323)
* Replaced slow string concatenation with faster StringBuilder * Replaced slow string concatenation with faster StringBuilder: update: don't allocate StringBuilder until the second string
1 parent 6fefce7 commit 415f6da

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -377,40 +377,43 @@ private final String _collectUntilTag() throws XMLStreamException
377377
return "";
378378
}
379379

380-
String text = null;
380+
CharSequence chars = null;
381381
while (true) {
382382
switch (_xmlReader.next()) {
383-
case XMLStreamConstants.START_ELEMENT:
384-
return (text == null) ? "" : text;
385-
386-
case XMLStreamConstants.END_ELEMENT:
387-
case XMLStreamConstants.END_DOCUMENT:
388-
// 04-May-2018, tatu: For 3.0 we can actually start exposing <tag></tag> ALSO
389-
// as `null`, as long as we default `String` null handling to coerce that to
390-
// "empty"
391-
if (text == null) {
392-
if (FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL.enabledIn(_formatFeatures)) {
393-
return null;
383+
case XMLStreamConstants.START_ELEMENT:
384+
return chars == null ? "" : chars.toString();
385+
386+
case XMLStreamConstants.END_ELEMENT:
387+
case XMLStreamConstants.END_DOCUMENT:
388+
// 04-May-2018, tatu: For 3.0 we can actually start exposing <tag></tag> ALSO
389+
// as `null`, as long as we default `String` null handling to coerce that to
390+
// "empty"
391+
if (chars == null) {
392+
if (FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL.enabledIn(_formatFeatures)) {
393+
return null;
394+
}
395+
return "";
394396
}
395-
return "";
396-
}
397-
return text;
397+
return chars.toString();
398398

399-
// note: SPACE is ignorable (and seldom seen), not to be included
400-
case XMLStreamConstants.CHARACTERS:
401-
case XMLStreamConstants.CDATA:
402-
// 17-Jul-2017, tatu: as per [dataformat-xml#236], need to try to...
399+
// note: SPACE is ignorable (and seldom seen), not to be included
400+
case XMLStreamConstants.CHARACTERS:
401+
case XMLStreamConstants.CDATA:
402+
// 17-Jul-2017, tatu: as per [dataformat-xml#236], need to try to...
403403
{
404404
String str = _getText(_xmlReader);
405-
if (text == null) {
406-
text = str;
407-
} else {
408-
text += str;
405+
if (chars == null) {
406+
chars = str;
407+
} else {
408+
if (chars instanceof String) {
409+
chars = new StringBuilder(chars);
410+
}
411+
((StringBuilder)chars).append(str);
409412
}
410413
}
411414
break;
412-
default:
413-
// any other type (proc instr, comment etc) is just ignored
415+
default:
416+
// any other type (proc instr, comment etc) is just ignored
414417
}
415418
}
416419
}

0 commit comments

Comments
 (0)