I plan to do some refactorings on String/StringBuffer/StringBuilder usage.

I hope to lower memory usage for temporal object creation (the objects created within a single request) / GC overhead and maybe increase performance.
Optimizations:

  • StringBuffer (thread safe) -> StringBuilder (faster, not thread safe) where possible
  • Set/optimize initial capacity for StringBuffer/StringBuilder where possible preventing string arrays to be recreated/copied in the buffer/builder internally
  • Use String.intern() if a string is likely to be used repeatedly
  • Replace String concatenations over multiple statements into StringBuilder (or StringBuffer)
  • Review our Encoders.encodeTemplateId method which gets invoked hundreds of times within a single request, performing MD5-hashing each time.

Performance measurements

Using the benchmark script developed by Chris Gersen, I will perform some tests on 2 pages using a local yellowgrass instance.

Test configuration

CPU scheduler was set to performance, not switching CPU speeds.

PERMSIZE=“256M”
HEAPSIZE=“2g”
CONTINUE_PATH=“$(basename $1 .cfg).continue”
WARS=find ./tests/*.war 2>/dev/null
dbserver=‘localhost’
dbname=‘mytestdb’
JAVA_HOME=“/usr/lib/jvm/java-7-oracle”
CATALINA_HOME=/home/elmer/apache-tomcat
SQLS=find ./tests/*.sql.gz 2>/dev/null
WARMUP=200
ITERATIONS=5000
PAGES=“issue/Intel/8 project/Intel”
RUNNINGTOMCAT=0
BASEURL=“http://localhost:8080/
SESSIONSQL=“SELECT id FROM _SessionManager LIMIT 1;”
REQUEST_TIMEOUT=900
REDUCE_DEPLOYS=0

Results

Before optimization

Avg Heap/req (MB) Avg req time (ms) 80% req times < (ms)
issue page 32.53 98 98
project page 9.43 11 12

Refactoring 1: StringBuffer -> StringBuilder

note: The system was not restarted, which renders this result kinda useless wrt request time measurements (may relate to OS caches)

Avg Heap/req (MB) Avg req time (ms) 80% req times < (ms)
issue page 32.55 99 100
project page 9.43 12 12

Refactoring 2: Set initialize capacity for StringBuilder/StringBuffer instances

With system restarted:

Avg Heap/req (MB) Avg req time (ms) 80% req times < (ms)
issue page 32.24 97 98
project page 9.27 11 12

Illustrative result, without system restart:

Avg Heap/req (MB) Avg req time (ms) 80% req times < (ms)
issue page 32.24 101 102
project page 9.29 12 12
Submitted by Elmer van Chastelet on 14 May 2013 at 08:53

On 14 May 2013 at 08:53 Elmer van Chastelet tagged performance

Log in to post comments