SlideShare a Scribd company logo
Java Performance Tips
Kai Chan
SoCal Code Camp, June 2014
http://bit.ly/sdcodecamp2014java
This Talk
● is about…
o several Java performance tips I have learned
● is NOT about…
o language war
o exhaustive list
o really in-depth analysis
The Usual Caveats
● performance not your only concern
● opportunity cost
● premature optimization
● test in your environment
String Manipulation
Ways to Concatenate Strings
● operators (+ and +=)
● String’s concat method
● StringBuilder
String s = a + b + c + d
same bytecode as:
String s = new
StringBuilder().append(a).append(b)
.append(c).append(d).toString()
String s = s + a + b + c + d
String s += a + b + c + d
same bytecode as:
String s = new
StringBuilder().append(s).append(a)
.append(b).append(c).append(d).toSt
ring()
String s =
a.concat(b).concat(c).concat(d)
String s =
s.concat(a).concat(b).concat(c)
.concat(d)
● not translated to use StringBuilder
● only accepts String, can’t call method of null
StringBuilder result = new
StringBuilder();
while (stillRunning()) {
…
result.append(a);
…
}
String result = “”;
while (stillRunning()) {
…
result += a;
…
}
String result = “”;
while (stillRunning()) {
…
result = new StringBuilder()
.append(s).append(a).toString();
…
}
String result = “”;
while (stillRunning()) {
…
result = new StringBuilder()
.append(s).append(a).toString();
…
}
copy characters from
String to StringBuilder
copy characters from
StringBuilder to String
create a new object
String Concatenation Example
● 100000 iterations
● a 1 byte String appended per iteration
● 100 KB of data appended
// initialize result variable
…
for (int i = 1; i <= 100000; i++) {
// append 1-byte string to result
}
+ or += operator
● iteration i:
o copy (i - 1) bytes from String to StringBuilder
o append 1 byte to StringBuilder
o copy i bytes from StringBuilder to String
● total: copy 1.4 GB of data
String’s concat method
● iteration i:
o copy (i - 1) bytes from the old String, plus 1 byte to
append with, to a new String
● total: copy 672 MB of data
StringBuilder
● when needed to expand
o create a new buffer at least 2x as large
o copy data over
● to grow the buffer from 1 byte to >= 100000
bytes: copy 128 KB of data
● plus, copy 98 KB of data from outside
StringBuilder
● total: copy 226 KB of data
CharSequence
● implemented by
String, StringBuffer, StringBuilder
● methods
o charAt
o length
o subSequence
● does not define equals and hashCode
CharSequence
● Writer
o append(CharSequence)
o append(CharSequence, int, int) // seq, start, end
● CharSequenceReader
public String getText() {
StringBuilder result = new
StringBuilder();
…
return result.toString();
}
copies all characters in the StringBuilder
to a String
public CharSequence getText() {
StringBuilder result = new
StringBuilder();
…
return result;
}
Primitive Collections
● no direct support
o cannot have List<int>, Map<int, long>
● indirect support via boxing
o “wrapper” classes, e.g. Integer, Long
o can have List<Integer>, Map<Integer, Long>
● memory cost
Primitives Collection in Java
Object Metadata
● object’s class
● lock/synchronization state
● array size (if the object is an array)
● total: ~8-12 bytes for 32-bit JVM
(more for 64-bit JVM)
metadat
a
element 999element 0
12 bytes 4 bytes 4 bytes 4 bytes
…
int[] with 1000 Elements
element 1
size = 4012 bytes
ArrayList<Integer> w/ 1000 Elements
metadat
a
size
8 bytes 4 bytes 4 or 8 bytes
metadata
value
metadata
value
metadata
value
8 bytes 8 bytes 8 bytes
4 bytes 4 bytes 4 bytes
size = 16028 bytes (if OOP size = 32 bits)
size = 20032 bytes (if OOP size = 64 bits)
metadat
a
element 999element 0
12 bytes 4 or 8 bytes 4 or 8 bytes 4 or 8 bytes
…element 1
elementData
Map: Even More Overhead
● both key and value can be boxed primitives
● some types of map half-full by design
● e.g. HashMap<Integer, Integer>
Primitive Collection Implementations
● Trove
o http://trove.starlight-systems.com/
o does NOT implement Java’s Collection interface
o iteration by callback object/method
Primitive Collection Implementations
● fastutil
o http://fastutil.di.unimi.it/
o type-specific maps, sets, lists
o sorted maps and sets
o implement Java’s Collection interface
o primitive iterators
o latest version’s binary JAR file is 17 MB
Convert Primitive Array To List
● you have an int[]
● some method only takes a List
(e.g. List<Integer>)
● Arrays.asList(int[]): type mismatch
// someFunc takes a List, read-only
// a: int[]
list = new
ArrayList<Integer>(a.length);
for(int i = 0; i < a.length; i++){
list.add(a[i]);
}
someFunc(list);
// someFunc takes a List, read-only
// a: int[]
someFunc(Ints.asList(a));
● Ints.asList: returns a fixed-size list backed by
the specified array
● Ints provided by Guava (from Google)
Regular Expression
Regular Expression
// input, regex: String
input.matches(regex)
does the following:
Pattern.compile(regex)
.matcher(input)
.matches()
while (stillRunning()) {
…
// str and regex: String
if (str.matches(regex)) {
…
}
…
}
while (stillRunning()) {
…
// str and regex: String
if (Pattern.compile(regex)
.matcher(input)
.matches()) {
…
}
…
}
while (stillRunning()) {
…
// str and regex: String
if (Pattern.compile(regex)
.matcher(input)
.matches()) {
…
}
…
}
generate a Pattern object from the RE pattern
(expensive)
java.util.regex.Pattern
● feature-rich
● not the fastest
RE Benchmark
● Java Regular expression library benchmarks
o http://tusker.org/regex/regex_benchmark.html
● many alternative implementations
● speed and capability vary
BRICS Automaton
● http://www.brics.dk/automaton/
● much faster than java.util.regex.Pattern
● basic functionality
● adopted by Lucene 4.x for RE query
Caching
Caching: Example Scenario
● calculate file checksums
● calculation is expensive
● lots of files
● repeated calculations
● solution: cache the checksum
o key: file path
Possible Solution: HashMap
● implement cache with
HashMap<String, byte[]>
● problem:
o assume cache will be “small enough”, or
o need to remove entries (how?)
Possible Solution: WeakHashMap
● implement cache with
WeakHashMap<String, byte[]>
● problem:
o keys are weak references
o if no strong reference on a key,
the entry is eligible for GC
Solution: Libraries
● Apache Commons Collections
o LRUMap class
o LRU (least recently updated) cache algorithm
o fixed max size
Solution: Libraries (cont.)
● Guava
o CacheBuilder and Cache classes
o accessible by multiple threads
o eviction by: max size, access time, write time, weak
keys, weak values, soft values
o CacheLoader class: creates entry on demand
o remove listeners
Data Compression
Lossless Compression Today
● compression saves more space than ever
● store more in faster medium (memory, SSD)
● compression ! == slow anymore
● new algorithms target high throughput
o > 300 MB/s compression
o > 1000 MB/s decompression
LZ4
● faster than Deflate with zlib
● decent ratio
● LZ4_HC
o slower compression speed
o higher compression ratio
o decoding side unchanged
● ported to Java
o https://github.com/jpountz/lz4-java
Memory-Mapped File
Memory-Mapped File Intro
● MappedByteBuffer
● content: memory-mapped region of a file
● OS handles reading, writing, caching
● can be used as persistent, shared memory
Memory-Mapped File Caveats
● file size limited by address space
o 64-bit JVM: not an issue
o 32-bit JVM: file size limited to <= 2 GB
● no “close function”
o you can’t control when resources are free
o not suitable for opening many files at/around the
same time
Performance Measurement
VisualVM
● bundled with Java SE
● local or remote (JMX) applications
● monitor
o CPU usage
o heap and PermGen usage
o class and thread counts
VisualVM
● heap dump
o instance and size by class
o biggest object by retain size
● sampler (fast)
● profiler (slow)
Sampling Example
● run 4 methods
o concatByAppendresult.append(x());
o concatByConcatresult =
result.concat(x());
o concatByPlusOperatorresult = result + x();
o concatByPlusEqualOperatorresult += x();
● if same speed, each would get 25% CPU
Thanks for Coming!
● slides available
o http://bit.ly/sdcodecamp2014java
● please vote for my conference session
o http://bit.ly/tvnews2014
● questions/feedback
o kai@ssc.ucla.edu
● questions?

More Related Content

What's hot

Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
Abdullah khawar
 
Introduction to JSON & Ajax
Introduction to JSON & AjaxIntroduction to JSON & Ajax
Introduction to JSON & Ajax
Seble Nigussie
 
File handling
File handlingFile handling
File handling
Nilesh Dalvi
 
Avro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSONAvro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSON
Alexandre Victoor
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced TopicsCésar Rodas
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
krishna partiwala
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
Dr .Ahmed Tawwab
 
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkSpark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Zalando Technology
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
TO THE NEW | Technology
 
Make Your Data Searchable With Solr in 25 Minutes
Make Your Data Searchable With Solr in 25 MinutesMake Your Data Searchable With Solr in 25 Minutes
Make Your Data Searchable With Solr in 25 Minutes
UCLA Social Sciences Computing
 
odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)
Mohamed Magdy
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Parquet Twitter Seattle open house
Parquet Twitter Seattle open houseParquet Twitter Seattle open house
Parquet Twitter Seattle open house
Julien Le Dem
 
Java Data Migration with Data Pipeline
Java Data Migration with Data PipelineJava Data Migration with Data Pipeline
Java Data Migration with Data PipelineNorth Concepts
 
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander KorotkovPostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
Nikolay Samokhvalov
 
MongoDB
MongoDBMongoDB
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Nosh Petigara
 

What's hot (20)

Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
Introduction to JSON & Ajax
Introduction to JSON & AjaxIntroduction to JSON & Ajax
Introduction to JSON & Ajax
 
File handling
File handlingFile handling
File handling
 
Avro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSONAvro, la puissance du binaire, la souplesse du JSON
Avro, la puissance du binaire, la souplesse du JSON
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced Topics
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkSpark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
04 standard class library c#
04 standard class library c#04 standard class library c#
04 standard class library c#
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Make Your Data Searchable With Solr in 25 Minutes
Make Your Data Searchable With Solr in 25 MinutesMake Your Data Searchable With Solr in 25 Minutes
Make Your Data Searchable With Solr in 25 Minutes
 
odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Parquet Twitter Seattle open house
Parquet Twitter Seattle open houseParquet Twitter Seattle open house
Parquet Twitter Seattle open house
 
Java Data Migration with Data Pipeline
Java Data Migration with Data PipelineJava Data Migration with Data Pipeline
Java Data Migration with Data Pipeline
 
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander KorotkovPostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
PostgreSQL Moscow Meetup - September 2014 - Oleg Bartunov and Alexander Korotkov
 
MongoDB
MongoDBMongoDB
MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Similar to Java Performance Tips (So Code Camp San Diego 2014)

Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdf
RomanKhavronenko
 
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
Shaul Rosenzwieg
 
Improving go-git performance
Improving go-git performanceImproving go-git performance
Improving go-git performance
source{d}
 
Scientific computing on jruby
Scientific computing on jrubyScientific computing on jruby
Scientific computing on jruby
Prasun Anand
 
Sorry - How Bieber broke Google Cloud at Spotify
Sorry - How Bieber broke Google Cloud at SpotifySorry - How Bieber broke Google Cloud at Spotify
Sorry - How Bieber broke Google Cloud at Spotify
Neville Li
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
Jorg Janke
 
Fosdem2017 Scientific computing on Jruby
Fosdem2017  Scientific computing on JrubyFosdem2017  Scientific computing on Jruby
Fosdem2017 Scientific computing on Jruby
Prasun Anand
 
SOSCON 2016 JerryScript
SOSCON 2016 JerryScriptSOSCON 2016 JerryScript
SOSCON 2016 JerryScript
Samsung Open Source Group
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
apidays
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineDataWorks Summit
 
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Gruter
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
JavaDayUA
 
Neo4j after 1 year in production
Neo4j after 1 year in productionNeo4j after 1 year in production
Neo4j after 1 year in production
Andrew Nikishaev
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
Amund Tveit
 
The Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization OpportunitiesThe Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization Opportunities
Databricks
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
x1 ichi
 

Similar to Java Performance Tips (So Code Camp San Diego 2014) (20)

Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdf
 
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
 
Improving go-git performance
Improving go-git performanceImproving go-git performance
Improving go-git performance
 
Scientific computing on jruby
Scientific computing on jrubyScientific computing on jruby
Scientific computing on jruby
 
Sorry - How Bieber broke Google Cloud at Spotify
Sorry - How Bieber broke Google Cloud at SpotifySorry - How Bieber broke Google Cloud at Spotify
Sorry - How Bieber broke Google Cloud at Spotify
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Fosdem2017 Scientific computing on Jruby
Fosdem2017  Scientific computing on JrubyFosdem2017  Scientific computing on Jruby
Fosdem2017 Scientific computing on Jruby
 
SOSCON 2016 JerryScript
SOSCON 2016 JerryScriptSOSCON 2016 JerryScript
SOSCON 2016 JerryScript
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
 
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized EngineApache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
Apache Tajo: Query Optimization Techniques and JIT-based Vectorized Engine
 
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
Hadoop Summit 2014: Query Optimization and JIT-based Vectorized Execution in ...
 
8.4 Upcoming Features
8.4 Upcoming Features 8.4 Upcoming Features
8.4 Upcoming Features
 
Java 8
Java 8Java 8
Java 8
 
Save Java memory
Save Java memorySave Java memory
Save Java memory
 
Neo4j after 1 year in production
Neo4j after 1 year in productionNeo4j after 1 year in production
Neo4j after 1 year in production
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
The Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization OpportunitiesThe Parquet Format and Performance Optimization Opportunities
The Parquet Format and Performance Optimization Opportunities
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
 

Recently uploaded

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 

Java Performance Tips (So Code Camp San Diego 2014)

  • 1. Java Performance Tips Kai Chan SoCal Code Camp, June 2014 http://bit.ly/sdcodecamp2014java
  • 2. This Talk ● is about… o several Java performance tips I have learned ● is NOT about… o language war o exhaustive list o really in-depth analysis
  • 3. The Usual Caveats ● performance not your only concern ● opportunity cost ● premature optimization ● test in your environment
  • 5. Ways to Concatenate Strings ● operators (+ and +=) ● String’s concat method ● StringBuilder
  • 6. String s = a + b + c + d same bytecode as: String s = new StringBuilder().append(a).append(b) .append(c).append(d).toString()
  • 7. String s = s + a + b + c + d String s += a + b + c + d same bytecode as: String s = new StringBuilder().append(s).append(a) .append(b).append(c).append(d).toSt ring()
  • 8. String s = a.concat(b).concat(c).concat(d) String s = s.concat(a).concat(b).concat(c) .concat(d) ● not translated to use StringBuilder ● only accepts String, can’t call method of null
  • 9. StringBuilder result = new StringBuilder(); while (stillRunning()) { … result.append(a); … }
  • 10. String result = “”; while (stillRunning()) { … result += a; … }
  • 11. String result = “”; while (stillRunning()) { … result = new StringBuilder() .append(s).append(a).toString(); … }
  • 12. String result = “”; while (stillRunning()) { … result = new StringBuilder() .append(s).append(a).toString(); … } copy characters from String to StringBuilder copy characters from StringBuilder to String create a new object
  • 13. String Concatenation Example ● 100000 iterations ● a 1 byte String appended per iteration ● 100 KB of data appended // initialize result variable … for (int i = 1; i <= 100000; i++) { // append 1-byte string to result }
  • 14. + or += operator ● iteration i: o copy (i - 1) bytes from String to StringBuilder o append 1 byte to StringBuilder o copy i bytes from StringBuilder to String ● total: copy 1.4 GB of data
  • 15. String’s concat method ● iteration i: o copy (i - 1) bytes from the old String, plus 1 byte to append with, to a new String ● total: copy 672 MB of data
  • 16. StringBuilder ● when needed to expand o create a new buffer at least 2x as large o copy data over ● to grow the buffer from 1 byte to >= 100000 bytes: copy 128 KB of data ● plus, copy 98 KB of data from outside StringBuilder ● total: copy 226 KB of data
  • 17. CharSequence ● implemented by String, StringBuffer, StringBuilder ● methods o charAt o length o subSequence ● does not define equals and hashCode
  • 18. CharSequence ● Writer o append(CharSequence) o append(CharSequence, int, int) // seq, start, end ● CharSequenceReader
  • 19. public String getText() { StringBuilder result = new StringBuilder(); … return result.toString(); } copies all characters in the StringBuilder to a String
  • 20. public CharSequence getText() { StringBuilder result = new StringBuilder(); … return result; }
  • 22. ● no direct support o cannot have List<int>, Map<int, long> ● indirect support via boxing o “wrapper” classes, e.g. Integer, Long o can have List<Integer>, Map<Integer, Long> ● memory cost Primitives Collection in Java
  • 23. Object Metadata ● object’s class ● lock/synchronization state ● array size (if the object is an array) ● total: ~8-12 bytes for 32-bit JVM (more for 64-bit JVM)
  • 24. metadat a element 999element 0 12 bytes 4 bytes 4 bytes 4 bytes … int[] with 1000 Elements element 1 size = 4012 bytes
  • 25. ArrayList<Integer> w/ 1000 Elements metadat a size 8 bytes 4 bytes 4 or 8 bytes metadata value metadata value metadata value 8 bytes 8 bytes 8 bytes 4 bytes 4 bytes 4 bytes size = 16028 bytes (if OOP size = 32 bits) size = 20032 bytes (if OOP size = 64 bits) metadat a element 999element 0 12 bytes 4 or 8 bytes 4 or 8 bytes 4 or 8 bytes …element 1 elementData
  • 26. Map: Even More Overhead ● both key and value can be boxed primitives ● some types of map half-full by design ● e.g. HashMap<Integer, Integer>
  • 27. Primitive Collection Implementations ● Trove o http://trove.starlight-systems.com/ o does NOT implement Java’s Collection interface o iteration by callback object/method
  • 28. Primitive Collection Implementations ● fastutil o http://fastutil.di.unimi.it/ o type-specific maps, sets, lists o sorted maps and sets o implement Java’s Collection interface o primitive iterators o latest version’s binary JAR file is 17 MB
  • 29. Convert Primitive Array To List ● you have an int[] ● some method only takes a List (e.g. List<Integer>) ● Arrays.asList(int[]): type mismatch
  • 30. // someFunc takes a List, read-only // a: int[] list = new ArrayList<Integer>(a.length); for(int i = 0; i < a.length; i++){ list.add(a[i]); } someFunc(list);
  • 31. // someFunc takes a List, read-only // a: int[] someFunc(Ints.asList(a)); ● Ints.asList: returns a fixed-size list backed by the specified array ● Ints provided by Guava (from Google)
  • 33. Regular Expression // input, regex: String input.matches(regex) does the following: Pattern.compile(regex) .matcher(input) .matches()
  • 34. while (stillRunning()) { … // str and regex: String if (str.matches(regex)) { … } … }
  • 35. while (stillRunning()) { … // str and regex: String if (Pattern.compile(regex) .matcher(input) .matches()) { … } … }
  • 36. while (stillRunning()) { … // str and regex: String if (Pattern.compile(regex) .matcher(input) .matches()) { … } … } generate a Pattern object from the RE pattern (expensive)
  • 38. RE Benchmark ● Java Regular expression library benchmarks o http://tusker.org/regex/regex_benchmark.html ● many alternative implementations ● speed and capability vary
  • 39. BRICS Automaton ● http://www.brics.dk/automaton/ ● much faster than java.util.regex.Pattern ● basic functionality ● adopted by Lucene 4.x for RE query
  • 41. Caching: Example Scenario ● calculate file checksums ● calculation is expensive ● lots of files ● repeated calculations ● solution: cache the checksum o key: file path
  • 42. Possible Solution: HashMap ● implement cache with HashMap<String, byte[]> ● problem: o assume cache will be “small enough”, or o need to remove entries (how?)
  • 43. Possible Solution: WeakHashMap ● implement cache with WeakHashMap<String, byte[]> ● problem: o keys are weak references o if no strong reference on a key, the entry is eligible for GC
  • 44. Solution: Libraries ● Apache Commons Collections o LRUMap class o LRU (least recently updated) cache algorithm o fixed max size
  • 45. Solution: Libraries (cont.) ● Guava o CacheBuilder and Cache classes o accessible by multiple threads o eviction by: max size, access time, write time, weak keys, weak values, soft values o CacheLoader class: creates entry on demand o remove listeners
  • 47. Lossless Compression Today ● compression saves more space than ever ● store more in faster medium (memory, SSD) ● compression ! == slow anymore ● new algorithms target high throughput o > 300 MB/s compression o > 1000 MB/s decompression
  • 48. LZ4 ● faster than Deflate with zlib ● decent ratio ● LZ4_HC o slower compression speed o higher compression ratio o decoding side unchanged ● ported to Java o https://github.com/jpountz/lz4-java
  • 50. Memory-Mapped File Intro ● MappedByteBuffer ● content: memory-mapped region of a file ● OS handles reading, writing, caching ● can be used as persistent, shared memory
  • 51. Memory-Mapped File Caveats ● file size limited by address space o 64-bit JVM: not an issue o 32-bit JVM: file size limited to <= 2 GB ● no “close function” o you can’t control when resources are free o not suitable for opening many files at/around the same time
  • 53. VisualVM ● bundled with Java SE ● local or remote (JMX) applications ● monitor o CPU usage o heap and PermGen usage o class and thread counts
  • 54. VisualVM ● heap dump o instance and size by class o biggest object by retain size ● sampler (fast) ● profiler (slow)
  • 55. Sampling Example ● run 4 methods o concatByAppendresult.append(x()); o concatByConcatresult = result.concat(x()); o concatByPlusOperatorresult = result + x(); o concatByPlusEqualOperatorresult += x(); ● if same speed, each would get 25% CPU
  • 56.
  • 57. Thanks for Coming! ● slides available o http://bit.ly/sdcodecamp2014java ● please vote for my conference session o http://bit.ly/tvnews2014 ● questions/feedback o kai@ssc.ucla.edu ● questions?

Editor's Notes

  1. tips covered here: * most are learned and tested in production * some are not (but interesting enough) performance goals: * finish a task in less time, given same amount of parallelization * use less memory language war: * there are places for that * use the right tool for the right job * programmers can make things work better exhaustive list or really in-depth analysis: * there are books for that * we only have 1 hour * get you interesting in this topic, if you are not already
  2. some truth for the saying that the compiler uses StringBuilder behind the scene
  3. some truth for the saying that the compiler uses StringBuilder behind the scene
  4. * when the concatenation is done inside a loop, things become more complex * here’s how the code looks like if you use StringBuilder explicitly
  5. * what if you use the + or += operator and hope the compiler optimizes things for you?
  6. * turns out, the compiler rewrites the code into this
  7. * note these 3 operations within the loop * if you think all these object creation and copying slow things down, you’re probably right
  8. * using StringBuilder explicitly results in much less copying of data * in short, if you’re concatenating strings inside a loop, use StringBuilder explicitly
  9. * CharSequence: a less well-known interface
  10. * however, the Writer support writing a CharSequence’s content to a file, etc. * CharSequenceReader (in Apache Commons IO library) to read from CharSequence
  11. * whenever we have a method that returns a String, and we have to convert something into a String return value, ask: do we really have to do the conversion? * might be able to modify to method to return another type, like CharSequence * might be able to avoid the conversion
  12. http://www.ibm.com/developerworks/library/j-codetoheap/
  13. * a lot of overhead incolved
  14. * no built-in implementation, but 3rd party implementations
  15. * unlike Trove, fastutil has sorted maps and asets * downside is size (so many classes in the library)
  16. * no automatic conversion/cast from int[] into List<Integer>
  17. slow approach * create an ArrayList<Integer> * copy elements, one at a time, from the int[]
  18. * no copying of array elements
  19. https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/LRUMap.html
  20. https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/LRUMap.html
  21. Zip and GZip use same algorithm Deflate zlib: open-source implementation compression saves more space than ever compression ! == slow anymore CPU much faster than I/O compressed data: less I/O
  22. * MappedByteBuffer: get from RandomAccessFile and FileChannel http://www.onjava.com/pub/a/onjava/2002/10/02/javanio.html?page=3
  23. SIZE = 100000