SlideShare a Scribd company logo
Copyright © 2019 Oracle and/or its affiliates.
Does Java need inline(value) types?
What project Valhalla can bring to Java from a performance perspective.
Java Platform Group
Oracle
November, 2019
Sergey Kuksenko
InfoQ.com: News & Community Site
• Over 1,000,000 software developers, architects and CTOs read the site world-
wide every month
• 250,000 senior developers subscribe to our weekly newsletter
• Published in 4 languages (English, Chinese, Japanese and Brazilian
Portuguese)
• Post content from our QCon conferences
• 2 dedicated podcast channels: The InfoQ Podcast, with a focus on
Architecture and The Engineering Culture Podcast, with a focus on building
• 96 deep dives on innovative topics packed as downloadable emags and
minibooks
• Over 40 new content items per week
Watch the video with slide
synchronization on InfoQ.com!
https://www.infoq.com/presentations/
java-valhalla-inline-types/
Purpose of QCon
- to empower software development by facilitating the spread of
knowledge and innovation
Strategy
- practitioner-driven conference designed for YOU: influencers of
change and innovation in your teams
- speakers and topics driving the evolution and innovation
- connecting and catalyzing the influencers and innovators
Highlights
- attended by more than 12,000 delegates since 2007
- held in 9 cities worldwide
Presented at QCon San Francisco
www.qconsf.com
The following is intended to outline our general product direction. It is intended for information purposes
only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code,
or functionality, and should not be relied upon in making purchasing decisions. The development,
release, timing, and pricing of any features or functionality described for Oracle’s products may change
and remains at the sole discretion of Oracle Corporation.
Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and
prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed
discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and
Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q
under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website
at http://www.oracle.com/investor. All information in this presentation is current as of September 2019
and Oracle undertakes no duty to update any statement in light of new information or future events.
Safe Harbor
Copyright © 2019 Oracle and/or its affiliates.
Who am I?
- Java/JVM Performance Engineer at Oracle, @since 2010
- Java/JVM Performance Engineer, @since 2005
- Java/JVM Engineer, @since 1996
Copyright © 2019 Oracle and/or its affiliates.
What is Valhalla?
Copyright © 2019 Oracle and/or its affiliates.
Presenter’s Title
Organization
Month 00, 2019
Presenter’s Name
Presenter’s Title
Organization
Month 00, 2019
Presenter’s Name
Copyright © 2019 Oracle and/or its affiliates.
Valhalla Goals
• Provide denser memory layout (inline/value types)
• Specialized generics (including primitive, value types)
• Smooth library migration
• JVM cleanup (e.g. Nestmates a.k.a. JEP-181)
Copyright © 2019 Oracle and/or its affiliates.
Object Identity is the root of all evil
Copyright © 2019 Oracle and/or its affiliates.
Identity gives
• Indirection
• Allocation in heap
• Nullability
• Mutability
• Reference equality (==)
• Locking
• Puzzlers, e.g.
Integer.valueOf(42) == Integer.valueOf(42)
but
Integer.valueOf(420) != Integer.valueOf(420)
Copyright © 2019 Oracle and/or its affiliates.
Why JVM can’t eliminate it?
Copyright © 2019 Oracle and/or its affiliates.
JVM can!
Copyright © 2019 Oracle and/or its affiliates.
JVM can!
Copyright © 2019 Oracle and/or its affiliates.
JVM can, but ...
~300 articles per
year!
Copyright © 2019 Oracle and/or its affiliates.
Inline class
• is a class
• no identity
• immutable
• not nullable
• no synchronization
Copyright © 2019 Oracle and/or its affiliates.
Inline class
public inline class Complex {
private double re;
private double im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
public double re() { return re; }
public double im() { return im; }
...
Copyright © 2019 Oracle and/or its affiliates.
Inline class
public inline class Complex {
private double re;
private double im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
public double re() { return re; }
public double im() { return im; }
...
final
im
plies
extends Object
im
plies
implements I1,...,In
allows
Copyright © 2019 Oracle and/or its affiliates.
Identity of indiscernibles
‘==’ on inline classes
deny at all
always false substitutability check
(recursive ‘==’ for each field)
OR
current choice
Copyright © 2019 Oracle and/or its affiliates.
Inline class means inlineable
• JVM decides if:
●
allocate on heap
OR
●
put on stack (locals, parameters, result)
●
inline into container class
●
inline into array (flattened array)
Copyright © 2019 Oracle and/or its affiliates.
Inline class
• Inline types are subtypes of Object (interface)
• Inline arrays are covariant with Object[]
(arrays of interface)
Copyright © 2019 Oracle and/or its affiliates.
Boxing vs boxing
• V? - nullable twin of ‘V’
• means all values of V + ‘null’
• Integer - nullable twin of ‘int’
• But Integer has full identity
compare to:
Float like a butterfly, Sting like a bee
Code like a class, Work like an int
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
Local variable
int count(Complex c) {
Complex z = c;
for (int i = 1; i < MAX_ITERATION; i++) {
if (z.modulus() >= 4.0) return i;
z = z.square().add(c);
}
return MAX_ITERATION;
}
Copyright © 2019 Oracle and/or its affiliates.
Local variable
int count(Complex c) {
Complex z = c;
for (int i = 1; i < MAX_ITERATION; i++) {
if (z.modulus() >= 4.0) return i;
z = z.square().add(c);
}
return MAX_ITERATION;
}
Copyright © 2019 Oracle and/or its affiliates.
Local variable
int count(Complex c) {
Complex z = c;
for (int i = 1; i < MAX_ITERATION; i++) {
if (z.modulus() >= 4.0) return i;
z = z.square().add(c);
}
return MAX_ITERATION;
}
Copyright © 2019 Oracle and/or its affiliates.
Reference vs Inline (Mandelbrot, 500x500)
Inline class:
• 4x less data loads
• 42x less L1 cache misses
• 5x less L3 cache misses
• 5x less dTLB misses
Copyright © 2019 Oracle and/or its affiliates.
Scalability (Mandelbrot, 500x500)
16x
Copyright © 2019 Oracle and/or its affiliates.
Method parameters/result
static Value ackermann(Value x, Value y) {
return x.isZero() ? y.inc() :
(y.isZero() ? ackermann(x.dec(), new Value(1)) :
ackermann(x.dec(), ackermann(x, y.dec())));
}
Copyright © 2019 Oracle and/or its affiliates.
Method parameters/result
static Value ackermann(Value x, Value y) {
return x.isZero() ? y.inc() :
(y.isZero() ? ackermann(x.dec(), new Value(1)) :
ackermann(x.dec(), ackermann(x, y.dec())));
}
Copyright © 2019 Oracle and/or its affiliates.
Array access
ref a
b
c
a
b
c
VS
Copyright © 2019 Oracle and/or its affiliates.
Array Random Access
Copyright © 2019 Oracle and/or its affiliates.
Array Sequential Access
Collateral Damage
in legacy world
Copyright © 2019 Oracle and/or its affiliates.
The following section is intended to outline Valhalla
current status and development. It is intended for
information purposes only, and may not be incorporated
into any contract (or slowdown blaming). Any adverted
performance regression maybe a subject to removal.
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
Inline in heap (“boxing”)
• Object o = my_inline_value;
• Interface i = my_inline_value;
• Value? nullable_value = my_inline_value;
• JVM decided
Copyright © 2019 Oracle and/or its affiliates.
Reference comparison
just compare it if <both refs are inline>
if <classes are same>
then
check substitutability
else
false
else
just compare it
Glorious pre Valhalla past Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
If object is inline class
Mark Word
Klass ptr
My Object
Class Desc
ptr
Copyright © 2019 Oracle and/or its affiliates.
If object is inline class
Mark Word
Klass ptr
My Object
Class Desc
ptr
Use Mark Word
Copyright © 2019 Oracle and/or its affiliates.
Mark Word
// 64 bits:
// --------
// unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object)
// JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object)
// "1" :54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased always locked object)
// PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
// size:64 ----------------------------------------------------->| (CMS free block)
//
// unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object)
// JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object)
// narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)
// unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)
Copyright © 2019 Oracle and/or its affiliates.
Mark Word
// 64 bits:
// --------
// unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object)
// JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object)
// "1" :54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased always locked object)
// PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
// size:64 ----------------------------------------------------->| (CMS free block)
//
// unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object)
// JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object)
// narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)
// unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)
// [ <unused> | larval |1| epoch | age | 1 | 01] permanently locked
Copyright © 2019 Oracle and/or its affiliates.
Reference comparison a.k.a. ‘acmp’
...
if (o1 == o2) {
... = x + 1;
} else {
... = x - 1;
}
...
Copyright © 2019 Oracle and/or its affiliates.
acmp –XX:–EnableValhalla
cmp %rdx,%rcx
inc %eax dec %eax
Copyright © 2019 Oracle and/or its affiliates.
acmp –XX:+EnableValhalla
cmp %rdx,%rcx
inc %eax
dec %eax
test %rcx,%rcx
mov $0x405,%r10
and (%rcx),%r10
cmp $0x405,%r10
mov 0x8(%rdx),%r11
mov 0x8(%rcx),%r10
cmp %r11,%r10
test %rdx,%rdx
#invokedynamic
...
Copyright © 2019 Oracle and/or its affiliates.
acmp –XX:+EnableValhalla
cmp %rdx,%rcx
inc %eax
dec %eax
test %rcx,%rcx
mov $0x405,%r10
and (%rcx),%r10
cmp $0x405,%r10
mov 0x8(%rdx),%r11
mov 0x8(%rcx),%r10
cmp %r11,%r10
test %rdx,%rdx
#invokedynamic
...
Value
World
Reference
World
Copyright © 2019 Oracle and/or its affiliates.
acmp
• Complex code
• Additional loads
• invokedynamic prevents loop unrolling
Copyright © 2019 Oracle and/or its affiliates.
acmp performance
Copyright © 2019 Oracle and/or its affiliates.
synchronized(obj)
do all synch stuff if <ref is inline class>
then
throw exception
else
do all synch stuff
Glorious pre Valhalla past Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
synchronized(obj)
do all synch stuff if <ref is inline class>
then
throw exception
else
do all synch stuff
< 1% difference
Glorious pre Valhalla past Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
Arrays (Object[])
• Good old reference array
• Array of inline classes in heap
●
references, but not nullable
• Flattened array of inline classes
Object[] may be:
Copyright © 2019 Oracle and/or its affiliates.
Arrays (Object[])
Mark Word
Klass ptr
My Array Class Desc
ptr
length
Copyright © 2019 Oracle and/or its affiliates.
Arrays (Object[])
Mark Word
Klass ptr
My Array Class Desc
ptr
length
flattened_bit
null_free_bit
Copyright © 2019 Oracle and/or its affiliates.
Arrays (Object[])
• Any access to Klass ptr required clearing:
●
and $0x1fffffff,%reg
• HotSpot is good enough at eliminating it
●
knowing that it isn’t an array
Copyright © 2019 Oracle and/or its affiliates.
Load from Object[]
element size is the same:
just load it
if <array is flattened>
find element size
load it
do boxing if needed
else
just load it
Glorious pre Valhalla past Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
Store to Object[]
do ArrayStoreCheck
store if ok
do ArrayStoreCheck
if <array is flattened>
find element size
do unboxing if needed
store
else
store
Glorious pre Valhalla past Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
Object[] access
• Targeting benchmarks: –2% . . – 10%
• Solution: aggressive loop hoisting and loop duplication
(in progress)
Copyright © 2019 Oracle and/or its affiliates.
Inline vs inline
Integer[] i1 = new Integer[1000];
Integer[] i2 = new Integer[1000];
@Setup
public void setup() {
for (int i = 0; i < 1000; i++)
i1[i] = i2[i] = i;
i2[999] = 394857623;
}
@Benchmark
public boolean arrayEquals() {
return Arrays.equals(i1, i2);
}
Copyright © 2019 Oracle and/or its affiliates.
Inline vs inline
Integer[] i1 = new Integer[1000];
Integer[] i2 = new Integer[1000];
@Setup
public void setup() {
for (int i = 0; i < 1000; i++)
i1[i] = i2[i] = i;
i2[999] = 394857623;
}
@Benchmark
public boolean arrayEquals() {
return Arrays.equals(i1, i2);
}
Copyright © 2019 Oracle and/or its affiliates.
Methods inline tree
-XX:-EnableValhalla
...
- @ java.util.Arrays::equals [..., bytes=57, insts=352]
(inlined: inline (hot))
- @ java.util.Objects::equals [..., bytes=23, insts=128]
(inlined: inline (hot))
-XX:+EnableValhalla
...
- @ java.util.Arrays::equals [..., bytes=57, insts=1760]
(inline failed: already compiled into a big method)
Copyright © 2019 Oracle and/or its affiliates.
Methods inline tree
-XX:-EnableValhalla
...
- @ java.util.Arrays::equals [..., bytes=57, insts=352]
(inlined: inline (hot))
- @ java.util.Objects::equals [..., bytes=23, insts=128]
(inlined: inline (hot))
-XX:+EnableValhalla
...
- @ java.util.Arrays::equals [..., bytes=57, insts=1760]
(inline failed: already compiled into a big method)
Size of
generated code
Copyright © 2019 Oracle and/or its affiliates.
Current status
• Checked ~30 big benchmarks:
●
No regressions more than 2%
• Checked ~1600 microbenchmarks:
●
1200 – ±0% at the first run
●
300 – fixed
●
100 – less 5% (in progress)
●
1 – 14% regression (in progress)
Brighter post Valhalla future
Copyright © 2019 Oracle and/or its affiliates.
Presenter’s Title
Organization
Month 00, 2019
Copyright © 2019 Oracle and/or its affiliates.
Arithmetic types
• Complex matrix multiplication (100x100)
ref Complex 12.6 ms
inline Complex 2.7 ms
inline Complex +
cache friendly
algorithm
2.1 ms
Copyright © 2019 Oracle and/or its affiliates.
java.util.Optional
public class HashMap<K,V> ... {
...
/**
* Returns the value to which the specified key is mapped,
* or null if this map contains no mapping for the key.
...
*/
public V get(Object key)
Copyright © 2019 Oracle and/or its affiliates.
java.util.Optional
public class HashMap<K,V> ... {
...
/**
* Returns the value to which the specified key is mapped,
* or null if this map contains no mapping for the key.
...
*/
public V get(Object key)
Not a good idea
Copyright © 2019 Oracle and/or its affiliates.
java.util.Optional
public class HashMap<K,V> ... {
...
/**
* Returns an Optional describing the value to which the specified
* key is mapped, or an empty Optional if this map contains no
* mapping for the key.
...
*/
public Optional<V> get(Object key)
What if?
Copyright © 2019 Oracle and/or its affiliates.
java.util.Optional
• 1000000 gets from 1000000 map
Copyright © 2019 Oracle and/or its affiliates.
Example: map with complex key
• Map from <K1
, …, KN
> → <V>
Two ways to implement:
1. Map<CompositeKey<K1
, …, KN
>, V>
2. Map<K1
, Map …, Map<KN
, V>...>
Copyright © 2019 Oracle and/or its affiliates.
Example: Map from <Integer, Integer> <Integer>→ <Integer>
• 1000000 gets from 1000000 map
Copyright © 2019 Oracle and/or its affiliates.
HashMap inside
key
value
next
key
value
next
Entry
Entry
key
value
next
Entry
key
value
next
Entry
vs
key
value
next
key
value
next
key
value
next
key
value
next
Entry
Copyright © 2019 Oracle and/or its affiliates.
HashMap experiments
• Classic HashMap.get(): ~75 ns
• Experimental HashMap.get(): ~60 ns
? Faster only for large maps
? ‘put’ is slower
... to be continued
Copyright © 2019 Oracle and/or its affiliates.
Iteration
HashMap<Integer, Integer> map; // map.size() == 1000000
@Benchmark
public int sumValuesInMap() {
int s = 0;
for (Integer i : map.values()) {
s += i;
}
return s;
}
Copyright © 2019 Oracle and/or its affiliates.
Iteration
HashMap<Integer, Integer> map; // map.size() == 1000000
@Benchmark
public int sumValuesInMap() {
int s = 0;
for (Integer i : map.values()) {
s += i;
}
return s;
}
Lucky case 33 ms
Unlucky case 55 ms
Copyright © 2019 Oracle and/or its affiliates.
Iteration
HashMap<Integer, Integer> map; // map.size() == 1000000
@Benchmark
public int sumValuesInMap() {
int s = 0;
for (Iterator<Integer> iterator = map.values().iterator();
iterator.hasNext(); ) {
s += iterator.next();
}
return s;
}
scalarized Lucky case 33 ms
on heap Unlucky case 55 ms
Copyright © 2019 Oracle and/or its affiliates.
Inside HashMap
abstract class HashIterator {
...
Node<K,V> next; // next entry to return
Node<K,V> current; // current entry
final Node<K,V> nextNode() {
...
if ((next = (current = e).next) == null && (t = table) != null) {
do {}
while (index < t.length && (next = t[index++]) == null);
}
return e;
}
...
Copyright © 2019 Oracle and/or its affiliates.
Inside HashMap
abstract class HashIterator {
...
Node<K,V> next; // next entry to return
Node<K,V> current; // current entry
final Node<K,V> nextNode() {
...
if ((next = (current = e).next) == null && (t = table) != null) {
do {}
while (index < t.length && (next = t[index++]) == null);
}
return e;
}
...
Write to reference field
GC write barriers
Copyright © 2019 Oracle and/or its affiliates.
inline Cursor
public interface Cursor<V> {
boolean hasElement();
V get();
Cursor<V> next();
}
Copyright © 2019 Oracle and/or its affiliates.
inline Cursor
@Benchmark
public int sumValuesInMap() {
int s = 0;
for (Cursor<Integer> cursor = map.values().cursor();
cursor.hasElement();
cursor = cursor.next()) {
s += cursor.get();
}
return s;
}
Copyright © 2019 Oracle and/or its affiliates.
inline Cursor
@Benchmark
public int sumValuesInMap() {
int s = 0;
for (Cursor<Integer> cursor = map.values().cursor();
cursor.hasElement();
cursor = cursor.next()) {
s += cursor.get();
}
return s;
}
Scalarized Iterator 33 ms
On heap Iterator 55 ms
inline Cursor 32 ms
Copyright © 2019 Oracle and/or its affiliates.
Move/Copy data
• Reference – easy, only reference is moved
• Inline – all data should be moved
– Sort:
Reference – default TimSort from JDK
Inline – reimplented TimSort
indexed Inline – sort ‘int’ indices first, then copy
Copyright © 2019 Oracle and/or its affiliates.
size==400, fit into L1 cache
Copyright © 2019 Oracle and/or its affiliates.
size==4000, fit into L2 cache
Copyright © 2019 Oracle and/or its affiliates.
size==40000, fit into L3 cache
Copyright © 2019 Oracle and/or its affiliates.
size==400000, slightly > L3 cache
Copyright © 2019 Oracle and/or its affiliates.
size==4000000, much more L3 cache
Copyright © 2019 Oracle and/or its affiliates.
Move/Copy data
• Dense location is better than moving less
Copyright © 2019 Oracle and/or its affiliates.
Inline classes
• Dense, HW-friendly memory layout
• More control on heap allocations:
●
less GC pressure
●
less GC barriers
Better performance!
Copyright © 2019 Oracle and/or its affiliates.
Links
• Wiki:
https://wiki.openjdk.java.net/display/valhalla/Main
• Mailing lists:
http://mail.openjdk.java.net/mailman/listinfo/valhalla-dev
http://mail.openjdk.java.net/mailman/listinfo/valhalla-spec-observers
• Repository:
http://hg.openjdk.java.net/valhalla
Thank You
Copyright © 2019 Oracle and/or its affiliates.
Java Platform Group
Oracle
November, 2019
Sergey Kuksenko
Watch the video with slide
synchronization on InfoQ.com!
https://www.infoq.com/presentations/
java-valhalla-inline-types/

More Related Content

What's hot (20)

PDF
Burns jsf-confess-2015
Edward Burns
 
PPTX
Project Jigsaw in JDK9
Simon Ritter
 
PPTX
Java 101
javafxpert
 
PPTX
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
PPTX
What's New in Java 8
javafxpert
 
PDF
Java: Create The Future Keynote
Simon Ritter
 
PDF
Java EE 7 from an HTML5 Perspective, JavaLand 2015
Edward Burns
 
PDF
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Edureka!
 
PPTX
Ed presents JSF 2.2 and WebSocket to Gameduell.
Edward Burns
 
PDF
JSF 2.2 Input Output JavaLand 2015
Edward Burns
 
PDF
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
David Buck
 
PDF
JSONB introduction and comparison with other frameworks
Dmitry Kornilov
 
PPTX
Improved Developer Productivity In JDK8
Simon Ritter
 
PDF
MySQL Developer Day conference: MySQL Replication and Scalability
Shivji Kumar Jha
 
PDF
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Edureka!
 
PDF
Introduction to Loops in Java | For, While, Do While, Infinite Loops | Edureka
Edureka!
 
PDF
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Edureka!
 
PDF
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Edureka!
 
PPTX
55 New Features in Java SE 8
Simon Ritter
 
PDF
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Edureka!
 
Burns jsf-confess-2015
Edward Burns
 
Project Jigsaw in JDK9
Simon Ritter
 
Java 101
javafxpert
 
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
What's New in Java 8
javafxpert
 
Java: Create The Future Keynote
Simon Ritter
 
Java EE 7 from an HTML5 Perspective, JavaLand 2015
Edward Burns
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Edureka!
 
Ed presents JSF 2.2 and WebSocket to Gameduell.
Edward Burns
 
JSF 2.2 Input Output JavaLand 2015
Edward Burns
 
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
David Buck
 
JSONB introduction and comparison with other frameworks
Dmitry Kornilov
 
Improved Developer Productivity In JDK8
Simon Ritter
 
MySQL Developer Day conference: MySQL Replication and Scalability
Shivji Kumar Jha
 
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Edureka!
 
Introduction to Loops in Java | For, While, Do While, Infinite Loops | Edureka
Edureka!
 
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Edureka!
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Edureka!
 
55 New Features in Java SE 8
Simon Ritter
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Edureka!
 

Similar to Does Java Need Inline Types? What Project Valhalla Can Bring to Java (20)

PDF
Projects Valhalla and Loom at IT Tage 2021
Vadym Kazulkin
 
PDF
JavaFest. Вадим Казулькин. Projects Valhalla, Loom and GraalVM
FestGroup
 
PDF
Projects Valhalla, Loom and GraalVM at virtual JavaFest 2020 in Kiev, Ukraine...
Vadym Kazulkin
 
PDF
Projects Valhalla, Loom and GraalVM at JUG Mainz
Vadym Kazulkin
 
PDF
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
David Buck
 
PDF
Projects Valhalla and Loom DWX 2022
Vadym Kazulkin
 
PPTX
Introduction to value types
Narendran Solai Sridharan
 
PDF
Java Full Throttle
José Paumard
 
PDF
Lagergren jvmls-2013-final
Marcus Lagergren
 
PDF
Java SE 8
Simon Ritter
 
PPTX
Core java
Ganesh Chittalwar
 
PPTX
Java PPT
Dilip Kr. Jangir
 
PDF
The State of Managed Runtimes 2013, by Attila Szegedi
ZeroTurnaround
 
PPTX
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
PDF
Inheritance Versus Roles - The In-Depth Version
Curtis Poe
 
PDF
invokedynamic for Mere Mortals [Code One 2019]
David Buck
 
PDF
Jax keynote
Marcus Lagergren
 
PPTX
Projects Valhalla, Loom and GraalVM at JCon 2020
Vadym Kazulkin
 
PDF
Introduction to JVM JIT Optimizations
diegosoftware
 
PPT
core java
Vinodh Kumar
 
Projects Valhalla and Loom at IT Tage 2021
Vadym Kazulkin
 
JavaFest. Вадим Казулькин. Projects Valhalla, Loom and GraalVM
FestGroup
 
Projects Valhalla, Loom and GraalVM at virtual JavaFest 2020 in Kiev, Ukraine...
Vadym Kazulkin
 
Projects Valhalla, Loom and GraalVM at JUG Mainz
Vadym Kazulkin
 
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
David Buck
 
Projects Valhalla and Loom DWX 2022
Vadym Kazulkin
 
Introduction to value types
Narendran Solai Sridharan
 
Java Full Throttle
José Paumard
 
Lagergren jvmls-2013-final
Marcus Lagergren
 
Java SE 8
Simon Ritter
 
The State of Managed Runtimes 2013, by Attila Szegedi
ZeroTurnaround
 
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
Inheritance Versus Roles - The In-Depth Version
Curtis Poe
 
invokedynamic for Mere Mortals [Code One 2019]
David Buck
 
Jax keynote
Marcus Lagergren
 
Projects Valhalla, Loom and GraalVM at JCon 2020
Vadym Kazulkin
 
Introduction to JVM JIT Optimizations
diegosoftware
 
core java
Vinodh Kumar
 
Ad

More from C4Media (20)

PDF
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
C4Media
 
PDF
Next Generation Client APIs in Envoy Mobile
C4Media
 
PDF
Software Teams and Teamwork Trends Report Q1 2020
C4Media
 
PDF
Understand the Trade-offs Using Compilers for Java Applications
C4Media
 
PDF
Kafka Needs No Keeper
C4Media
 
PDF
High Performing Teams Act Like Owners
C4Media
 
PDF
Service Meshes- The Ultimate Guide
C4Media
 
PDF
Shifting Left with Cloud Native CI/CD
C4Media
 
PDF
CI/CD for Machine Learning
C4Media
 
PDF
Fault Tolerance at Speed
C4Media
 
PDF
Architectures That Scale Deep - Regaining Control in Deep Systems
C4Media
 
PDF
ML in the Browser: Interactive Experiences with Tensorflow.js
C4Media
 
PDF
Build Your Own WebAssembly Compiler
C4Media
 
PDF
User & Device Identity for Microservices @ Netflix Scale
C4Media
 
PDF
Scaling Patterns for Netflix's Edge
C4Media
 
PDF
Make Your Electron App Feel at Home Everywhere
C4Media
 
PDF
The Talk You've Been Await-ing For
C4Media
 
PDF
Future of Data Engineering
C4Media
 
PDF
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
C4Media
 
PDF
Navigating Complexity: High-performance Delivery and Discovery Teams
C4Media
 
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
C4Media
 
Next Generation Client APIs in Envoy Mobile
C4Media
 
Software Teams and Teamwork Trends Report Q1 2020
C4Media
 
Understand the Trade-offs Using Compilers for Java Applications
C4Media
 
Kafka Needs No Keeper
C4Media
 
High Performing Teams Act Like Owners
C4Media
 
Service Meshes- The Ultimate Guide
C4Media
 
Shifting Left with Cloud Native CI/CD
C4Media
 
CI/CD for Machine Learning
C4Media
 
Fault Tolerance at Speed
C4Media
 
Architectures That Scale Deep - Regaining Control in Deep Systems
C4Media
 
ML in the Browser: Interactive Experiences with Tensorflow.js
C4Media
 
Build Your Own WebAssembly Compiler
C4Media
 
User & Device Identity for Microservices @ Netflix Scale
C4Media
 
Scaling Patterns for Netflix's Edge
C4Media
 
Make Your Electron App Feel at Home Everywhere
C4Media
 
The Talk You've Been Await-ing For
C4Media
 
Future of Data Engineering
C4Media
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
C4Media
 
Navigating Complexity: High-performance Delivery and Discovery Teams
C4Media
 
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 

Does Java Need Inline Types? What Project Valhalla Can Bring to Java

  • 1. Copyright © 2019 Oracle and/or its affiliates. Does Java need inline(value) types? What project Valhalla can bring to Java from a performance perspective. Java Platform Group Oracle November, 2019 Sergey Kuksenko
  • 2. InfoQ.com: News & Community Site • Over 1,000,000 software developers, architects and CTOs read the site world- wide every month • 250,000 senior developers subscribe to our weekly newsletter • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • 2 dedicated podcast channels: The InfoQ Podcast, with a focus on Architecture and The Engineering Culture Podcast, with a focus on building • 96 deep dives on innovative topics packed as downloadable emags and minibooks • Over 40 new content items per week Watch the video with slide synchronization on InfoQ.com! https://www.infoq.com/presentations/ java-valhalla-inline-types/
  • 3. Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide Presented at QCon San Francisco www.qconsf.com
  • 4. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2019 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2019 Oracle and/or its affiliates.
  • 5. Who am I? - Java/JVM Performance Engineer at Oracle, @since 2010 - Java/JVM Performance Engineer, @since 2005 - Java/JVM Engineer, @since 1996 Copyright © 2019 Oracle and/or its affiliates.
  • 6. What is Valhalla? Copyright © 2019 Oracle and/or its affiliates. Presenter’s Title Organization Month 00, 2019 Presenter’s Name Presenter’s Title Organization Month 00, 2019 Presenter’s Name
  • 7. Copyright © 2019 Oracle and/or its affiliates. Valhalla Goals • Provide denser memory layout (inline/value types) • Specialized generics (including primitive, value types) • Smooth library migration • JVM cleanup (e.g. Nestmates a.k.a. JEP-181)
  • 8. Copyright © 2019 Oracle and/or its affiliates. Object Identity is the root of all evil
  • 9. Copyright © 2019 Oracle and/or its affiliates. Identity gives • Indirection • Allocation in heap • Nullability • Mutability • Reference equality (==) • Locking • Puzzlers, e.g. Integer.valueOf(42) == Integer.valueOf(42) but Integer.valueOf(420) != Integer.valueOf(420)
  • 10. Copyright © 2019 Oracle and/or its affiliates. Why JVM can’t eliminate it?
  • 11. Copyright © 2019 Oracle and/or its affiliates. JVM can!
  • 12. Copyright © 2019 Oracle and/or its affiliates. JVM can!
  • 13. Copyright © 2019 Oracle and/or its affiliates. JVM can, but ... ~300 articles per year!
  • 14. Copyright © 2019 Oracle and/or its affiliates. Inline class • is a class • no identity • immutable • not nullable • no synchronization
  • 15. Copyright © 2019 Oracle and/or its affiliates. Inline class public inline class Complex { private double re; private double im; public Complex(double re, double im) { this.re = re; this.im = im; } public double re() { return re; } public double im() { return im; } ...
  • 16. Copyright © 2019 Oracle and/or its affiliates. Inline class public inline class Complex { private double re; private double im; public Complex(double re, double im) { this.re = re; this.im = im; } public double re() { return re; } public double im() { return im; } ... final im plies extends Object im plies implements I1,...,In allows
  • 17. Copyright © 2019 Oracle and/or its affiliates. Identity of indiscernibles ‘==’ on inline classes deny at all always false substitutability check (recursive ‘==’ for each field) OR current choice
  • 18. Copyright © 2019 Oracle and/or its affiliates. Inline class means inlineable • JVM decides if: ● allocate on heap OR ● put on stack (locals, parameters, result) ● inline into container class ● inline into array (flattened array)
  • 19. Copyright © 2019 Oracle and/or its affiliates. Inline class • Inline types are subtypes of Object (interface) • Inline arrays are covariant with Object[] (arrays of interface)
  • 20. Copyright © 2019 Oracle and/or its affiliates. Boxing vs boxing • V? - nullable twin of ‘V’ • means all values of V + ‘null’ • Integer - nullable twin of ‘int’ • But Integer has full identity compare to:
  • 21. Float like a butterfly, Sting like a bee Code like a class, Work like an int Copyright © 2019 Oracle and/or its affiliates.
  • 22. Copyright © 2019 Oracle and/or its affiliates. Local variable int count(Complex c) { Complex z = c; for (int i = 1; i < MAX_ITERATION; i++) { if (z.modulus() >= 4.0) return i; z = z.square().add(c); } return MAX_ITERATION; }
  • 23. Copyright © 2019 Oracle and/or its affiliates. Local variable int count(Complex c) { Complex z = c; for (int i = 1; i < MAX_ITERATION; i++) { if (z.modulus() >= 4.0) return i; z = z.square().add(c); } return MAX_ITERATION; }
  • 24. Copyright © 2019 Oracle and/or its affiliates. Local variable int count(Complex c) { Complex z = c; for (int i = 1; i < MAX_ITERATION; i++) { if (z.modulus() >= 4.0) return i; z = z.square().add(c); } return MAX_ITERATION; }
  • 25. Copyright © 2019 Oracle and/or its affiliates. Reference vs Inline (Mandelbrot, 500x500) Inline class: • 4x less data loads • 42x less L1 cache misses • 5x less L3 cache misses • 5x less dTLB misses
  • 26. Copyright © 2019 Oracle and/or its affiliates. Scalability (Mandelbrot, 500x500) 16x
  • 27. Copyright © 2019 Oracle and/or its affiliates. Method parameters/result static Value ackermann(Value x, Value y) { return x.isZero() ? y.inc() : (y.isZero() ? ackermann(x.dec(), new Value(1)) : ackermann(x.dec(), ackermann(x, y.dec()))); }
  • 28. Copyright © 2019 Oracle and/or its affiliates. Method parameters/result static Value ackermann(Value x, Value y) { return x.isZero() ? y.inc() : (y.isZero() ? ackermann(x.dec(), new Value(1)) : ackermann(x.dec(), ackermann(x, y.dec()))); }
  • 29. Copyright © 2019 Oracle and/or its affiliates. Array access ref a b c a b c VS
  • 30. Copyright © 2019 Oracle and/or its affiliates. Array Random Access
  • 31. Copyright © 2019 Oracle and/or its affiliates. Array Sequential Access
  • 32. Collateral Damage in legacy world Copyright © 2019 Oracle and/or its affiliates.
  • 33. The following section is intended to outline Valhalla current status and development. It is intended for information purposes only, and may not be incorporated into any contract (or slowdown blaming). Any adverted performance regression maybe a subject to removal. Copyright © 2019 Oracle and/or its affiliates.
  • 34. Copyright © 2019 Oracle and/or its affiliates. Inline in heap (“boxing”) • Object o = my_inline_value; • Interface i = my_inline_value; • Value? nullable_value = my_inline_value; • JVM decided
  • 35. Copyright © 2019 Oracle and/or its affiliates. Reference comparison just compare it if <both refs are inline> if <classes are same> then check substitutability else false else just compare it Glorious pre Valhalla past Brighter post Valhalla future
  • 36. Copyright © 2019 Oracle and/or its affiliates. If object is inline class Mark Word Klass ptr My Object Class Desc ptr
  • 37. Copyright © 2019 Oracle and/or its affiliates. If object is inline class Mark Word Klass ptr My Object Class Desc ptr Use Mark Word
  • 38. Copyright © 2019 Oracle and/or its affiliates. Mark Word // 64 bits: // -------- // unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object) // JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object) // "1" :54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased always locked object) // PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object) // size:64 ----------------------------------------------------->| (CMS free block) // // unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object) // JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object) // narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object) // unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)
  • 39. Copyright © 2019 Oracle and/or its affiliates. Mark Word // 64 bits: // -------- // unused:25 hash:31 -->| unused:1 age:4 biased_lock:1 lock:2 (normal object) // JavaThread*:54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased object) // "1" :54 epoch:2 unused:1 age:4 biased_lock:1 lock:2 (biased always locked object) // PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object) // size:64 ----------------------------------------------------->| (CMS free block) // // unused:25 hash:31 -->| cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && normal object) // JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2 (COOPs && biased object) // narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object) // unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block) // [ <unused> | larval |1| epoch | age | 1 | 01] permanently locked
  • 40. Copyright © 2019 Oracle and/or its affiliates. Reference comparison a.k.a. ‘acmp’ ... if (o1 == o2) { ... = x + 1; } else { ... = x - 1; } ...
  • 41. Copyright © 2019 Oracle and/or its affiliates. acmp –XX:–EnableValhalla cmp %rdx,%rcx inc %eax dec %eax
  • 42. Copyright © 2019 Oracle and/or its affiliates. acmp –XX:+EnableValhalla cmp %rdx,%rcx inc %eax dec %eax test %rcx,%rcx mov $0x405,%r10 and (%rcx),%r10 cmp $0x405,%r10 mov 0x8(%rdx),%r11 mov 0x8(%rcx),%r10 cmp %r11,%r10 test %rdx,%rdx #invokedynamic ...
  • 43. Copyright © 2019 Oracle and/or its affiliates. acmp –XX:+EnableValhalla cmp %rdx,%rcx inc %eax dec %eax test %rcx,%rcx mov $0x405,%r10 and (%rcx),%r10 cmp $0x405,%r10 mov 0x8(%rdx),%r11 mov 0x8(%rcx),%r10 cmp %r11,%r10 test %rdx,%rdx #invokedynamic ... Value World Reference World
  • 44. Copyright © 2019 Oracle and/or its affiliates. acmp • Complex code • Additional loads • invokedynamic prevents loop unrolling
  • 45. Copyright © 2019 Oracle and/or its affiliates. acmp performance
  • 46. Copyright © 2019 Oracle and/or its affiliates. synchronized(obj) do all synch stuff if <ref is inline class> then throw exception else do all synch stuff Glorious pre Valhalla past Brighter post Valhalla future
  • 47. Copyright © 2019 Oracle and/or its affiliates. synchronized(obj) do all synch stuff if <ref is inline class> then throw exception else do all synch stuff < 1% difference Glorious pre Valhalla past Brighter post Valhalla future
  • 48. Copyright © 2019 Oracle and/or its affiliates. Arrays (Object[]) • Good old reference array • Array of inline classes in heap ● references, but not nullable • Flattened array of inline classes Object[] may be:
  • 49. Copyright © 2019 Oracle and/or its affiliates. Arrays (Object[]) Mark Word Klass ptr My Array Class Desc ptr length
  • 50. Copyright © 2019 Oracle and/or its affiliates. Arrays (Object[]) Mark Word Klass ptr My Array Class Desc ptr length flattened_bit null_free_bit
  • 51. Copyright © 2019 Oracle and/or its affiliates. Arrays (Object[]) • Any access to Klass ptr required clearing: ● and $0x1fffffff,%reg • HotSpot is good enough at eliminating it ● knowing that it isn’t an array
  • 52. Copyright © 2019 Oracle and/or its affiliates. Load from Object[] element size is the same: just load it if <array is flattened> find element size load it do boxing if needed else just load it Glorious pre Valhalla past Brighter post Valhalla future
  • 53. Copyright © 2019 Oracle and/or its affiliates. Store to Object[] do ArrayStoreCheck store if ok do ArrayStoreCheck if <array is flattened> find element size do unboxing if needed store else store Glorious pre Valhalla past Brighter post Valhalla future
  • 54. Copyright © 2019 Oracle and/or its affiliates. Object[] access • Targeting benchmarks: –2% . . – 10% • Solution: aggressive loop hoisting and loop duplication (in progress)
  • 55. Copyright © 2019 Oracle and/or its affiliates. Inline vs inline Integer[] i1 = new Integer[1000]; Integer[] i2 = new Integer[1000]; @Setup public void setup() { for (int i = 0; i < 1000; i++) i1[i] = i2[i] = i; i2[999] = 394857623; } @Benchmark public boolean arrayEquals() { return Arrays.equals(i1, i2); }
  • 56. Copyright © 2019 Oracle and/or its affiliates. Inline vs inline Integer[] i1 = new Integer[1000]; Integer[] i2 = new Integer[1000]; @Setup public void setup() { for (int i = 0; i < 1000; i++) i1[i] = i2[i] = i; i2[999] = 394857623; } @Benchmark public boolean arrayEquals() { return Arrays.equals(i1, i2); }
  • 57. Copyright © 2019 Oracle and/or its affiliates. Methods inline tree -XX:-EnableValhalla ... - @ java.util.Arrays::equals [..., bytes=57, insts=352] (inlined: inline (hot)) - @ java.util.Objects::equals [..., bytes=23, insts=128] (inlined: inline (hot)) -XX:+EnableValhalla ... - @ java.util.Arrays::equals [..., bytes=57, insts=1760] (inline failed: already compiled into a big method)
  • 58. Copyright © 2019 Oracle and/or its affiliates. Methods inline tree -XX:-EnableValhalla ... - @ java.util.Arrays::equals [..., bytes=57, insts=352] (inlined: inline (hot)) - @ java.util.Objects::equals [..., bytes=23, insts=128] (inlined: inline (hot)) -XX:+EnableValhalla ... - @ java.util.Arrays::equals [..., bytes=57, insts=1760] (inline failed: already compiled into a big method) Size of generated code
  • 59. Copyright © 2019 Oracle and/or its affiliates. Current status • Checked ~30 big benchmarks: ● No regressions more than 2% • Checked ~1600 microbenchmarks: ● 1200 – ±0% at the first run ● 300 – fixed ● 100 – less 5% (in progress) ● 1 – 14% regression (in progress)
  • 60. Brighter post Valhalla future Copyright © 2019 Oracle and/or its affiliates. Presenter’s Title Organization Month 00, 2019
  • 61. Copyright © 2019 Oracle and/or its affiliates. Arithmetic types • Complex matrix multiplication (100x100) ref Complex 12.6 ms inline Complex 2.7 ms inline Complex + cache friendly algorithm 2.1 ms
  • 62. Copyright © 2019 Oracle and/or its affiliates. java.util.Optional public class HashMap<K,V> ... { ... /** * Returns the value to which the specified key is mapped, * or null if this map contains no mapping for the key. ... */ public V get(Object key)
  • 63. Copyright © 2019 Oracle and/or its affiliates. java.util.Optional public class HashMap<K,V> ... { ... /** * Returns the value to which the specified key is mapped, * or null if this map contains no mapping for the key. ... */ public V get(Object key) Not a good idea
  • 64. Copyright © 2019 Oracle and/or its affiliates. java.util.Optional public class HashMap<K,V> ... { ... /** * Returns an Optional describing the value to which the specified * key is mapped, or an empty Optional if this map contains no * mapping for the key. ... */ public Optional<V> get(Object key) What if?
  • 65. Copyright © 2019 Oracle and/or its affiliates. java.util.Optional • 1000000 gets from 1000000 map
  • 66. Copyright © 2019 Oracle and/or its affiliates. Example: map with complex key • Map from <K1 , …, KN > → <V> Two ways to implement: 1. Map<CompositeKey<K1 , …, KN >, V> 2. Map<K1 , Map …, Map<KN , V>...>
  • 67. Copyright © 2019 Oracle and/or its affiliates. Example: Map from <Integer, Integer> <Integer>→ <Integer> • 1000000 gets from 1000000 map
  • 68. Copyright © 2019 Oracle and/or its affiliates. HashMap inside key value next key value next Entry Entry key value next Entry key value next Entry vs key value next key value next key value next key value next Entry
  • 69. Copyright © 2019 Oracle and/or its affiliates. HashMap experiments • Classic HashMap.get(): ~75 ns • Experimental HashMap.get(): ~60 ns ? Faster only for large maps ? ‘put’ is slower ... to be continued
  • 70. Copyright © 2019 Oracle and/or its affiliates. Iteration HashMap<Integer, Integer> map; // map.size() == 1000000 @Benchmark public int sumValuesInMap() { int s = 0; for (Integer i : map.values()) { s += i; } return s; }
  • 71. Copyright © 2019 Oracle and/or its affiliates. Iteration HashMap<Integer, Integer> map; // map.size() == 1000000 @Benchmark public int sumValuesInMap() { int s = 0; for (Integer i : map.values()) { s += i; } return s; } Lucky case 33 ms Unlucky case 55 ms
  • 72. Copyright © 2019 Oracle and/or its affiliates. Iteration HashMap<Integer, Integer> map; // map.size() == 1000000 @Benchmark public int sumValuesInMap() { int s = 0; for (Iterator<Integer> iterator = map.values().iterator(); iterator.hasNext(); ) { s += iterator.next(); } return s; } scalarized Lucky case 33 ms on heap Unlucky case 55 ms
  • 73. Copyright © 2019 Oracle and/or its affiliates. Inside HashMap abstract class HashIterator { ... Node<K,V> next; // next entry to return Node<K,V> current; // current entry final Node<K,V> nextNode() { ... if ((next = (current = e).next) == null && (t = table) != null) { do {} while (index < t.length && (next = t[index++]) == null); } return e; } ...
  • 74. Copyright © 2019 Oracle and/or its affiliates. Inside HashMap abstract class HashIterator { ... Node<K,V> next; // next entry to return Node<K,V> current; // current entry final Node<K,V> nextNode() { ... if ((next = (current = e).next) == null && (t = table) != null) { do {} while (index < t.length && (next = t[index++]) == null); } return e; } ... Write to reference field GC write barriers
  • 75. Copyright © 2019 Oracle and/or its affiliates. inline Cursor public interface Cursor<V> { boolean hasElement(); V get(); Cursor<V> next(); }
  • 76. Copyright © 2019 Oracle and/or its affiliates. inline Cursor @Benchmark public int sumValuesInMap() { int s = 0; for (Cursor<Integer> cursor = map.values().cursor(); cursor.hasElement(); cursor = cursor.next()) { s += cursor.get(); } return s; }
  • 77. Copyright © 2019 Oracle and/or its affiliates. inline Cursor @Benchmark public int sumValuesInMap() { int s = 0; for (Cursor<Integer> cursor = map.values().cursor(); cursor.hasElement(); cursor = cursor.next()) { s += cursor.get(); } return s; } Scalarized Iterator 33 ms On heap Iterator 55 ms inline Cursor 32 ms
  • 78. Copyright © 2019 Oracle and/or its affiliates. Move/Copy data • Reference – easy, only reference is moved • Inline – all data should be moved – Sort: Reference – default TimSort from JDK Inline – reimplented TimSort indexed Inline – sort ‘int’ indices first, then copy
  • 79. Copyright © 2019 Oracle and/or its affiliates. size==400, fit into L1 cache
  • 80. Copyright © 2019 Oracle and/or its affiliates. size==4000, fit into L2 cache
  • 81. Copyright © 2019 Oracle and/or its affiliates. size==40000, fit into L3 cache
  • 82. Copyright © 2019 Oracle and/or its affiliates. size==400000, slightly > L3 cache
  • 83. Copyright © 2019 Oracle and/or its affiliates. size==4000000, much more L3 cache
  • 84. Copyright © 2019 Oracle and/or its affiliates. Move/Copy data • Dense location is better than moving less
  • 85. Copyright © 2019 Oracle and/or its affiliates. Inline classes • Dense, HW-friendly memory layout • More control on heap allocations: ● less GC pressure ● less GC barriers Better performance!
  • 86. Copyright © 2019 Oracle and/or its affiliates. Links • Wiki: https://wiki.openjdk.java.net/display/valhalla/Main • Mailing lists: http://mail.openjdk.java.net/mailman/listinfo/valhalla-dev http://mail.openjdk.java.net/mailman/listinfo/valhalla-spec-observers • Repository: http://hg.openjdk.java.net/valhalla
  • 87. Thank You Copyright © 2019 Oracle and/or its affiliates. Java Platform Group Oracle November, 2019 Sergey Kuksenko
  • 88. Watch the video with slide synchronization on InfoQ.com! https://www.infoq.com/presentations/ java-valhalla-inline-types/