SlideShare a Scribd company logo
1 of 47
Download to read offline
High Performance Java Binary
instead of Objects
BEFORE WE BEGIN…
3
BINARY WE'RE TALKING 1S & 0S
46
001
00010110
DECIMAL
HEX
BINARY
4
BINARY WE'RE TALKING 1S & 0S
11111111
BYTE
INT 4 BYTES
LONG 8 BYTES
There are also shorts (16 bits), chars (16), floats (32),
doubles (64) and booleans (undefined)
5
BINARY WE'RE TALKING 1S & 0S
Most of the Java primitives (for some bizarre reason)
are signed
So the “top” bit is used for the sign, 0 is positive and 1 is negative
Bitwise operands work on ints and longs
& (AND)
| (OR)
^ (XOR)
~ (NOT)
<< (left shift), >> (right shirt with sign) and >>> (right shift with 0s)
6
A QUICK TEST…
What is this 0x10 in decimal?
What is special about 0x55 and 0xAA in hex?
How many bits are needed to store all of the months of
the year?
There are 365.25 days in a year, how many bits to store
100 years at day resolution?
What is ~-1?
7
A QUICK TEST…
What is this 0x10 in decimal?
32
What is special about 0x55 and 0xAA in hex?
The are 01010101 and 10101010 (alternate 1s and 0s)
How many bits are needed to store all of the months of
the year?
Just 4 bits
There are 365.25 days in a year, how many bits to store
100 years at day resolution?
16 bits (2 bytes)
What is ~-1?
NOT -1 or 0, i.e. the reverse of 0xFFFFFFFF (same as -1^-1)
8
JAVA IS VERY INEFFICIENT AT STORING DATA
Take the string “ABC”, typically it needs just 4 bytes
Three if you know the length won’t change
Even less if “ABC” is an enumeration
Java takes 48 bytes to store “ABC” as a String
You could argue that we don’t need to run down the entire length of the
String to execute length() but it’s a big price to pay
A B C 0
9
IT'S NOT JUST A STRING
If it was just String then we could use byte[] or char[] but Java
bloating is endemic
Double (even today’s date takes 48 bytes)
BigDecimal
Date
ArrayList
Use just one or two and we’re OK but write a class with a few of
these and we really start to see the problem
A class with 10 minimum sized Objects can be over 500 bytes
in size - for each instance
What’s worse is that each object requires 11 separate memory allocations
All of which need managing
Which is why we have the garbage collector(s)
10
A NEW DATE?
Could we create a new Date class?
class NewDate {
private byte year;
private byte month;
private byte day;
}
Yes but this is still 16 bytes in Java, 8 times more than
we need
In C/C++ we could use bitfields, 7 for the year, 4 for the
month and 5 for the day.
Total just 16 bits or two bytes
This is is our target, 2 bytes
11
48 BYTES IS A LOT!
To store today’s date Java needs 48 bytes that looks
like this…
00101001 11000111 10100000 00010111
10100011 11100101 00010101 10010100
11101001 01000001 10101111 02010001
10100011 11100100 11010100 11010100
00101001 11000111 10100000 00010101
10100111 11100101 00010101 10010100
01101101 01000001 10101101 02010101
10100011 01100100 01010101 11010100
10001001 01000111 10100001 00010100
10100111 11100101 00010100 10010100
11101001 01000000 00101100 02010100
10100011 11100101 01010101 11010101
As opposed to this: 0x4130 or 01000001 00110000
The red is just random 0s and 1s but the green binary is real (16,688)
12
START SIMPLE
Simple data we’re going to be referring to for the next few
slides…
ID TradeDate BuySell Currency1 Amount1
Exchange
Rate
Currency2 Amount2
Settlement
Date
1 21/07/2014 Buy EUR 50,000,000 1.344 USD 67,200,000.00 28/07/2014
2 21/07/2014 Sell USD 35,000,000 0.7441 EUR 26,043,500.00 20/08/2014
3 22/07/2014 Buy GBP 7,000,000 172.99 JPY 1,210,930,000.00 05/08/2014
4 23/07/2014 Sell AUD 13,500,000 0.9408 USD 12,700,800.00 22/08/2014
5 24/07/2014 Buy EUR 11,000,000 1.2148 CHF 13,362,800.00 31/07/2014
6 24/07/2014 Buy CHF 6,000,000 0.6513 GBP 3,907,800.00 31/07/2014
7 25/07/2014 Sell JPY 150,000,000 0.6513 EUR 97,695,000.00 08/08/2014
8 25/07/2014 Sell CAD 17,500,000 0.9025 USD 15,793,750.00 01/08/2014
9 28/07/2014 Buy GBP 7,000,000 1.8366 CAD 12,856,200.00 27/08/2014
10 28/07/2014 Buy EUR 13,500,000 0.7911 GBP 10,679,850.00 11/08/2014
13
START WITH THE CSV
Each line is relatively efficient
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
But it’s in human-readable format not CPU readable
At least not efficient CPU readable
We could store the lines as they are but in order to work
with the data we need it in something Java can work
with
The same goes for any other language, C, C++, PHP, Scala etc.
So typically we parse it into a Java class and give it a
self-documenting name - Row
14
CSV TO JAVA
This seems like a reasonably good implementation
From this…
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
We get this…
public class ObjectTrade {
private long id;
private Date tradeDate;
private String buySell;
private String currency1;
private BigDecimal amount1;
private double exchangeRate;
private String currency2;
private BigDecimal amount2;
private Date settlementDate;
}
15
EVERYTHINGS FINE
With very simple getters and setters, something to
parse the CSV and a custom toString() we’re good
public BasicTrade parse( String line ) throws ParseException {
String[] fields = line.split(",");
setId(Long.parseLong(fields[0]));
setTradeDate(DATE_FORMAT.get().parse(fields[1]));
setBuySell(fields[2]);
setCurrency1(fields[3]);
setAmount1(new BigDecimal(fields[4]));
setExchangeRate(Double.parseDouble(fields[5]));
setCurrency2(fields[6]);
setAmount2(new BigDecimal(fields[7]));
setSettlementDate(DATE_FORMAT.get().parse(fields[8]));
return this;
}
What could possibly go wrong?
16
CLASSIC JAVA
In fact everything works really well, this is how Java was
designed to work
There are a few “fixes” to add for SimpleDateFormat due to it not being
thread safe but otherwise we’re good
Performance is good, well it seems good and
everything is well behaved
As the years go on and the volumes increase, we now
have 100 million of them
Now we start to see some problems
To start with we don’t have enough memory - GC is killing performance
When we distribute the size of the objects are killing performance too
17
JAVA IS VERBOSE & SLOW
A simple CSV can grow by over 4 times…
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
From roughly 70 bytes per line as CSV to around 328 in
Java
That means you get over 4 times less data when
stored in Java
Or need over 4 times more RAM, network
capacity and disk
Serialized objects are even bigger!
18
JAVA BLOATS YOUR DATA
You probably thought XML was bad imagine what
happens when you take XML and bind it to Java!
Anything you put into Java objects get
horribly bloated in memory
Effectively you are paying the price of
memory and hardware abstraction
19
JAVA OBJECTS - GOOD FOR VENDORS
Bloated Java Objects are a great for hardware vendor’s
Think about it, Java came from Sun, it was free but they made money
selling hardware, well they tried at least
Bloated Java Objects need more memory, more CPU,
more network capacity, more machines
More money for the hardware vendors - why would they change it?
And everything just runs slower because you’re busy
collecting all the memory you’re not using
20
THIS ISN’T JUST JAVA
Think this is just a Java problem?
It’s all the same, every time you create objects you’re
blasting huge holes all over your machine’s RAM
And someone’s got to clean all the garbage up too!
Great for performance-tuning consultants :-)
21
IN-MEMORY CACHES
If you use an in-memory cache then you’re most likely
suffering from the same problem…
Many of them provide and use compression or “clever”
memory optimisation
If they use reflection things slow down
If you have to write it yourself you have a lot of work to do, especially for
hierarchical data
22
CLASSIC JAVA BINDING
This is how we’d typically code this simple CSV
example…
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
public class ObjectTrade {
private long id;
private Date tradeDate;
private String buySell;
private String currency1;
private BigDecimal amount1;
private double exchangeRate;
private String currency2;
private BigDecimal amount2;
private Date settlementDate;
}
It’s easy to write the code and fast to execute, retrieve,
search and query data BUT it needs a lot of RAM and it
slow to manage
23
JUST STORE THE ORIGINAL DATA?
We could just store each row
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
public class StringTrade {
private String row;
}
But every time we wanted a date or amount we’d need
to parse it and that would slow down analysis
If the data was XML it would be even worse
We’d need a SAX (or other) parser every time
24
JUST STORE THE ORIGINAL DATA?
public class StringTrade {
private String row;
}
Allocation of new StringTrades are faster as we allocate
just 2 Objects
Serialization and De-Serialization are improved for the
same reason
BUT over all we lose out when we’re accessing the data
We need to find what we’re looking each time
This is sort of OK with a CSV but VERY expensive for XML
public class XmlTrade {
private String xml;
}
25
COMPRESSION OR COMPACTION?
OK, a lot of asks, why don’t we just use compression?
Well there are many reasons, mainly that it’s slow, slow
to compress and slow to de-compress, the better it is
the slower it is
Compression is the lazy person’s tool, a good protocol
or codec doesn’t compress well, try compressing your
MP3s or videos
It has it’s place but we’re looking for more, we want
compaction not compression, then we get fast
performance too
It’s very similar to image processing, zip the images
and you can’t see them
26
NOW IN BINARY…
This is what our binary version looks like…
ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date
1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014
2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014
3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014
public class ObjectTrade extends SDO {
private byte[] data;
}
Just one object again so fast to allocate
If we can encode the data in the binary then it’s fast to
query too
And serialisation is just writing out the byte[]
27
SAME API, JUST BINARY
Classic getter and setter vs. binary implementation
Identical API
28
SAME API, JUST BINARY
Classic getter and setter vs. binary implementation
Identical API
29
DID I MENTION.. THE SAME API
This is a key point, we’re changing the implementation
not the API
This means that Spring, in-memory caches and other
tools work exactly as they did before
Let’s look at some code and
a little demo of this…
30
HOW DOES IT PERFORM?
Compare classic Java binding to binary…
Classic'Java'
version
Binary'Java'
version
Improvement
Bytes&used 328 39 840%
Serialization+size 668 85 786%
Custom'
Serialization
668 40 1,670%
Time%to%
Serialize/Deseriali
ze
41.1µS 4.17µS 10x
Batched(
Serialize/Deseriali
ze
12.3µS 44nS 280x
31
COMPARING…
While the shapes look the same you can clearly see the
differences on both axis
The Object version is a lot slower
And the Object version uses significantly more memory
Note that the left side (objects) has a heap size of 8GB,
the right (binary) has a heap size of just 2GB
32
COMPARING…
While the shapes look the same you can clearly see the
differences on both axis
The Object version is a lot slower
And the Object version uses significantly more memory
Note that the left side (objects) has a heap size of 8GB, the
right (binary) has a heap size of just 2GB
33
COMPARING…
These two graphs show the GC pause time during
message creation and serialisation
Left is “classic” Java
Right is the binary version
The top of the right hand graph is lower than the first
rung of the left (50ms)
34
COMPARING…
These two graphs show the GC pause time during message
creation and serialisation
Left is “classic” Java
Right is the binary version
The top of the right hand graph is lower than the first rung of
the left (50ms)
35
GENERATED CODE
The C24 generated code for the Trade example is
actually smaller, it averages around 33 bytes
It uses run-length encoding so we can represent the 64
bit long by as little as a single byte for small numbers
This means the size of the binary message varies
slightly but for more complex models/message
This is probably not a huge advantage for this CSV
example but makes a huge difference with more
complex XML
36
BEYOND THE CSV FILE
It worked for a simple CSV how about more complex
models like XML or JSON?
Once we have a mapping of types to binary and code
we can extend this to any type of model
But it gets a lot more complex as we have optional
elements, repeating elements and a vast array of
different types
XML can be represented as binary and binary as XML,
you just need a model
37
STANDARD JAVA BINDING
<resetFrequency>
<periodMultiplier>6</periodMultiplier>
<period>M</period>
</resetFrequency>
JAXB, JIBX, Castor etc. generate something like …
public class ResetFrequency {
private BigInteger periodMultiplier; // Positive Integer
private Object period; // Enum of D, W, M, Q,Y
public BigInteger getPeriodMultiplier() {
return this.periodMultiplier;
}
// constructors & other getters and setters
In memory - 3 objects - at least 144 bytes
The parent, a positive integer and an enumeration for Period
3 Java objects at 48 bytes is 144 bytes and it becomes fragmented in
memory
38
JAVA BINDING TO BINARY
<resetFrequency>
<periodMultiplier>6</periodMultiplier>
<period>M</period>
</resetFrequency>
Using our binary codec we generate …
ByteBuffer data; // From the root object
public BigInteger getPeriodMultiplier() {
int byteOffset = 123; // Actually a lot more complex
return BigInteger.valueOf( data.get(byteOffset) & 0x1F );
}
// constructors & other getters
In memory -1 byte for all three fields
The root contains one ByteBuffer which is a wrapper for byte[]
The getters use bit-fields…
PeriodMultiplier is 1-30 so needs 5 bits
Period is just 3 bits for values D, W, M, Q or Y
39
SERIALIZATION & DESERIALIZATION
Writing a Java Object as a serialized stream is slow
Of the Object contains other objects then it’s painfully SLOW
Several frameworks exist for (performance) serialisation
Avro, Kryo, Thrift, Protobuf, Cap’n proto
Of course there’s always JSON and XML if speed and memory are
infinite resources and you like reading the data streams before bed
time
Generating the serialisation though is faster still since
we can avoid reflection and the data is already in
serialised form
The same is true for deserialisation
So we can almost double the speed of serialisation
frameworks like Kryo with even smaller packet size
40
JMH BENCHMARKS
JMH is an open source micro-benchmarking
framework, the following are timings comparing Kryo
and our own binary serialisation…
Benchmark Mode Cnt Score Error Units
IoBenchmark.deSer sample 58102 171.596 ± 0.541 ns/op
IoBenchmark.ser sample 58794 169.579 ± 0.546 ns/op
Benchmark Mode Cnt Score Error Units
KryoBenchmark.deSer sample 26423 378.121 ± 1.096 ns/op
KryoBenchmark.ser sample 24951 400.387 ± 1.512 ns/op
http://openjdk.java.net/projects/code-tools/jmh/
https://github.com/eishay/jvm-serializers
41
ANALYTICS
When we have a lot of data there seems to be an
assumption that we need Hadoop
Hadoop is an implementation of Map / Reduce
If we shrink the data then we can use a lambda for the
same task…
.collect(Collectors.groupingBy(f -> f.getSignedCurrencyAmount()
.getCurrencyAmount().getCurrency(),
Collectors.reducing(BigDecimal.ZERO, t -> t.getSignedCurrencyAmount()
.getCurrencyAmount().getAmount(), BigDecimal::add)));
Produces (in 160ms for 50,000 complex SWIFT
messages)
TWD=16218034.70, CHF=127406235.40, HKD=1292102912.26, EUR=831021868.46,
DKK=196507853.20, CAD=137982907.68, USD=525123842.02, ZAR=19383094.78,
NOK=618033504.06, AUD=218890981.32, ILS=46628.66, SGD=74032013.42 etc.
42
ANALYTICS
Extracting data, converting it, writing it to HBase or HDFS to
run in Hadoop seems like going back to ORM
i.e. we’re adding a new layer of code for something we really shouldn't need
Why not just reduce the size of the data and run it in memory?
In theory the same could be done onto disk (SSD) rather
than memory because the serialised form is fully readable
So, use binary Java instead of Java Objects, shrink your
data and use ordinary Java to process it
10 times smaller and 10 times faster is 5 “Moore’s Law”
years
That 5 years you can get back today with a few changes
43
HADOOP OR SPARK?
It’s not really one or the other, they have different use-
cases but our binary binding (PREON) used in Spark
shows some amazing results…
44
HOW IT WORKS
Performance
Size
Or any XML message
Parser
(Classic Java API)
Validation (Optional step)
PREON Sink
(Converts CDO to
PREON)
XML
Fully mutable API
(Getters, setters, rules, validation &
transformation)
Fully immutable API
(Getters only)
PREON API
(to binary FpML)
PREON Source
(Converts SDO to
PREON)
~5-8k 10-25k < 500 bytes
10k/sec ~1m/sec ~1m/sec
Identical APIs
(for getters)
45
ANOTHER DEMO
Take some raw XML (an FpML derivative, about 7.4k of XML)
Parse it, mutate a few variables and then compact each one
to its binary version - We do this 1 million times
This takes about 100 seconds
Now the test begins
We take a look at the binary version (just 370 bytes)
We search through the 1 million trades for data and aggregate the results
Then we try it multi-threaded (using parallelStream())
A few more similar operations (I will discuss)
Finally we’ll write all 1 million to disk and then read them back
into another array (comparing to make sure it worked)
This will be a test of serialisation
50
SO IT WORKS
Key points from the slides…
If you want performance, scalability and ease of
maintenance then you need…
Reduce the amount of memory you’re using
Reduce the way you use the memory
Try using binary data instead of objects
Start coding intelligently like we used to in the 80s and 90s
Or just buy much bigger, much faster machines, more
RAM, bigger networks and more DnD programmers
And you’ll probably get free lunches from your hardware vendors
TAKK!
@jtdavies

More Related Content

Viewers also liked

Exactpro Systems High Level Overview January 2014
Exactpro Systems High Level Overview January 2014Exactpro Systems High Level Overview January 2014
Exactpro Systems High Level Overview January 2014Iosif Itkin
 
Mapping, GIS and geolocating data in Java
Mapping, GIS and geolocating data in JavaMapping, GIS and geolocating data in Java
Mapping, GIS and geolocating data in JavaJoachim Van der Auwera
 
Best Practices for Automotive 2015
Best Practices for Automotive 2015Best Practices for Automotive 2015
Best Practices for Automotive 2015Melissa Reider
 
ISI 5121 Trove Presentation
ISI 5121 Trove PresentationISI 5121 Trove Presentation
ISI 5121 Trove Presentationksirett
 
Java performance: What's the big deal? - Trisha Gee
Java performance: What's the big deal? - Trisha GeeJava performance: What's the big deal? - Trisha Gee
Java performance: What's the big deal? - Trisha GeeJAX London
 
Performance Tuning
Performance TuningPerformance Tuning
Performance TuningJannet Peetz
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunningguest1f2740
 
Trading Clearing Systems Test Automation
Trading Clearing Systems Test AutomationTrading Clearing Systems Test Automation
Trading Clearing Systems Test AutomationIosif Itkin
 
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...Spark Summit
 
Extent3 exactpro the_next_step_in_reconciliation_testing
Extent3 exactpro the_next_step_in_reconciliation_testingExtent3 exactpro the_next_step_in_reconciliation_testing
Extent3 exactpro the_next_step_in_reconciliation_testingextentconf Tsoy
 
Extent3 exactpro four_houses_test_tools_2012 (1)
Extent3 exactpro four_houses_test_tools_2012 (1)Extent3 exactpro four_houses_test_tools_2012 (1)
Extent3 exactpro four_houses_test_tools_2012 (1)extentconf Tsoy
 
Extent april2012-kostroma social-networks-socialmedia-trading
Extent april2012-kostroma social-networks-socialmedia-tradingExtent april2012-kostroma social-networks-socialmedia-trading
Extent april2012-kostroma social-networks-socialmedia-tradingextentconf Tsoy
 
Extent3 exactpro the_future_of_risk_controls
Extent3 exactpro the_future_of_risk_controlsExtent3 exactpro the_future_of_risk_controls
Extent3 exactpro the_future_of_risk_controlsextentconf Tsoy
 
Extent3 exante broker_for_algorithmic_trading_2012
Extent3 exante broker_for_algorithmic_trading_2012Extent3 exante broker_for_algorithmic_trading_2012
Extent3 exante broker_for_algorithmic_trading_2012extentconf Tsoy
 
Building a modern Application with DataFrames
Building a modern Application with DataFramesBuilding a modern Application with DataFrames
Building a modern Application with DataFramesDatabricks
 
Low level java programming
Low level java programmingLow level java programming
Low level java programmingPeter Lawrey
 
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)Spark Summit
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesSergey Podolsky
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 

Viewers also liked (19)

Exactpro Systems High Level Overview January 2014
Exactpro Systems High Level Overview January 2014Exactpro Systems High Level Overview January 2014
Exactpro Systems High Level Overview January 2014
 
Mapping, GIS and geolocating data in Java
Mapping, GIS and geolocating data in JavaMapping, GIS and geolocating data in Java
Mapping, GIS and geolocating data in Java
 
Best Practices for Automotive 2015
Best Practices for Automotive 2015Best Practices for Automotive 2015
Best Practices for Automotive 2015
 
ISI 5121 Trove Presentation
ISI 5121 Trove PresentationISI 5121 Trove Presentation
ISI 5121 Trove Presentation
 
Java performance: What's the big deal? - Trisha Gee
Java performance: What's the big deal? - Trisha GeeJava performance: What's the big deal? - Trisha Gee
Java performance: What's the big deal? - Trisha Gee
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Trading Clearing Systems Test Automation
Trading Clearing Systems Test AutomationTrading Clearing Systems Test Automation
Trading Clearing Systems Test Automation
 
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...
Solving Low Latency Query Over Big Data with Spark SQL-(Julien Pierre, Micros...
 
Extent3 exactpro the_next_step_in_reconciliation_testing
Extent3 exactpro the_next_step_in_reconciliation_testingExtent3 exactpro the_next_step_in_reconciliation_testing
Extent3 exactpro the_next_step_in_reconciliation_testing
 
Extent3 exactpro four_houses_test_tools_2012 (1)
Extent3 exactpro four_houses_test_tools_2012 (1)Extent3 exactpro four_houses_test_tools_2012 (1)
Extent3 exactpro four_houses_test_tools_2012 (1)
 
Extent april2012-kostroma social-networks-socialmedia-trading
Extent april2012-kostroma social-networks-socialmedia-tradingExtent april2012-kostroma social-networks-socialmedia-trading
Extent april2012-kostroma social-networks-socialmedia-trading
 
Extent3 exactpro the_future_of_risk_controls
Extent3 exactpro the_future_of_risk_controlsExtent3 exactpro the_future_of_risk_controls
Extent3 exactpro the_future_of_risk_controls
 
Extent3 exante broker_for_algorithmic_trading_2012
Extent3 exante broker_for_algorithmic_trading_2012Extent3 exante broker_for_algorithmic_trading_2012
Extent3 exante broker_for_algorithmic_trading_2012
 
Building a modern Application with DataFrames
Building a modern Application with DataFramesBuilding a modern Application with DataFrames
Building a modern Application with DataFrames
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
 
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)
Data Storage Tips for Optimal Spark Performance-(Vida Ha, Databricks)
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issues
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 

Similar to John Davies: "High Performance Java Binary" from JavaZone 2015

Turn your XML into binary - JavaOne 2014
Turn your XML into binary - JavaOne 2014Turn your XML into binary - JavaOne 2014
Turn your XML into binary - JavaOne 2014John Davies
 
[1] Data Representation
[1] Data Representation[1] Data Representation
[1] Data RepresentationMr McAlpine
 
When indexes are not enough
When indexes are not enoughWhen indexes are not enough
When indexes are not enoughDavide Mauri
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++Mike Acton
 
Big Data in Memory - SpringOne 2014
Big Data in Memory - SpringOne 2014Big Data in Memory - SpringOne 2014
Big Data in Memory - SpringOne 2014John Davies
 
Lesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squencesLesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squencesLexume1
 
Creating your own Abstract Processor
Creating your own Abstract ProcessorCreating your own Abstract Processor
Creating your own Abstract ProcessorAodrulez
 
TDD for the Newb Who Wants to Become an Apprentice
TDD for the Newb Who Wants to Become an ApprenticeTDD for the Newb Who Wants to Become an Apprentice
TDD for the Newb Who Wants to Become an ApprenticeHoward Deiner
 
Scaling MongoDB for real time analytics
Scaling MongoDB for real time analyticsScaling MongoDB for real time analytics
Scaling MongoDB for real time analyticsDavid Tollmyr
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxrelaine1
 
Counting (Notes)
Counting (Notes)Counting (Notes)
Counting (Notes)roshmat
 
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)Андрей Новиков
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?Colin Riley
 
Ross Snyder, Etsy, SXSW Lean Startup 2013
Ross Snyder, Etsy, SXSW Lean Startup 2013Ross Snyder, Etsy, SXSW Lean Startup 2013
Ross Snyder, Etsy, SXSW Lean Startup 2013500 Startups
 
Computer Systems Data Representation
Computer Systems   Data RepresentationComputer Systems   Data Representation
Computer Systems Data Representationiarthur
 
Network Slides
Network SlidesNetwork Slides
Network Slidesiarthur
 
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc SQL Basic and conceptual Explained with Examples,Graphs, pictures etc
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc maheshpharale
 
Duplicates everywhere (Kiev)
Duplicates everywhere (Kiev)Duplicates everywhere (Kiev)
Duplicates everywhere (Kiev)Alexey Grigorev
 
Heap overflows for humans – 101
Heap overflows for humans – 101Heap overflows for humans – 101
Heap overflows for humans – 101Craft Symbol
 

Similar to John Davies: "High Performance Java Binary" from JavaZone 2015 (20)

Turn your XML into binary - JavaOne 2014
Turn your XML into binary - JavaOne 2014Turn your XML into binary - JavaOne 2014
Turn your XML into binary - JavaOne 2014
 
[1] Data Representation
[1] Data Representation[1] Data Representation
[1] Data Representation
 
When indexes are not enough
When indexes are not enoughWhen indexes are not enough
When indexes are not enough
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
Binary.pdf
Binary.pdfBinary.pdf
Binary.pdf
 
Big Data in Memory - SpringOne 2014
Big Data in Memory - SpringOne 2014Big Data in Memory - SpringOne 2014
Big Data in Memory - SpringOne 2014
 
Lesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squencesLesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squences
 
Creating your own Abstract Processor
Creating your own Abstract ProcessorCreating your own Abstract Processor
Creating your own Abstract Processor
 
TDD for the Newb Who Wants to Become an Apprentice
TDD for the Newb Who Wants to Become an ApprenticeTDD for the Newb Who Wants to Become an Apprentice
TDD for the Newb Who Wants to Become an Apprentice
 
Scaling MongoDB for real time analytics
Scaling MongoDB for real time analyticsScaling MongoDB for real time analytics
Scaling MongoDB for real time analytics
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docx
 
Counting (Notes)
Counting (Notes)Counting (Notes)
Counting (Notes)
 
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)
PostgreSQL as seen by Rubyists (Kaigi on Rails 2022)
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
Ross Snyder, Etsy, SXSW Lean Startup 2013
Ross Snyder, Etsy, SXSW Lean Startup 2013Ross Snyder, Etsy, SXSW Lean Startup 2013
Ross Snyder, Etsy, SXSW Lean Startup 2013
 
Computer Systems Data Representation
Computer Systems   Data RepresentationComputer Systems   Data Representation
Computer Systems Data Representation
 
Network Slides
Network SlidesNetwork Slides
Network Slides
 
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc SQL Basic and conceptual Explained with Examples,Graphs, pictures etc
SQL Basic and conceptual Explained with Examples,Graphs, pictures etc
 
Duplicates everywhere (Kiev)
Duplicates everywhere (Kiev)Duplicates everywhere (Kiev)
Duplicates everywhere (Kiev)
 
Heap overflows for humans – 101
Heap overflows for humans – 101Heap overflows for humans – 101
Heap overflows for humans – 101
 

Recently uploaded

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 

John Davies: "High Performance Java Binary" from JavaZone 2015

  • 1. High Performance Java Binary instead of Objects
  • 3. 3 BINARY WE'RE TALKING 1S & 0S 46 001 00010110 DECIMAL HEX BINARY
  • 4. 4 BINARY WE'RE TALKING 1S & 0S 11111111 BYTE INT 4 BYTES LONG 8 BYTES There are also shorts (16 bits), chars (16), floats (32), doubles (64) and booleans (undefined)
  • 5. 5 BINARY WE'RE TALKING 1S & 0S Most of the Java primitives (for some bizarre reason) are signed So the “top” bit is used for the sign, 0 is positive and 1 is negative Bitwise operands work on ints and longs & (AND) | (OR) ^ (XOR) ~ (NOT) << (left shift), >> (right shirt with sign) and >>> (right shift with 0s)
  • 6. 6 A QUICK TEST… What is this 0x10 in decimal? What is special about 0x55 and 0xAA in hex? How many bits are needed to store all of the months of the year? There are 365.25 days in a year, how many bits to store 100 years at day resolution? What is ~-1?
  • 7. 7 A QUICK TEST… What is this 0x10 in decimal? 32 What is special about 0x55 and 0xAA in hex? The are 01010101 and 10101010 (alternate 1s and 0s) How many bits are needed to store all of the months of the year? Just 4 bits There are 365.25 days in a year, how many bits to store 100 years at day resolution? 16 bits (2 bytes) What is ~-1? NOT -1 or 0, i.e. the reverse of 0xFFFFFFFF (same as -1^-1)
  • 8. 8 JAVA IS VERY INEFFICIENT AT STORING DATA Take the string “ABC”, typically it needs just 4 bytes Three if you know the length won’t change Even less if “ABC” is an enumeration Java takes 48 bytes to store “ABC” as a String You could argue that we don’t need to run down the entire length of the String to execute length() but it’s a big price to pay A B C 0
  • 9. 9 IT'S NOT JUST A STRING If it was just String then we could use byte[] or char[] but Java bloating is endemic Double (even today’s date takes 48 bytes) BigDecimal Date ArrayList Use just one or two and we’re OK but write a class with a few of these and we really start to see the problem A class with 10 minimum sized Objects can be over 500 bytes in size - for each instance What’s worse is that each object requires 11 separate memory allocations All of which need managing Which is why we have the garbage collector(s)
  • 10. 10 A NEW DATE? Could we create a new Date class? class NewDate { private byte year; private byte month; private byte day; } Yes but this is still 16 bytes in Java, 8 times more than we need In C/C++ we could use bitfields, 7 for the year, 4 for the month and 5 for the day. Total just 16 bits or two bytes This is is our target, 2 bytes
  • 11. 11 48 BYTES IS A LOT! To store today’s date Java needs 48 bytes that looks like this… 00101001 11000111 10100000 00010111 10100011 11100101 00010101 10010100 11101001 01000001 10101111 02010001 10100011 11100100 11010100 11010100 00101001 11000111 10100000 00010101 10100111 11100101 00010101 10010100 01101101 01000001 10101101 02010101 10100011 01100100 01010101 11010100 10001001 01000111 10100001 00010100 10100111 11100101 00010100 10010100 11101001 01000000 00101100 02010100 10100011 11100101 01010101 11010101 As opposed to this: 0x4130 or 01000001 00110000 The red is just random 0s and 1s but the green binary is real (16,688)
  • 12. 12 START SIMPLE Simple data we’re going to be referring to for the next few slides… ID TradeDate BuySell Currency1 Amount1 Exchange Rate Currency2 Amount2 Settlement Date 1 21/07/2014 Buy EUR 50,000,000 1.344 USD 67,200,000.00 28/07/2014 2 21/07/2014 Sell USD 35,000,000 0.7441 EUR 26,043,500.00 20/08/2014 3 22/07/2014 Buy GBP 7,000,000 172.99 JPY 1,210,930,000.00 05/08/2014 4 23/07/2014 Sell AUD 13,500,000 0.9408 USD 12,700,800.00 22/08/2014 5 24/07/2014 Buy EUR 11,000,000 1.2148 CHF 13,362,800.00 31/07/2014 6 24/07/2014 Buy CHF 6,000,000 0.6513 GBP 3,907,800.00 31/07/2014 7 25/07/2014 Sell JPY 150,000,000 0.6513 EUR 97,695,000.00 08/08/2014 8 25/07/2014 Sell CAD 17,500,000 0.9025 USD 15,793,750.00 01/08/2014 9 28/07/2014 Buy GBP 7,000,000 1.8366 CAD 12,856,200.00 27/08/2014 10 28/07/2014 Buy EUR 13,500,000 0.7911 GBP 10,679,850.00 11/08/2014
  • 13. 13 START WITH THE CSV Each line is relatively efficient ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 But it’s in human-readable format not CPU readable At least not efficient CPU readable We could store the lines as they are but in order to work with the data we need it in something Java can work with The same goes for any other language, C, C++, PHP, Scala etc. So typically we parse it into a Java class and give it a self-documenting name - Row
  • 14. 14 CSV TO JAVA This seems like a reasonably good implementation From this… ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 We get this… public class ObjectTrade { private long id; private Date tradeDate; private String buySell; private String currency1; private BigDecimal amount1; private double exchangeRate; private String currency2; private BigDecimal amount2; private Date settlementDate; }
  • 15. 15 EVERYTHINGS FINE With very simple getters and setters, something to parse the CSV and a custom toString() we’re good public BasicTrade parse( String line ) throws ParseException { String[] fields = line.split(","); setId(Long.parseLong(fields[0])); setTradeDate(DATE_FORMAT.get().parse(fields[1])); setBuySell(fields[2]); setCurrency1(fields[3]); setAmount1(new BigDecimal(fields[4])); setExchangeRate(Double.parseDouble(fields[5])); setCurrency2(fields[6]); setAmount2(new BigDecimal(fields[7])); setSettlementDate(DATE_FORMAT.get().parse(fields[8])); return this; } What could possibly go wrong?
  • 16. 16 CLASSIC JAVA In fact everything works really well, this is how Java was designed to work There are a few “fixes” to add for SimpleDateFormat due to it not being thread safe but otherwise we’re good Performance is good, well it seems good and everything is well behaved As the years go on and the volumes increase, we now have 100 million of them Now we start to see some problems To start with we don’t have enough memory - GC is killing performance When we distribute the size of the objects are killing performance too
  • 17. 17 JAVA IS VERBOSE & SLOW A simple CSV can grow by over 4 times… ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 From roughly 70 bytes per line as CSV to around 328 in Java That means you get over 4 times less data when stored in Java Or need over 4 times more RAM, network capacity and disk Serialized objects are even bigger!
  • 18. 18 JAVA BLOATS YOUR DATA You probably thought XML was bad imagine what happens when you take XML and bind it to Java! Anything you put into Java objects get horribly bloated in memory Effectively you are paying the price of memory and hardware abstraction
  • 19. 19 JAVA OBJECTS - GOOD FOR VENDORS Bloated Java Objects are a great for hardware vendor’s Think about it, Java came from Sun, it was free but they made money selling hardware, well they tried at least Bloated Java Objects need more memory, more CPU, more network capacity, more machines More money for the hardware vendors - why would they change it? And everything just runs slower because you’re busy collecting all the memory you’re not using
  • 20. 20 THIS ISN’T JUST JAVA Think this is just a Java problem? It’s all the same, every time you create objects you’re blasting huge holes all over your machine’s RAM And someone’s got to clean all the garbage up too! Great for performance-tuning consultants :-)
  • 21. 21 IN-MEMORY CACHES If you use an in-memory cache then you’re most likely suffering from the same problem… Many of them provide and use compression or “clever” memory optimisation If they use reflection things slow down If you have to write it yourself you have a lot of work to do, especially for hierarchical data
  • 22. 22 CLASSIC JAVA BINDING This is how we’d typically code this simple CSV example… ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 public class ObjectTrade { private long id; private Date tradeDate; private String buySell; private String currency1; private BigDecimal amount1; private double exchangeRate; private String currency2; private BigDecimal amount2; private Date settlementDate; } It’s easy to write the code and fast to execute, retrieve, search and query data BUT it needs a lot of RAM and it slow to manage
  • 23. 23 JUST STORE THE ORIGINAL DATA? We could just store each row ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 public class StringTrade { private String row; } But every time we wanted a date or amount we’d need to parse it and that would slow down analysis If the data was XML it would be even worse We’d need a SAX (or other) parser every time
  • 24. 24 JUST STORE THE ORIGINAL DATA? public class StringTrade { private String row; } Allocation of new StringTrades are faster as we allocate just 2 Objects Serialization and De-Serialization are improved for the same reason BUT over all we lose out when we’re accessing the data We need to find what we’re looking each time This is sort of OK with a CSV but VERY expensive for XML public class XmlTrade { private String xml; }
  • 25. 25 COMPRESSION OR COMPACTION? OK, a lot of asks, why don’t we just use compression? Well there are many reasons, mainly that it’s slow, slow to compress and slow to de-compress, the better it is the slower it is Compression is the lazy person’s tool, a good protocol or codec doesn’t compress well, try compressing your MP3s or videos It has it’s place but we’re looking for more, we want compaction not compression, then we get fast performance too It’s very similar to image processing, zip the images and you can’t see them
  • 26. 26 NOW IN BINARY… This is what our binary version looks like… ID,TradeDate,BuySell,Currency1,Amount1,Exchange Rate,Currency2,Amount2,Settlement Date 1,21/07/2014,Buy,EUR,50000000.00,1.344,USD,67200000.00,28/07/2014 2,21/07/2014,Sell,USD,35000000.00,0.7441,EUR,26043500.00,20/08/2014 3,22/07/2014,Buy,GBP,7000000.00,172.99,JPY,1210930000,05/08/2014 public class ObjectTrade extends SDO { private byte[] data; } Just one object again so fast to allocate If we can encode the data in the binary then it’s fast to query too And serialisation is just writing out the byte[]
  • 27. 27 SAME API, JUST BINARY Classic getter and setter vs. binary implementation Identical API
  • 28. 28 SAME API, JUST BINARY Classic getter and setter vs. binary implementation Identical API
  • 29. 29 DID I MENTION.. THE SAME API This is a key point, we’re changing the implementation not the API This means that Spring, in-memory caches and other tools work exactly as they did before Let’s look at some code and a little demo of this…
  • 30. 30 HOW DOES IT PERFORM? Compare classic Java binding to binary… Classic'Java' version Binary'Java' version Improvement Bytes&used 328 39 840% Serialization+size 668 85 786% Custom' Serialization 668 40 1,670% Time%to% Serialize/Deseriali ze 41.1µS 4.17µS 10x Batched( Serialize/Deseriali ze 12.3µS 44nS 280x
  • 31. 31 COMPARING… While the shapes look the same you can clearly see the differences on both axis The Object version is a lot slower And the Object version uses significantly more memory Note that the left side (objects) has a heap size of 8GB, the right (binary) has a heap size of just 2GB
  • 32. 32 COMPARING… While the shapes look the same you can clearly see the differences on both axis The Object version is a lot slower And the Object version uses significantly more memory Note that the left side (objects) has a heap size of 8GB, the right (binary) has a heap size of just 2GB
  • 33. 33 COMPARING… These two graphs show the GC pause time during message creation and serialisation Left is “classic” Java Right is the binary version The top of the right hand graph is lower than the first rung of the left (50ms)
  • 34. 34 COMPARING… These two graphs show the GC pause time during message creation and serialisation Left is “classic” Java Right is the binary version The top of the right hand graph is lower than the first rung of the left (50ms)
  • 35. 35 GENERATED CODE The C24 generated code for the Trade example is actually smaller, it averages around 33 bytes It uses run-length encoding so we can represent the 64 bit long by as little as a single byte for small numbers This means the size of the binary message varies slightly but for more complex models/message This is probably not a huge advantage for this CSV example but makes a huge difference with more complex XML
  • 36. 36 BEYOND THE CSV FILE It worked for a simple CSV how about more complex models like XML or JSON? Once we have a mapping of types to binary and code we can extend this to any type of model But it gets a lot more complex as we have optional elements, repeating elements and a vast array of different types XML can be represented as binary and binary as XML, you just need a model
  • 37. 37 STANDARD JAVA BINDING <resetFrequency> <periodMultiplier>6</periodMultiplier> <period>M</period> </resetFrequency> JAXB, JIBX, Castor etc. generate something like … public class ResetFrequency { private BigInteger periodMultiplier; // Positive Integer private Object period; // Enum of D, W, M, Q,Y public BigInteger getPeriodMultiplier() { return this.periodMultiplier; } // constructors & other getters and setters In memory - 3 objects - at least 144 bytes The parent, a positive integer and an enumeration for Period 3 Java objects at 48 bytes is 144 bytes and it becomes fragmented in memory
  • 38. 38 JAVA BINDING TO BINARY <resetFrequency> <periodMultiplier>6</periodMultiplier> <period>M</period> </resetFrequency> Using our binary codec we generate … ByteBuffer data; // From the root object public BigInteger getPeriodMultiplier() { int byteOffset = 123; // Actually a lot more complex return BigInteger.valueOf( data.get(byteOffset) & 0x1F ); } // constructors & other getters In memory -1 byte for all three fields The root contains one ByteBuffer which is a wrapper for byte[] The getters use bit-fields… PeriodMultiplier is 1-30 so needs 5 bits Period is just 3 bits for values D, W, M, Q or Y
  • 39. 39 SERIALIZATION & DESERIALIZATION Writing a Java Object as a serialized stream is slow Of the Object contains other objects then it’s painfully SLOW Several frameworks exist for (performance) serialisation Avro, Kryo, Thrift, Protobuf, Cap’n proto Of course there’s always JSON and XML if speed and memory are infinite resources and you like reading the data streams before bed time Generating the serialisation though is faster still since we can avoid reflection and the data is already in serialised form The same is true for deserialisation So we can almost double the speed of serialisation frameworks like Kryo with even smaller packet size
  • 40. 40 JMH BENCHMARKS JMH is an open source micro-benchmarking framework, the following are timings comparing Kryo and our own binary serialisation… Benchmark Mode Cnt Score Error Units IoBenchmark.deSer sample 58102 171.596 ± 0.541 ns/op IoBenchmark.ser sample 58794 169.579 ± 0.546 ns/op Benchmark Mode Cnt Score Error Units KryoBenchmark.deSer sample 26423 378.121 ± 1.096 ns/op KryoBenchmark.ser sample 24951 400.387 ± 1.512 ns/op http://openjdk.java.net/projects/code-tools/jmh/ https://github.com/eishay/jvm-serializers
  • 41. 41 ANALYTICS When we have a lot of data there seems to be an assumption that we need Hadoop Hadoop is an implementation of Map / Reduce If we shrink the data then we can use a lambda for the same task… .collect(Collectors.groupingBy(f -> f.getSignedCurrencyAmount() .getCurrencyAmount().getCurrency(), Collectors.reducing(BigDecimal.ZERO, t -> t.getSignedCurrencyAmount() .getCurrencyAmount().getAmount(), BigDecimal::add))); Produces (in 160ms for 50,000 complex SWIFT messages) TWD=16218034.70, CHF=127406235.40, HKD=1292102912.26, EUR=831021868.46, DKK=196507853.20, CAD=137982907.68, USD=525123842.02, ZAR=19383094.78, NOK=618033504.06, AUD=218890981.32, ILS=46628.66, SGD=74032013.42 etc.
  • 42. 42 ANALYTICS Extracting data, converting it, writing it to HBase or HDFS to run in Hadoop seems like going back to ORM i.e. we’re adding a new layer of code for something we really shouldn't need Why not just reduce the size of the data and run it in memory? In theory the same could be done onto disk (SSD) rather than memory because the serialised form is fully readable So, use binary Java instead of Java Objects, shrink your data and use ordinary Java to process it 10 times smaller and 10 times faster is 5 “Moore’s Law” years That 5 years you can get back today with a few changes
  • 43. 43 HADOOP OR SPARK? It’s not really one or the other, they have different use- cases but our binary binding (PREON) used in Spark shows some amazing results…
  • 44. 44 HOW IT WORKS Performance Size Or any XML message Parser (Classic Java API) Validation (Optional step) PREON Sink (Converts CDO to PREON) XML Fully mutable API (Getters, setters, rules, validation & transformation) Fully immutable API (Getters only) PREON API (to binary FpML) PREON Source (Converts SDO to PREON) ~5-8k 10-25k < 500 bytes 10k/sec ~1m/sec ~1m/sec Identical APIs (for getters)
  • 45. 45 ANOTHER DEMO Take some raw XML (an FpML derivative, about 7.4k of XML) Parse it, mutate a few variables and then compact each one to its binary version - We do this 1 million times This takes about 100 seconds Now the test begins We take a look at the binary version (just 370 bytes) We search through the 1 million trades for data and aggregate the results Then we try it multi-threaded (using parallelStream()) A few more similar operations (I will discuss) Finally we’ll write all 1 million to disk and then read them back into another array (comparing to make sure it worked) This will be a test of serialisation
  • 46. 50 SO IT WORKS Key points from the slides… If you want performance, scalability and ease of maintenance then you need… Reduce the amount of memory you’re using Reduce the way you use the memory Try using binary data instead of objects Start coding intelligently like we used to in the 80s and 90s Or just buy much bigger, much faster machines, more RAM, bigger networks and more DnD programmers And you’ll probably get free lunches from your hardware vendors