From 6322f4f826ab0dba601401b88964419b8f647594 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 26 Nov 2023 21:24:35 +0100 Subject: [PATCH] [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 70e48300a6ba ("[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 --- .../smack/util/XmlStringBuilder.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index 21b192b4c4..d71a437572 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -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 @@ 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 XmlStringBuilder condAttribute(boolean condition, String name, String val @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; }