Skip to content

Commit

Permalink
[core] Add global option to flatten when appending in XmlStringBuilder
Browse files Browse the repository at this point in the history
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
70e4830 ("[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 <[email protected]>
  • Loading branch information
Flowdalic committed Nov 26, 2023
1 parent 70e4830 commit 6322f4f
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 6322f4f

Please sign in to comment.