[QFJ-740] Replace StringBuffer with StringBuilder Created: 24/Apr/13  Updated: 02/Apr/15  Resolved: 18/Mar/14

Status: Closed
Project: QuickFIX/J
Component/s: Engine
Affects Version/s: 1.5.3
Fix Version/s: 1.6.0

Type: Improvement Priority: Default
Reporter: Peter Lawrey Assignee: Christoph John
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to QFJ-736 performance improvement: Modify the c... Closed

 Description   

In 2004 Java 5.0 was released with a note in StringBuffer's Javadoc

> As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Perhaps its is time to use StringBuilder as it is a drop in replacement. esp as this class is used heavily.



 Comments   
Comment by Christoph John [ 24/Apr/13 ]

As it says in the doc, we need to carefully check which places are single-threaded before replacing everything by StringBuilder.

Comment by Peter Lawrey [ 24/Apr/13 ]

Can you give me an example of when using StringBuffer in a multi-threaded context makes sense.

These methods call only be called by one thread at a time safely, so using StringBuffer doesn't buy anything.

ObjectNameFactory.addProperty(String name, String value)

SessionSessing.getToken(Reader)

In all other cases StringBuffer is used as a local variable, i.e. thread local variable.

Comment by Peter Lawrey [ 24/Apr/13 ]

The ObjectNameFactory.addProperty() method makes for an interesting puzzle.

Write a program which will generate all the possible outcomes of

[code]
// thread 1
objectNameFactory.addProperty("a", "b");
// thread 2
objectNameFactory.addProperty("c", "d");
[code]

You have the obvious

[code]
a=b,c=d
c=d,a=b
[code]

but also

[code]
ac==bd
a,c=b=d
[code]

and many more

Comment by Peter Lawrey [ 24/Apr/13 ]

I have generated the list

a=b,c=d
a=,bc=d
a=,cb=d
a=,c=bd
a,=bc=d
a,=cb=d
a,=c=bd
a,c=b=d
a,c==bd
c=d,a=b
c=,da=b
c=,ad=b
c=,a=db
c,=da=b
c,=ad=b
c,=a=db
c,a=d=b
c,a==db
a=bc=d
a=cb=d
a=c=bd
ac=b=d
ac==bd
c=da=b
c=ad=b
c=a=db
ca=d=b
ca==db

Comment by Christoph John [ 25/Apr/13 ]

Good point. To be honest I did not check the code on usages of StringBuffer yet. I was only aware of the usage referred to in QFJ-736.

Comment by Christoph John [ 18/Mar/14 ]

http://sourceforge.net/p/quickfixj/code/1152/

Generated at Sat Nov 23 01:18:53 UTC 2024 using JIRA 7.5.2#75007-sha1:9f5725bb824792b3230a5d8716f0c13e296a3cae.