SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Oracle Code One, October 2018
Dmitry Vyazelenko @DVyazelenko
Maurice Naftalin @mauricenaftalin
What Lies Beneath
Maurice Naftalin
Java 5 Java 8
2013 2014 20152017
Dmitry Vyazelenko
• Disorganiser-in-chief of the unconferences
JCrete and JAlba
• Actually understands today’s topic
What Lies Beneath
• What, another ”Hello World” talk?
• Yes! It’s about what happens when we type

• It’s amazingly complicated
— and interesting!
java Hello.java
Everybody Knows…
javac
jit
Bytecode
Java
Machine code
Java to Bytecode
private int add(int value) {
return value + 254;
}

public int compute(int value) {
return add(value / 0xdeadbeef);
}

public static void main(String[] args) {
System.out.println(new Computer().compute(0xcafebabe));
}
Result: 0xff
private int add(int value) {
return value + 254;
}

public int compute(int value) {
return add(value / 0xdeadbeef);
}

public static void main(String[] args) {
System.out.println(new Computer().compute(0xcafebabe));
}
Java to Bytecode
public int compute(int);
Code:
0: aload_0
1: iload_1
2: ldc #2 // int 0xdeadbeef
4: idiv
5: invokespecial #3 // Method add:(I)I
8: ireturn
javac
interpreter1.
2.
Everybody Knows…
javac
jit
Bytecode
Java
Machine code
Java to Bytecode
private int add250(int value) {
return value + 250; // FF: 250 + 5
}


public int caffeinate(int value) {
return 0xCAFEBABE * add3(value);
}


public static void main(String[] args) {
WhatLiesBeneath wlb = new WhatLiesBeneath();
System.out.println(new Adder().doubleAdd3(5));
}
public int compute(int);
Code:
0: aload_0
1: iload_1
2: ldc #2 // int 0xdeadbeef
4: idiv
5: invokespecial #3 // Method add:(I)I
8: ireturn
javac
public int compute(int);
Code:
0: aload_0
1: iload_1
2: ldc #2 // int 0xdeadbeef
4: idiv
5: invokespecial #3 // Method add:(I)I
8: ireturn
Bytecode to (Templated) Assembler
idiv 108 idiv [0x00007f8f8f72ecc0, 0x00007f8f8f72ed00] 64 bytes
0x00007f8f8f72ecc0: mov (%rsp),%eax
0x00007f8f8f72ecc3: add $0x8,%rsp
0x00007f8f8f72ecc7: mov %eax,%ecx
0x00007f8f8f72ecc9: mov (%rsp),%eax
0x00007f8f8f72eccc: add $0x8,%rsp
0x00007f8f8f72ecd0: cmp $0x80000000,%eax
0x00007f8f8f72ecd6: jne 0x00007f8f8f72ece7
0x00007f8f8f72ecdc: xor %edx,%edx
0x00007f8f8f72ecde: cmp $0xffffffff,%ecx
0x00007f8f8f72ece1: je 0x00007f8f8f72ecea
0x00007f8f8f72ece7: cltd
0x00007f8f8f72ece8: idiv %ecx
0x00007f8f8f72ecea: movzbl 0x1(%r13),%ebx
0x00007f8f8f72ecef: inc %r13
0x00007f8f8f72ecf2: movabs $0x7f8fadbe4d80,%r10
0x00007f8f8f72ecfc: jmpq *(%r10,%rbx,8)
Let’s Benchmark!
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class ComputerBenchmark {
private Computer computer = new Computer();
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
@Benchmark
public int compute() {
return computer.compute(0xcafebabe);
}
}
JMH tells us...
REMEMBER: The numbers below are just data. To gain reusable insights, you need to
follow up on why the numbers are the way they are...
Do not assume the numbers tell you what you want them to tell.
Benchmark Mode Cnt Score Error Units
ComputerBenchmark.compute avgt 15 423.276 ± 22.782 ns/op
Improving Our Program…
private int add(int value) {
return value + 254;
}

public int compute(int value) {
return add(value / 0xdeadbeef);
}

public static void main(String[] args) {
System.out.println(new Computer().compute(0xcafebabe));
}
private int add(int value) {
return value + 254;
}

public int compute(int value) {
return (value / 0xdeadbeef) + 254;
}

public static void main(String[] args) {
System.out.println(new Computer().compute(0xcafebabe));
}
Improving Our Program…
2.
interpreter1.
Everybody Knows…
javac
jit
Bytecode
Java
Machine code
Without Inlining (-XX:-Inline)
ComputerBenchmark::compute
...
mov $0xcafebabe,%edx
callq 0x00007f0568fb3c60 ;*invokevirtual Computer.compute
...
Computer::compute
...
imul $0x7aeca299,%r10,%r10
sar $0x3c,%r10
mov %r10d,%r10d
sub %r10d,%edx ;*idiv
callq 0x00007f0568fb40e0 ;*invokevirtual Computer.add
...
Computer::add
...
add $0xfe,%edx
...
With Inlining
ComputerBenchmark::compute
...
mov $0xff,%eax
...
Now JMH tells us…
REMEMBER: The numbers below are just data. To gain reusable insights, you need to
follow up on why the numbers are the way they are...
Do not assume the numbers tell you what you want them to tell.
Benchmark Mode Cnt Score Error Units
ComputerBenchmark.compute (interpreted) avgt 15 423.276 ± 22.782 ns/op
ComputerBenchmark.compute (no inlining) avgt 15 11.831 ± 0.002 ns/op
ComputerBenchmark.compute (w/ inlining) avgt 15 6.307 ± 0.006 ns/op
Hotspot Optimisations
Everybody Knows…
javac
jit
Bytecode
Java
Machine code
os/hardware
Computers Are Simple!
A Pile of Cache…
Intel Core i7-3770K Ivy Bridge Processor
The Memory Hierarchy
Measuring Cache-Friendliness
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1024)
public void linkedList() {
if (iterator == null || !iterator.hasNext()) {
iterator = linkedList.iterator();
}
for (int i = 0; i < 1024 && iterator.hasNext(); i++) {
sink(iterator.next());
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1024)
public void primitiveArray() {
if (index == intArray.length) {
index = 0;
}
for (int i = 0; i < 1024 && index < intArray.length; i++) {
sink(intArray[index++]);
}
}
list length (K) 1 7 63 511
performance (ns/op) 7.25 9.03 20.87 29.07
CPI (clockticks/instrn) 0.32 0.41 0.93 1.33
events/operation
cycles 17.97 22.77 51.32 72.66
instructions 56.08 55.88 54.96 54.49
L1-dcache-load-misses 1.18 1.83 1.87 2.65
L1-dcache-loads 18.94 19.39 18.88 18.22
L1-dcache-stores 12.00 12.18 11.99 11.15
LLC-load-misses 0 0 0.41 1.31
LLC-loads 0 0.72 1.33 1.56
LinkedList
Primitive Array
list length (K) 1 7 63 511
performance (ns/op) 3.62 3.65 3.65 3.66
CPI 0.30 0.30 0.30 0.31
events/operation
cycles 9.09 9.16 9.10 9.13
instructions 30.24 30.13 29.94 29.85
L1-dcache-load-misses 0.00 0.01 0.06 0.06
L1-dcache-loads 12.00 12.00 11.97 12.14
L1-dcache-stores 6.00 6.02 6.02 6.04
LLC-load-misses 0.00 0.00 0.00 0.00
LLC-loads 0.00 0.00 0.00 0.00
Poor Unloved LinkedList…
Conclusions
From javac soup to hardware nuts, there’s a lot of advanced
technology here.
And as everyone knows, to the ignorant –
“Any sufficiently advanced
technology is indistinguishable
from magic.”
Don’t believe in magic!
Digging Deeper…
• https://groups.google.com/forum/#!forum/mechanical-sympathy
• http://openjdk.java.net/projects/code-tools/jmh/
• https://github.com/AdoptOpenJDK/jitwatch
• https://github.com/AdoptOpenJDK/jitwatch/wiki/Building-hsdis

Contenu connexe

Tendances

Обзор фреймворка Twisted
Обзор фреймворка TwistedОбзор фреймворка Twisted
Обзор фреймворка Twisted
Maxim Kulsha
 
Saving and retrieving_normal_modes_analysis_results
Saving and retrieving_normal_modes_analysis_resultsSaving and retrieving_normal_modes_analysis_results
Saving and retrieving_normal_modes_analysis_results
AltairKorea
 
ipython notebook poc memory forensics
ipython notebook poc memory forensicsipython notebook poc memory forensics
ipython notebook poc memory forensics
Vincent Ohprecio
 

Tendances (19)

Обзор фреймворка Twisted
Обзор фреймворка TwistedОбзор фреймворка Twisted
Обзор фреймворка Twisted
 
Saving and retrieving_normal_modes_analysis_results
Saving and retrieving_normal_modes_analysis_resultsSaving and retrieving_normal_modes_analysis_results
Saving and retrieving_normal_modes_analysis_results
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
JS Fest 2019 Node.js Antipatterns
JS Fest 2019 Node.js AntipatternsJS Fest 2019 Node.js Antipatterns
JS Fest 2019 Node.js Antipatterns
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in Django
 
Test innode
Test innodeTest innode
Test innode
 
Towards Reusable Components With Aspects [ICSE 2008]
Towards Reusable Components With Aspects [ICSE 2008]Towards Reusable Components With Aspects [ICSE 2008]
Towards Reusable Components With Aspects [ICSE 2008]
 
Maximizing SQL Reviews and Tuning with pt-query-digest
Maximizing SQL Reviews and Tuning with pt-query-digestMaximizing SQL Reviews and Tuning with pt-query-digest
Maximizing SQL Reviews and Tuning with pt-query-digest
 
ipython notebook poc memory forensics
ipython notebook poc memory forensicsipython notebook poc memory forensics
ipython notebook poc memory forensics
 
Matlab code for An overview of solar photovoltaic panel modeling based on ana...
Matlab code for An overview of solar photovoltaic panel modeling based on ana...Matlab code for An overview of solar photovoltaic panel modeling based on ana...
Matlab code for An overview of solar photovoltaic panel modeling based on ana...
 
Scrollytelling
ScrollytellingScrollytelling
Scrollytelling
 
Mutexes 2
Mutexes 2Mutexes 2
Mutexes 2
 
Ramirez entorno
Ramirez entornoRamirez entorno
Ramirez entorno
 
"Auth for React.js APP", Nikita Galkin
"Auth for React.js APP", Nikita Galkin"Auth for React.js APP", Nikita Galkin
"Auth for React.js APP", Nikita Galkin
 
Tests unitaires mock_kesako_20130516
Tests unitaires mock_kesako_20130516Tests unitaires mock_kesako_20130516
Tests unitaires mock_kesako_20130516
 
node.js Module Development
node.js Module Developmentnode.js Module Development
node.js Module Development
 

Similaire à What Lies Beneath

Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Carol McDonald
 
Back To The Future.Key 2
Back To The Future.Key 2Back To The Future.Key 2
Back To The Future.Key 2
gueste8cc560
 
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
Geir Høydalsvik
 

Similaire à What Lies Beneath (20)

Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to Debuggers
 
Internet of Things Magic Show
Internet of Things Magic ShowInternet of Things Magic Show
Internet of Things Magic Show
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf.NET 7 Performance Improvements_10_03_2023.pdf
.NET 7 Performance Improvements_10_03_2023.pdf
 
DEF CON 24 - Patrick Wardle - 99 problems little snitch
DEF CON 24 - Patrick Wardle - 99 problems little snitchDEF CON 24 - Patrick Wardle - 99 problems little snitch
DEF CON 24 - Patrick Wardle - 99 problems little snitch
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
Bsides
BsidesBsides
Bsides
 
Online test program generator for RISC-V processors
Online test program generator for RISC-V processorsOnline test program generator for RISC-V processors
Online test program generator for RISC-V processors
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
Back To The Future.Key 2
Back To The Future.Key 2Back To The Future.Key 2
Back To The Future.Key 2
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?
 
44CON London 2015 - Jtagsploitation: 5 wires, 5 ways to root
44CON London 2015 - Jtagsploitation: 5 wires, 5 ways to root44CON London 2015 - Jtagsploitation: 5 wires, 5 ways to root
44CON London 2015 - Jtagsploitation: 5 wires, 5 ways to root
 
CompilersAndLibraries
CompilersAndLibrariesCompilersAndLibraries
CompilersAndLibraries
 
node.js - Eventful JavaScript on the Server
node.js - Eventful JavaScript on the Servernode.js - Eventful JavaScript on the Server
node.js - Eventful JavaScript on the Server
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
Where the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-OptimisationsWhere the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-Optimisations
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020MySQL Goes to 8!  FOSDEM 2020 Database Track, January 2nd, 2020
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
 

Plus de Maurice Naftalin

Plus de Maurice Naftalin (9)

Lambda/Streams Hands-On Lab
Lambda/Streams Hands-On LabLambda/Streams Hands-On Lab
Lambda/Streams Hands-On Lab
 
Complicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine AgeComplicating Complexity: Performance in a New Machine Age
Complicating Complexity: Performance in a New Machine Age
 
Journey's end
Journey's endJourney's end
Journey's end
 
Shooting the Rapids
Shooting the RapidsShooting the Rapids
Shooting the Rapids
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
Shooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsShooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 Streams
 
Journey's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIJourney's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream API
 

Dernier

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

What Lies Beneath

  • 1. Oracle Code One, October 2018 Dmitry Vyazelenko @DVyazelenko Maurice Naftalin @mauricenaftalin What Lies Beneath
  • 2. Maurice Naftalin Java 5 Java 8 2013 2014 20152017
  • 3. Dmitry Vyazelenko • Disorganiser-in-chief of the unconferences JCrete and JAlba • Actually understands today’s topic
  • 4. What Lies Beneath • What, another ”Hello World” talk? • Yes! It’s about what happens when we type
 • It’s amazingly complicated — and interesting! java Hello.java
  • 6. Java to Bytecode private int add(int value) { return value + 254; }
 public int compute(int value) { return add(value / 0xdeadbeef); }
 public static void main(String[] args) { System.out.println(new Computer().compute(0xcafebabe)); } Result: 0xff
  • 7. private int add(int value) { return value + 254; }
 public int compute(int value) { return add(value / 0xdeadbeef); }
 public static void main(String[] args) { System.out.println(new Computer().compute(0xcafebabe)); } Java to Bytecode public int compute(int); Code: 0: aload_0 1: iload_1 2: ldc #2 // int 0xdeadbeef 4: idiv 5: invokespecial #3 // Method add:(I)I 8: ireturn javac
  • 9. Java to Bytecode private int add250(int value) { return value + 250; // FF: 250 + 5 } 
 public int caffeinate(int value) { return 0xCAFEBABE * add3(value); } 
 public static void main(String[] args) { WhatLiesBeneath wlb = new WhatLiesBeneath(); System.out.println(new Adder().doubleAdd3(5)); } public int compute(int); Code: 0: aload_0 1: iload_1 2: ldc #2 // int 0xdeadbeef 4: idiv 5: invokespecial #3 // Method add:(I)I 8: ireturn javac
  • 10. public int compute(int); Code: 0: aload_0 1: iload_1 2: ldc #2 // int 0xdeadbeef 4: idiv 5: invokespecial #3 // Method add:(I)I 8: ireturn Bytecode to (Templated) Assembler idiv 108 idiv [0x00007f8f8f72ecc0, 0x00007f8f8f72ed00] 64 bytes 0x00007f8f8f72ecc0: mov (%rsp),%eax 0x00007f8f8f72ecc3: add $0x8,%rsp 0x00007f8f8f72ecc7: mov %eax,%ecx 0x00007f8f8f72ecc9: mov (%rsp),%eax 0x00007f8f8f72eccc: add $0x8,%rsp 0x00007f8f8f72ecd0: cmp $0x80000000,%eax 0x00007f8f8f72ecd6: jne 0x00007f8f8f72ece7 0x00007f8f8f72ecdc: xor %edx,%edx 0x00007f8f8f72ecde: cmp $0xffffffff,%ecx 0x00007f8f8f72ece1: je 0x00007f8f8f72ecea 0x00007f8f8f72ece7: cltd 0x00007f8f8f72ece8: idiv %ecx 0x00007f8f8f72ecea: movzbl 0x1(%r13),%ebx 0x00007f8f8f72ecef: inc %r13 0x00007f8f8f72ecf2: movabs $0x7f8fadbe4d80,%r10 0x00007f8f8f72ecfc: jmpq *(%r10,%rbx,8)
  • 11. Let’s Benchmark! @Fork(3) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Benchmark) public class ComputerBenchmark { private Computer computer = new Computer(); @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Benchmark public int compute() { return computer.compute(0xcafebabe); } }
  • 12. JMH tells us... REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on why the numbers are the way they are... Do not assume the numbers tell you what you want them to tell. Benchmark Mode Cnt Score Error Units ComputerBenchmark.compute avgt 15 423.276 ± 22.782 ns/op
  • 13. Improving Our Program… private int add(int value) { return value + 254; }
 public int compute(int value) { return add(value / 0xdeadbeef); }
 public static void main(String[] args) { System.out.println(new Computer().compute(0xcafebabe)); }
  • 14. private int add(int value) { return value + 254; }
 public int compute(int value) { return (value / 0xdeadbeef) + 254; }
 public static void main(String[] args) { System.out.println(new Computer().compute(0xcafebabe)); } Improving Our Program…
  • 16. Without Inlining (-XX:-Inline) ComputerBenchmark::compute ... mov $0xcafebabe,%edx callq 0x00007f0568fb3c60 ;*invokevirtual Computer.compute ... Computer::compute ... imul $0x7aeca299,%r10,%r10 sar $0x3c,%r10 mov %r10d,%r10d sub %r10d,%edx ;*idiv callq 0x00007f0568fb40e0 ;*invokevirtual Computer.add ... Computer::add ... add $0xfe,%edx ...
  • 18. Now JMH tells us… REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on why the numbers are the way they are... Do not assume the numbers tell you what you want them to tell. Benchmark Mode Cnt Score Error Units ComputerBenchmark.compute (interpreted) avgt 15 423.276 ± 22.782 ns/op ComputerBenchmark.compute (no inlining) avgt 15 11.831 ± 0.002 ns/op ComputerBenchmark.compute (w/ inlining) avgt 15 6.307 ± 0.006 ns/op
  • 22. A Pile of Cache… Intel Core i7-3770K Ivy Bridge Processor
  • 24. Measuring Cache-Friendliness @Benchmark @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(1024) public void linkedList() { if (iterator == null || !iterator.hasNext()) { iterator = linkedList.iterator(); } for (int i = 0; i < 1024 && iterator.hasNext(); i++) { sink(iterator.next()); } } @Benchmark @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(1024) public void primitiveArray() { if (index == intArray.length) { index = 0; } for (int i = 0; i < 1024 && index < intArray.length; i++) { sink(intArray[index++]); } }
  • 25. list length (K) 1 7 63 511 performance (ns/op) 7.25 9.03 20.87 29.07 CPI (clockticks/instrn) 0.32 0.41 0.93 1.33 events/operation cycles 17.97 22.77 51.32 72.66 instructions 56.08 55.88 54.96 54.49 L1-dcache-load-misses 1.18 1.83 1.87 2.65 L1-dcache-loads 18.94 19.39 18.88 18.22 L1-dcache-stores 12.00 12.18 11.99 11.15 LLC-load-misses 0 0 0.41 1.31 LLC-loads 0 0.72 1.33 1.56 LinkedList
  • 26. Primitive Array list length (K) 1 7 63 511 performance (ns/op) 3.62 3.65 3.65 3.66 CPI 0.30 0.30 0.30 0.31 events/operation cycles 9.09 9.16 9.10 9.13 instructions 30.24 30.13 29.94 29.85 L1-dcache-load-misses 0.00 0.01 0.06 0.06 L1-dcache-loads 12.00 12.00 11.97 12.14 L1-dcache-stores 6.00 6.02 6.02 6.04 LLC-load-misses 0.00 0.00 0.00 0.00 LLC-loads 0.00 0.00 0.00 0.00
  • 28. Conclusions From javac soup to hardware nuts, there’s a lot of advanced technology here. And as everyone knows, to the ignorant – “Any sufficiently advanced technology is indistinguishable from magic.” Don’t believe in magic!
  • 29. Digging Deeper… • https://groups.google.com/forum/#!forum/mechanical-sympathy • http://openjdk.java.net/projects/code-tools/jmh/ • https://github.com/AdoptOpenJDK/jitwatch • https://github.com/AdoptOpenJDK/jitwatch/wiki/Building-hsdis