[PATCH] Use StringBuilder instead of StringBuffer for performance gains
Tomas-Kraus opened this issue · comments
The current FastInfoset code uses the StringBuffer class for building strings.
The StringBuffer class is synchronised, which doesn't seem to be needed in this case, as the involved decoder, parser and various algorithm classes aren't meant to be thread-safe anyway and their use depends on external synchronization (correct me if I'm wrong).
Since the FastInfoset project requires Java >= 1.5 (according to its pom.xml), we can use the StringBuilder class which has an almost identical API and which by itself doesn't involve synchronization. This should give small performance gains.
Affected Versions
[1.2.12]
- Issue Imported From: https://github.com/javaee/metro-fi/issues/48
- Original Issue Raised By:@glassfishrobot
- Original Issue Assigned To: @glassfishrobot
@glassfishrobot Commented
Reported by olo
@glassfishrobot Commented
olo said:
I don't seem to have permissions required to attach a patch in this JIRA, so I've published a full FastInfoset repo on GitHub, with the change covered by this issue in this commit:
@glassfishrobot Commented
snajper said:
Hi, thanks for looking into this. I commited some small changes that are expected to be safe, however most of these are api changes and would require somewhat more safer approach.
@glassfishrobot Commented
olo said:
If a concrete class is specified as an element of the API on public and protected method signatures, there's no really safe approach.
Wouldn't it be a good idea to make a major version number increment and clean this API up by substituting StringBuffer class with an appropriate interface (Appendable?) in those signatures?
@glassfishrobot Commented
olo said:
BTW, the changes that I'd envision would be something like this:
diff --git a/code/fastinfoset/src/main/java/org/jvnet/fastinfoset/EncodingAlgorithm.java b/code/fastinfoset/src/main/java/org/jvnet/fastinfoset/EncodingAlgorithm.java
index b7a016b..667efe3 100755
--- a/code/fastinfoset/src/main/java/org/jvnet/fastinfoset/EncodingAlgorithm.java
+++ b/code/fastinfoset/src/main/java/org/jvnet/fastinfoset/EncodingAlgorithm.java
@@ -33,6 +33,6 @@ public interface EncodingAlgorithm {
public Object convertFromCharacters(char[] ch, int start, int length) throws EncodingAlgorithmException;
- public void convertToCharacters(Object data, StringBuilder s) throws EncodingAlgorithmException;
+ public <T extends Appendable & CharSequence> void convertToCharacters(Object data, T s) throws EncodingAlgorithmException;
}
@glassfishrobot Commented
This issue was imported from java.net JIRA FI-48