[core] Add global option to flatten when appending in XmlStringBuilder

For certain use cases, this provides a performance improvement,
probably due better cache locality. However, it comes with the cost of
additional memory consumption.

This was initially suggested by Boris Grozev, who also reported a
significant performance problem of
XmlStringBuilder/LazyStringBuilder. However, the main cause of the
performance probelm was the missing caching of LazyStringBuilder. The
length of the lazy string is now cached by LazyStringBuidler since
70e48300a6 ("[core] Cache length in LazyStringBuilder"), which
accounts for large performance improvement. A significantly smaller
improvement is achieved by this commit and setting
XmlStringBuilder.FLAT_APPEND to 'true'.

Suggested-by: Boris Grozev <boris@jitsi.org>
This commit is contained in:
Florian Schmaus 2023-11-26 21:24:35 +01:00
parent 70e48300a6
commit 6322f4f826
1 changed files with 14 additions and 2 deletions

View File

@ -1,6 +1,6 @@
/**
*
* Copyright 2014-2021 Florian Schmaus
* Copyright 2014-2023 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,6 +33,8 @@ import org.jxmpp.util.XmppDateTime;
public class XmlStringBuilder implements Appendable, CharSequence, Element {
public static final String RIGHT_ANGLE_BRACKET = Character.toString('>');
public static final boolean FLAT_APPEND = false;
private final LazyStringBuilder sb;
private final XmlEnvironment effectiveXmlEnvironment;
@ -596,7 +598,17 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
@Override
public XmlStringBuilder append(CharSequence csq) {
assert csq != null;
sb.append(csq);
if (FLAT_APPEND) {
if (csq instanceof XmlStringBuilder) {
sb.append(((XmlStringBuilder) csq).sb);
} else if (csq instanceof LazyStringBuilder) {
sb.append((LazyStringBuilder) csq);
} else {
sb.append(csq);
}
} else {
sb.append(csq);
}
return this;
}