Performance optimization techniques for Java code

30
Performance optimization techniques for Java code

description

The presentation covers the the basics of performance optimizations for real-world Java code. It starts with a theoretical overview of the concepts followed by several live demos showing how performance bottlenecks can be diagnosed and eliminated. The demos include some non-trivial multi-threaded examples inspired by real-world applications.

Transcript of Performance optimization techniques for Java code

Page 1: Performance optimization techniques for Java code

Performance optimization techniques for Java code

Page 2: Performance optimization techniques for Java code

Who am I and why should you trust me?

● Attila-Mihály Balázshttp://hype-free.blogspot.com/

● Former malware researcher (”low-level guy”)

● Current Java dev (”high level dude”)● Spent the last ~6 monts optimizing a large

(1 000 000+ LOC) legacy system● Will spend the next 6 months on it too (at

least )

Page 3: Performance optimization techniques for Java code

Question everything!

?

Page 4: Performance optimization techniques for Java code

What's this about● Core principles● Demo 1: collections framework● Demo 2, 3, 4: synchronization performance● Demo 5: ugly code, is it worth it?● Demo 6, 7, 8: playing with Strings● Conclusions● Q&A

Page 5: Performance optimization techniques for Java code

What this is not about● Selecting efficient algorithms● High level optimizations (architectural

changes)

● These are important too! (but require more effort, and we are going for the quick win here)

Page 6: Performance optimization techniques for Java code

Core principles● Performance is a balence, and endless

game of shifting bottlenecks, no silver bullets here!

CPUCPU MemoryMemory

DiskDisk NetworkNetwork

Your program

Page 7: Performance optimization techniques for Java code

Perform on all levels!● Performance has many levels:

– Compiler (JIT): 5 to 6: 100%(1)

– Memory: L1/L2 cache, main memory– Disk: cache, RAID, SSD– Network: 10Mbit, 100Mbit, 1000Mbit

● Until recently we had it easy (performance doubled every 18 months)

● Now we need to do some work(1) http://java.sun.com/performance/reference/whitepapers/6_performance.html

Page 8: Performance optimization techniques for Java code

Core principles● Measure, measure, measure! (before,

during, after). ● Try using realistic data!● Watch out for the Heisenberg effect (more

on this later)● Some things are not intuitive:

– Pop-question: if processing 1000 messages takes 1 second, how long does the processing of 1 message take?

Page 9: Performance optimization techniques for Java code

Core principles● Troughput● Latency● Thread context, context switching● Lock contention● Queueing theory● Profiling● Sampling

Page 10: Performance optimization techniques for Java code

Feasibility – ”numbers everyone should know” (2)

● L1 cache reference 0.5 ns● Branch mispredict 5 ns● L2 cache reference 7 ns● Mutex lock/unlock 100 ns● Main memory reference 100 ns● Compress 1K bytes with Zippy 10,000 ns● Send 2K bytes over 1 Gbps network 20,000 ns● Read 1 MB sequentially from memory 250,000 ns● Round trip within same datacenter 500,000 ns● Disk seek 10,000,000 ns● Read 1 MB sequentially from network 10,000,000 ns● Read 1 MB sequentially from disk 30,000,000 ns● Send packet CA->Netherlands->CA 150,000,000 n(2) http://research.google.com/people/jeff/stanford-295-talk.pdf

Page 11: Performance optimization techniques for Java code

Feasability● Amdahl's law: The speedup of a program

using multiple processors in parallel computing is limited by the time needed for the sequential fraction of the program.

Page 12: Performance optimization techniques for Java code

Course of action● Have a clear (written?), measourable goal:

operation X should take less than 100ms in 99.9% of the cases

● Measure (profile)● Is the goal met? → The End● Optimize hotspots → go to step 2

Page 13: Performance optimization techniques for Java code

Tools● VisualVM● JProfiler● YourKit

● Eclipse TPTP● Netbeans Profiler

Page 14: Performance optimization techniques for Java code

Demo 1: collections framework● Name 3 things wrong with this code:

Vector<String> v1;…if (!v1.contains(s)) { v1.add(s); }

Page 15: Performance optimization techniques for Java code

Demo 1: collections framework● Wrong data structure (list / array instead of

set), hence slooow performance for large data sets (but not for small ones!)

● Extra synchronization if used by a single thread only

● Not actually thread safe! (only ”exception safe”)

Page 16: Performance optimization techniques for Java code

Demo 1: lessons● Use existing classes● Use realistic sample data● Thread safety is hard!● Heisenberg (observer) effect

Page 17: Performance optimization techniques for Java code

Demo 2, 3, 4: synchronization performance

● If I have N units of work and use 4, it must be faster than using a single thread, right?

● What does lock contention look like?● What does a ”synchronization train(wreck)”

look like?

Page 18: Performance optimization techniques for Java code

Demo 2, 3, 4: lessons● Use existing classes

– ReadWriteLock– java.util.concurrent.*

● Use realistic sample data (too short / too long units of work)

● Sometimes throwing a threadpool at it makes it worse!

● Consider using a private copy of the variable for each thread

Page 19: Performance optimization techniques for Java code

Demo 5: ugly code, is it worth it?● Parsing a logfile

Page 20: Performance optimization techniques for Java code

Demo 5: lessons● Sometimes yes, but always profile first!

Page 21: Performance optimization techniques for Java code

Demo 6: String.substring● How are strings stored in Java?

Page 22: Performance optimization techniques for Java code

Demo 6: Lesson● You can look inside the JRE when needed!

Page 23: Performance optimization techniques for Java code

Demo 7: repetitive strings

Page 24: Performance optimization techniques for Java code

Demo 7: Lessons● You shouldn't use String.intern:

– Slow– You have to use it everywhere– Needs hand-tuning

● Use a WeakHashMap for caching (don't forget to synchronize!)

● Use String.equals (not ==)

Page 25: Performance optimization techniques for Java code

Demo 8: charsets– ASCII– ISO-8859-1– UTF-8– UTF-16

Page 26: Performance optimization techniques for Java code

Demo 8: lessons● Use UTF-8 where possible

Page 27: Performance optimization techniques for Java code

Conclusions● Measure twice, cut once● Don't trust advice you didn't test! (including

mine)● Most of the time you don't need to sacrifice

clean code for performant code

Page 28: Performance optimization techniques for Java code

Conclusions● Slides:

– Google Groups– http://hype-free.blogspot.com/– [email protected]

● Source code:– http://code.google.com/p/hype-

free/source/browse/#svn/trunk/java-perfopt-201003

● Profiler evaluation licenses

Page 29: Performance optimization techniques for Java code

Resources● https://visualvm.dev.java.net/ ● http://www.ej-technologies.com/● http://blog.ej-technologies.com/ ● http://www.yourkit.com/ ● http://www.yourkit.com/docs/index.jsp ● http://www.yourkit.com/eap/index.jsp

Page 30: Performance optimization techniques for Java code

Thank you!

Questions?