SlideShare a Scribd company logo
<Insert Picture Here>




Java 8: Selected Updates
John Rose —Da Vinci Machine Project Lead & Java Nerd
April 2, 2012
http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012
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, and timing of any
features or functionality described for Oracle s
products remains at the sole discretion of Oracle.



                                                 The image part with
Warning: Dense Slides Ahead!

It’s all on http://cr.openjdk.java.net/~jrose/pres:


http://cr.openjdk.java.net/~jrose/pres/201204-LangNext.pdf




                                                             The image part with
What’s Brewing!
 http://openjdk.java.net/projects/jdk8/

•  Modules: Project Jigsaw
•  “Nashorn” JavaScript engine
   •  uses invokedynamic; strong Java integration
•  JVM convergence (JRockit + HotSpot)
   •  “permgen” removal, manageability hooks, optimizations
•  Project Lambda
   •  Better inner classes; defender methods (= no-state traits)
•  Technical debt: going into collections
   •  Lambda queries, fork/join integration, immutability, etc.
•  More: APIs, annotations, OS X, Java FX, etc., etc.

                                                                   The image part with
Jigsaw: A few big pieces
 http://mreinhold.org/blog/jigsaw-focus

•  Retire the classpath.
•  Explicit module versions and dependencies.
•  Consistent behaviors for compile, build, install, run.
   •  This means language, toolchain, and VM integration!
•  Encapsulation: Real privacy, module composition.
   •  A social network for code.
•  Optionality, providers, platforms.
   •  Pluggable impls., including platform-specific native code.
•  Stop torturing ClassLoaders.
•  Works with JARs, maven, OSGi, Debian, etc., etc. (!)

                                                                   The image part with
Nashorn: A rhino with an attitude
 http://wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdf


•  Clean rewrite on JVM of ECMAScript-262-5.
•  State-of-the-art map based data structures.
   •  Map normalization and reuse.
•  Builds inline caches with invokedynamic.
   •  I.e., mutable call sites with variable profiles and targets.
   •  Invokedynamic surfaces the recompilation magic.
•  Full use of Hotspot JVM GC and JIT.
•  Strong interoperability with Java.



                                                                     The image part with
A convergence of JVMs
 https://blogs.oracle.com/henrik/


•  JRockit and Hotspot today
•  And SE/ME/CDC tomorrow, using module system
•  Oracle JRockit and Hotspot teams have merged

•  Monitoring/manageability hooks ported to HS
•  Removing “permgen” using JR design (Java 7 and 8)
   •  Helps scale very broad or very dynamic systems.
•  Working on combined optimization algorithms
   •  Example: Escape analysis, flow insensitive + flow sensitive
   •  Inlining heuristics, including manually directed ones.

                                                               The image part with
Big λ Goal: parallel queries!
 http://blogs.oracle.com/briangoetz/resource/devoxx-lang-lib-vm-co-evol.pdf



•  Make parallel (collection) computations simple.
•  And similar in look & feel to serial computation.
•  (A familiar goal… Cf. LINQ.)

Key parts require lambdas:
•  Internal iterators, not classic Java external iterators
•  Chained queries, not side effects or accumulators.

people.filter(p -> p.age() >= 21)
  .sort(comparing(Person::getLastName));

                                                                       The image part with
What’s in a (Java-style) lambda?
 http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html


•  (Type) Params ‘->’ Body
   •  Examples: ()->42, x->x+1, (int x)->{foo(x);}
•  Type = target type from assignment, invocation, etc.
   •  Must be a functional interface type (e.g., Runnable)
   •  Typically inferred from context, could be an explicit cast.
•  Params = ‘(’ type var … ‘)’
   •  Elided types can be inferred from lambda target type
   •  Can elide parens if arity = 1
•  Body = expression | block
   •  In a block, ‘return’ keyword presents the result value


                                                                    The image part with
Lambdas in Java and C#

•  Minor syntax differences. (Arrow shaft, dot arity.)
•  Functional interfaces vs. sealed delegate types.
   •  A functional (“SAM type”) interface is a pattern, an old one.
•  Type inference, type matching differ. (Naturally.)
•  Captured outer variables must be constants in Java
•  Java 8 has no reification, no expression trees. Alas.
Similarities (besides basic lambda-ness)
•  Contextual typing; lambdas have no intrinsic type.
•  No branching to outer scopes.


                                                                 The image part with
Outer variable capture (more details)

•  Captured outer variables must be constants in Java.
   •  Same rule as for inner/nested classes.
•  This restriction was not (and is not) a mistake:
   •  for (i=0;i<4;i++) launch(()->doTask(i));
•  Can elide “final” under new “effectively final” rules.

•  Even for “safe” uses, mutable accumulators are bad.
   •  Accumulators are inherently serial. The future is functional!
   •  int a=0; es.forEach((e)->{a+=e.salary;});



                                                                The image part with
Outer variable capture (an alternative)




                                     The image part with
Method references

•  Unbound: String::length
•  Bound: "pre-"::concat
•  Constructor: StringBuffer::new

Comparable lambdas:
•  Unbound: (String s) -> s.length
•  Bound: (String t) -> "pre-".concat(t)
•  Constructor: () -> new StringBuffer()

C#, with delegate typing magic, has ¼ the dots!
                                                  The image part with
Lambda example: Query on collection

•  Functional type (aka “Single Abstract Method”):
   •  interface Predicate<T> {boolean apply(T t);}


•  Queryable type, with higher-order methods:
   •  Collection<T> filter(Predicate<T> p) { … }


•  The end user writes this:
   •  kids = people.filter(p -> p.age() < agelim);


•  The compiler infers λ-type Predicate<Integer>

                                                     The image part with
Fattening up the collection types

•  Higher-order methods are not found in List, etc.
•  New in Java 8: extension (“defender”) methods.
   •  interface List<T> … { …
        List<T> filter(Predicate<T> p)
          default { … }
        … }
•  Default method supplied to all implementations.
   •  As with abstract classes, subtypes can override.
   •  This shares algorithmic responsibility. (Not just sugar!)
•  Details are TBD. Stay tuned
   http://blogs.oracle.com/briangoetz

                                                                  The image part with
Translation options for lambdas

•  Could just translate to inner classes
    •  p -> p.age() < agelim translates to
     class Foo$1 implements Predicate<Person> {
         private final int v0;
         Foo$1(int $v0) { this.$v0 = v0 }
         public boolean apply(Person p) {
             return (p.age() < $v0);
         }
     }
•  Capture == invoke constructor (new Foo$1(agelim))
•  One class per lambda expression – yuck, JAR explosion
•  Would burden lambdas with identity
   •  Would like to improve performance over inner classes
•  Why copy yesterday’s mistakes?
                                                             The image part with
Translation options

•  Could translate directly to method handles
   •  Desugar lambda body to a static method
   •  Capture == take method reference + curry captured args
   •  Invocation == MethodHandle.invoke
•  Whatever translation we choose becomes not only
   implementation, but a binary specification
   •  Want to choose something that will be good forever
   •  Is the MH API ready to be a permanent binary specification?
   •  Are raw MHs yet performance-competitive with inner
      classes?



                                                               The image part with
Translation options

•  What about “inner classes now and method handles
   later”?
   •  But old class files would still have the inner class translation
   •  Java has never had “recompile to get better performance”
      before
•  Whatever we do now should be where we want to
   stay
   •  But the “old” technology is bad
   •  And the “new” technology isn’t proven yet
   •  What to do?



                                                                   The image part with
Invokedynamic to the rescue!

•  We can use invokedynamic to delay the translation
   strategy until runtime
   •  Invokedynamic was originally intended for dynamic
      languages, not statically typed languages like Java
   •  But why should the dynamic languages keep all the dynamic
      fun for themselves?
•  We can use invokedynamic to embed a recipe for
   constructing a lambda at the capture site
   •  At first capture, a translation strategy is chosen and the call
      site linked (the strategy is chosen by a metafactory)
   •  Subsequent captures bypass the slow path
   •  As a bonus, stateless lambdas translated to constant loads
                                                                  The image part with
Layers of cost for lambdas

•  Any translation scheme imposes phase costs:
   •  Linkage cost – one-time cost of setting up capture
   •  Capture cost – cost of creating a lambda
   •  Invocation cost – cost of invoking the lambda method
•  For inner class instances, these costs are
   •  Linkage: loading the class (Foo$1.class)
   •  Capture: invoking the constructor (new Foo$1(agelim))
   •  Invocation: invokeinterface (Predicate.apply)
•  The key phase to optimize is invocation
   •  Capture is important too, and must be inlinable.


                                                             The image part with
Layers of cost for lambdas (take two)

•    For invokedynamic, the phase costs are flexible:
•    Linkage: metafactory selects a local lambda factory
•    Capture: Invokes the local lambda factory.
•    Invocation: invokeinterface (as before)

•  The metafactory decides, once, how to spin each λ
      •  It can spin inner classes, and/or tightly couple to the JVM.
      •  The metafactory is named symbolically in the class file.
      •  Its behavior is totally decoupled from the bytecode shape.



                                                                    The image part with
Code generation strategy

•  All lambda bodies are desugared to static methods
   •  For “stateless” (non-capturing) lambdas, lambda signature
      matches SAM signature exactly
      (Person p) -> p.age() < 18
   •  Becomes (when translated to Predicate<String>)
      private static boolean lambda$1(Person p) {
        return p.age() < 18;
      }
•  In this case, the lambda instance λ0 can be created
   eagerly by the metafactory.
   •  The meta factory uses a K combinator, so that the linked
      semantics of the invokedynamic instruction becomes K(λ0).

                                                             The image part with
Code generation strategy

•  For lambdas that capture variables from the
   enclosing context, these are prepended to the
   argument list.
   •  So we can freely copy variables at point of capture
      (Person p) -> p.age() < agelim
   •  Becomes (when translated to Predicate<String>)
      private static boolean lambda$2(int agelim,
                                                  Person p) {
        return p.age() < agelim;
      }
•  Desugared (lifted) lambda$2 is a curried function.

                                                            The image part with
Code generation strategy

•  At point of lambda capture, compiler emits an
   invokedynamic call to the local lambda factory
   •  Bootstrap is metafactory (standard language runtime API)
   •  Static arguments identify properties of the lambda and SAM
   •  Call arguments are the captured values (if any)
   list.filter(p -> p.age() < agelim);
   becomes
   list.filter(indy[BSM=Lambda::metafactory,
      body=Foo::lambda$2,
      type=Predicate.class]( agelim ));
•  Static args encode properties of lambda and SAM
   •  Is lambda cacheable? Is SAM serializable?

                                                             The image part with
Benefits of invokedynamic

•  Invokedynamic is the ultimate lazy evaluation idiom
   •  For stateless lambdas that can be cached, they are
      initialized at first use and cached at the capture site
   •  Programmers frequently cache inner class instances (like
      Comparators) in static fields, but indy does this better
•  No overhead if lambda is never used
   •  No field, no static initializer
   •  Just some extra constant pool entries
•  SAM conversion strategy becomes a pure
   implementation detail
   •  Can be changed dynamically by changing metafactory

                                                             The image part with
What’s dynamic about invokedynamic?

•  Invokedynamic has user-defined linkage semantics.
   •  Defined by a per-instruction “bootstrap method” or BSM.
•  In the case of lambda, the BSM is the metafactory.
•  Invokedynamic linkage info is open-ended.
   •  BSM has up to 252 optional arguments from constant pool.
•  For lambda, BSM takes a couple extra BSM args.
   •  Method handle reference to desugared body.
   •  Class reference to target type (functional interface).
   •  Added in Java 8: Method handle constant cracking.
•  (Caveat: The BSM is hairier for serializables.)

                                                                The image part with
(That’s not very dynamic, is it?)

•  (Invokedynamic also provides mutable call sites.)
•  (But this feature is not used by Lambda.)

•  Used for JRuby (1.7), Nashorn, Smalltalk, etc.

•      Indy = linker macros + mutable call sites.

•  Linker macros can help with any language
   implementation plagued by small class file
   infestations.
                                                       The image part with
Invokedynamic odds & ends (Java 7)

For the record: Late developments from Java 7.
•  Bootstrap method takes any constant arguments.
•  Each invokedynamic instruction (potentially) has its
   own bootstrap method arguments.
•  Constant pool holds method handles, method types.
•  Method handles are fully competent with Java APIs.
   •  Including autoboxing & varargs conversions, when approp.
   •  Big exception: The types are erased.
   •  Small exception: “invokespecial <init>” not available.



                                                            The image part with
After 8 comes ∞

•  More stuff incubating in the Da Vinci Machine Project
•  Some possibilities:
   •  Tailcall, coroutines, continuations
   •  Extended arrays
   •  Primitive / reference unions
      http://hg.openjdk.java.net/mlvm/mlvm/hotspot/file/tip/tagu.txt
   •  Tuples, value types
      https://blogs.oracle.com/jrose/entry/value_types_in_the_vm
   •  Species, larvae, typestate, reification
      https://blogs.oracle.com/jrose/entry/
      larval_objects_in_the_vm


                                                                 The image part with
Other channels to tune in on…

•  Maxine project: Java as a system language.
   •  https://wikis.oracle.com/display/MaxineVM/Home
•  Graal project (OpenJDK): Self-hosting JIT.
   •  http://openjdk.java.net/projects/graal/


•  JVM Language Summit 2012
   •  July 30 – August 1; Oracle Santa Clara (same as last year)
   •  CFP coming in a few days




                                                             The image part with
http://openjdk.java.net/



∞   …




P.S. The Java/JVM team is hiring!
   https://blogs.oracle.com/jrose/entry/the_openjdk_group_at_oracle




                                                                      The image part with

More Related Content

What's hot (20)

PDF
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
jaxLondonConference
 
PPTX
Software Uni Conf October 2014
Nayden Gochev
 
PPTX
SoftwareUniversity seminar fast REST Api with Spring
Nayden Gochev
 
PPT
JavaClassPresentation
juliasceasor
 
PDF
Clojure A Dynamic Programming Language for the JVM
elliando dias
 
PDF
Clojure talk at Münster JUG
Alex Ott
 
PPSX
Introduction to Java
Hitesh-Java
 
PPTX
The Road to Lambda - Mike Duigou
jaxconf
 
PDF
The State of Managed Runtimes 2013, by Attila Szegedi
ZeroTurnaround
 
PPT
The Economies of Scaling Software
Abdelmonaim Remani
 
PDF
Core Java
Prakash Dimmita
 
PPSX
Core java lessons
vivek shah
 
PPTX
1 java programming- introduction
jyoti_lakhani
 
PPTX
Introduction to java
Steve Fort
 
PPTX
Core Java
Priyanka Pradhan
 
PDF
Java 8 features
Oleg Tsal-Tsalko
 
PDF
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
PPTX
Java Presentation
Amr Salah
 
ODP
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
PPTX
Introduction to JAVA
Mindsmapped Consulting
 
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
jaxLondonConference
 
Software Uni Conf October 2014
Nayden Gochev
 
SoftwareUniversity seminar fast REST Api with Spring
Nayden Gochev
 
JavaClassPresentation
juliasceasor
 
Clojure A Dynamic Programming Language for the JVM
elliando dias
 
Clojure talk at Münster JUG
Alex Ott
 
Introduction to Java
Hitesh-Java
 
The Road to Lambda - Mike Duigou
jaxconf
 
The State of Managed Runtimes 2013, by Attila Szegedi
ZeroTurnaround
 
The Economies of Scaling Software
Abdelmonaim Remani
 
Core Java
Prakash Dimmita
 
Core java lessons
vivek shah
 
1 java programming- introduction
jyoti_lakhani
 
Introduction to java
Steve Fort
 
Core Java
Priyanka Pradhan
 
Java 8 features
Oleg Tsal-Tsalko
 
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
Java Presentation
Amr Salah
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
Introduction to JAVA
Mindsmapped Consulting
 

Similar to Java 8 selected updates (20)

PDF
JSR 335 / java 8 - update reference
sandeepji_choudhary
 
PDF
Java 8 Lambda
François Sarradin
 
PDF
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
 
PDF
Java SE 8
Simon Ritter
 
PPTX
New Features in JDK 8
Martin Toshev
 
PDF
Java jdk-update-nov10-sde-v3m
Steve Elliott
 
PPTX
Improved Developer Productivity In JDK8
Simon Ritter
 
PDF
Java SE 8 & EE 7 Launch
Digicomp Academy AG
 
PDF
Java 8 Overview
Nicola Pedot
 
KEY
Java Closures
Ben Evans
 
PPTX
Getting ready to java 8
DataArt
 
PPTX
A brief tour of modern Java
Sina Madani
 
PPTX
Getting ready to java 8
Strannik_2013
 
PDF
FP in Java - Project Lambda and beyond
Mario Fusco
 
PPT
Java user group 2015 02-09-java8
Marc Tritschler
 
PPT
Java user group 2015 02-09-java8
marctritschler
 
PDF
AMIS Oracle OpenWorld 2013 Review Part 3 - Fusion Middleware
Getting value from IoT, Integration and Data Analytics
 
PPTX
Java 8 Feature Preview
Jim Bethancourt
 
PPTX
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
PPTX
New features in jdk8 iti
Ahmed mar3y
 
JSR 335 / java 8 - update reference
sandeepji_choudhary
 
Java 8 Lambda
François Sarradin
 
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
 
Java SE 8
Simon Ritter
 
New Features in JDK 8
Martin Toshev
 
Java jdk-update-nov10-sde-v3m
Steve Elliott
 
Improved Developer Productivity In JDK8
Simon Ritter
 
Java SE 8 & EE 7 Launch
Digicomp Academy AG
 
Java 8 Overview
Nicola Pedot
 
Java Closures
Ben Evans
 
Getting ready to java 8
DataArt
 
A brief tour of modern Java
Sina Madani
 
Getting ready to java 8
Strannik_2013
 
FP in Java - Project Lambda and beyond
Mario Fusco
 
Java user group 2015 02-09-java8
Marc Tritschler
 
Java user group 2015 02-09-java8
marctritschler
 
AMIS Oracle OpenWorld 2013 Review Part 3 - Fusion Middleware
Getting value from IoT, Integration and Data Analytics
 
Java 8 Feature Preview
Jim Bethancourt
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
New features in jdk8 iti
Ahmed mar3y
 
Ad

More from Vinay H G (12)

PPTX
Continuous integration using jenkins
Vinay H G
 
PDF
Developers best practices_tutorial
Vinay H G
 
PDF
Javamagazine20140304 dl
Vinay H G
 
PDF
Hibernate tutorial
Vinay H G
 
PDF
Why should i switch to Java SE 7
Vinay H G
 
PDF
Lambda Expressions
Vinay H G
 
PDF
Javase7 1641812
Vinay H G
 
PDF
Tutorial storybook
Vinay H G
 
PDF
Virtual dev-day-java7-keynote-1641807
Vinay H G
 
PDF
Agile practice-2012
Vinay H G
 
PDF
OAuth with Restful Web Services
Vinay H G
 
PDF
Java Garbage Collection
Vinay H G
 
Continuous integration using jenkins
Vinay H G
 
Developers best practices_tutorial
Vinay H G
 
Javamagazine20140304 dl
Vinay H G
 
Hibernate tutorial
Vinay H G
 
Why should i switch to Java SE 7
Vinay H G
 
Lambda Expressions
Vinay H G
 
Javase7 1641812
Vinay H G
 
Tutorial storybook
Vinay H G
 
Virtual dev-day-java7-keynote-1641807
Vinay H G
 
Agile practice-2012
Vinay H G
 
OAuth with Restful Web Services
Vinay H G
 
Java Garbage Collection
Vinay H G
 
Ad

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 

Java 8 selected updates

  • 1. <Insert Picture Here> Java 8: Selected Updates John Rose —Da Vinci Machine Project Lead & Java Nerd April 2, 2012 http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012
  • 2. 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, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle. The image part with
  • 3. Warning: Dense Slides Ahead! It’s all on http://cr.openjdk.java.net/~jrose/pres: http://cr.openjdk.java.net/~jrose/pres/201204-LangNext.pdf The image part with
  • 4. What’s Brewing! http://openjdk.java.net/projects/jdk8/ •  Modules: Project Jigsaw •  “Nashorn” JavaScript engine •  uses invokedynamic; strong Java integration •  JVM convergence (JRockit + HotSpot) •  “permgen” removal, manageability hooks, optimizations •  Project Lambda •  Better inner classes; defender methods (= no-state traits) •  Technical debt: going into collections •  Lambda queries, fork/join integration, immutability, etc. •  More: APIs, annotations, OS X, Java FX, etc., etc. The image part with
  • 5. Jigsaw: A few big pieces http://mreinhold.org/blog/jigsaw-focus •  Retire the classpath. •  Explicit module versions and dependencies. •  Consistent behaviors for compile, build, install, run. •  This means language, toolchain, and VM integration! •  Encapsulation: Real privacy, module composition. •  A social network for code. •  Optionality, providers, platforms. •  Pluggable impls., including platform-specific native code. •  Stop torturing ClassLoaders. •  Works with JARs, maven, OSGi, Debian, etc., etc. (!) The image part with
  • 6. Nashorn: A rhino with an attitude http://wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdf •  Clean rewrite on JVM of ECMAScript-262-5. •  State-of-the-art map based data structures. •  Map normalization and reuse. •  Builds inline caches with invokedynamic. •  I.e., mutable call sites with variable profiles and targets. •  Invokedynamic surfaces the recompilation magic. •  Full use of Hotspot JVM GC and JIT. •  Strong interoperability with Java. The image part with
  • 7. A convergence of JVMs https://blogs.oracle.com/henrik/ •  JRockit and Hotspot today •  And SE/ME/CDC tomorrow, using module system •  Oracle JRockit and Hotspot teams have merged •  Monitoring/manageability hooks ported to HS •  Removing “permgen” using JR design (Java 7 and 8) •  Helps scale very broad or very dynamic systems. •  Working on combined optimization algorithms •  Example: Escape analysis, flow insensitive + flow sensitive •  Inlining heuristics, including manually directed ones. The image part with
  • 8. Big λ Goal: parallel queries! http://blogs.oracle.com/briangoetz/resource/devoxx-lang-lib-vm-co-evol.pdf •  Make parallel (collection) computations simple. •  And similar in look & feel to serial computation. •  (A familiar goal… Cf. LINQ.) Key parts require lambdas: •  Internal iterators, not classic Java external iterators •  Chained queries, not side effects or accumulators. people.filter(p -> p.age() >= 21) .sort(comparing(Person::getLastName)); The image part with
  • 9. What’s in a (Java-style) lambda? http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html •  (Type) Params ‘->’ Body •  Examples: ()->42, x->x+1, (int x)->{foo(x);} •  Type = target type from assignment, invocation, etc. •  Must be a functional interface type (e.g., Runnable) •  Typically inferred from context, could be an explicit cast. •  Params = ‘(’ type var … ‘)’ •  Elided types can be inferred from lambda target type •  Can elide parens if arity = 1 •  Body = expression | block •  In a block, ‘return’ keyword presents the result value The image part with
  • 10. Lambdas in Java and C# •  Minor syntax differences. (Arrow shaft, dot arity.) •  Functional interfaces vs. sealed delegate types. •  A functional (“SAM type”) interface is a pattern, an old one. •  Type inference, type matching differ. (Naturally.) •  Captured outer variables must be constants in Java •  Java 8 has no reification, no expression trees. Alas. Similarities (besides basic lambda-ness) •  Contextual typing; lambdas have no intrinsic type. •  No branching to outer scopes. The image part with
  • 11. Outer variable capture (more details) •  Captured outer variables must be constants in Java. •  Same rule as for inner/nested classes. •  This restriction was not (and is not) a mistake: •  for (i=0;i<4;i++) launch(()->doTask(i)); •  Can elide “final” under new “effectively final” rules. •  Even for “safe” uses, mutable accumulators are bad. •  Accumulators are inherently serial. The future is functional! •  int a=0; es.forEach((e)->{a+=e.salary;}); The image part with
  • 12. Outer variable capture (an alternative) The image part with
  • 13. Method references •  Unbound: String::length •  Bound: "pre-"::concat •  Constructor: StringBuffer::new Comparable lambdas: •  Unbound: (String s) -> s.length •  Bound: (String t) -> "pre-".concat(t) •  Constructor: () -> new StringBuffer() C#, with delegate typing magic, has ¼ the dots! The image part with
  • 14. Lambda example: Query on collection •  Functional type (aka “Single Abstract Method”): •  interface Predicate<T> {boolean apply(T t);} •  Queryable type, with higher-order methods: •  Collection<T> filter(Predicate<T> p) { … } •  The end user writes this: •  kids = people.filter(p -> p.age() < agelim); •  The compiler infers λ-type Predicate<Integer> The image part with
  • 15. Fattening up the collection types •  Higher-order methods are not found in List, etc. •  New in Java 8: extension (“defender”) methods. •  interface List<T> … { … List<T> filter(Predicate<T> p) default { … } … } •  Default method supplied to all implementations. •  As with abstract classes, subtypes can override. •  This shares algorithmic responsibility. (Not just sugar!) •  Details are TBD. Stay tuned http://blogs.oracle.com/briangoetz The image part with
  • 16. Translation options for lambdas •  Could just translate to inner classes •  p -> p.age() < agelim translates to class Foo$1 implements Predicate<Person> { private final int v0; Foo$1(int $v0) { this.$v0 = v0 } public boolean apply(Person p) { return (p.age() < $v0); } } •  Capture == invoke constructor (new Foo$1(agelim)) •  One class per lambda expression – yuck, JAR explosion •  Would burden lambdas with identity •  Would like to improve performance over inner classes •  Why copy yesterday’s mistakes? The image part with
  • 17. Translation options •  Could translate directly to method handles •  Desugar lambda body to a static method •  Capture == take method reference + curry captured args •  Invocation == MethodHandle.invoke •  Whatever translation we choose becomes not only implementation, but a binary specification •  Want to choose something that will be good forever •  Is the MH API ready to be a permanent binary specification? •  Are raw MHs yet performance-competitive with inner classes? The image part with
  • 18. Translation options •  What about “inner classes now and method handles later”? •  But old class files would still have the inner class translation •  Java has never had “recompile to get better performance” before •  Whatever we do now should be where we want to stay •  But the “old” technology is bad •  And the “new” technology isn’t proven yet •  What to do? The image part with
  • 19. Invokedynamic to the rescue! •  We can use invokedynamic to delay the translation strategy until runtime •  Invokedynamic was originally intended for dynamic languages, not statically typed languages like Java •  But why should the dynamic languages keep all the dynamic fun for themselves? •  We can use invokedynamic to embed a recipe for constructing a lambda at the capture site •  At first capture, a translation strategy is chosen and the call site linked (the strategy is chosen by a metafactory) •  Subsequent captures bypass the slow path •  As a bonus, stateless lambdas translated to constant loads The image part with
  • 20. Layers of cost for lambdas •  Any translation scheme imposes phase costs: •  Linkage cost – one-time cost of setting up capture •  Capture cost – cost of creating a lambda •  Invocation cost – cost of invoking the lambda method •  For inner class instances, these costs are •  Linkage: loading the class (Foo$1.class) •  Capture: invoking the constructor (new Foo$1(agelim)) •  Invocation: invokeinterface (Predicate.apply) •  The key phase to optimize is invocation •  Capture is important too, and must be inlinable. The image part with
  • 21. Layers of cost for lambdas (take two) •  For invokedynamic, the phase costs are flexible: •  Linkage: metafactory selects a local lambda factory •  Capture: Invokes the local lambda factory. •  Invocation: invokeinterface (as before) •  The metafactory decides, once, how to spin each λ •  It can spin inner classes, and/or tightly couple to the JVM. •  The metafactory is named symbolically in the class file. •  Its behavior is totally decoupled from the bytecode shape. The image part with
  • 22. Code generation strategy •  All lambda bodies are desugared to static methods •  For “stateless” (non-capturing) lambdas, lambda signature matches SAM signature exactly (Person p) -> p.age() < 18 •  Becomes (when translated to Predicate<String>) private static boolean lambda$1(Person p) { return p.age() < 18; } •  In this case, the lambda instance λ0 can be created eagerly by the metafactory. •  The meta factory uses a K combinator, so that the linked semantics of the invokedynamic instruction becomes K(λ0). The image part with
  • 23. Code generation strategy •  For lambdas that capture variables from the enclosing context, these are prepended to the argument list. •  So we can freely copy variables at point of capture (Person p) -> p.age() < agelim •  Becomes (when translated to Predicate<String>) private static boolean lambda$2(int agelim, Person p) { return p.age() < agelim; } •  Desugared (lifted) lambda$2 is a curried function. The image part with
  • 24. Code generation strategy •  At point of lambda capture, compiler emits an invokedynamic call to the local lambda factory •  Bootstrap is metafactory (standard language runtime API) •  Static arguments identify properties of the lambda and SAM •  Call arguments are the captured values (if any) list.filter(p -> p.age() < agelim); becomes list.filter(indy[BSM=Lambda::metafactory, body=Foo::lambda$2, type=Predicate.class]( agelim )); •  Static args encode properties of lambda and SAM •  Is lambda cacheable? Is SAM serializable? The image part with
  • 25. Benefits of invokedynamic •  Invokedynamic is the ultimate lazy evaluation idiom •  For stateless lambdas that can be cached, they are initialized at first use and cached at the capture site •  Programmers frequently cache inner class instances (like Comparators) in static fields, but indy does this better •  No overhead if lambda is never used •  No field, no static initializer •  Just some extra constant pool entries •  SAM conversion strategy becomes a pure implementation detail •  Can be changed dynamically by changing metafactory The image part with
  • 26. What’s dynamic about invokedynamic? •  Invokedynamic has user-defined linkage semantics. •  Defined by a per-instruction “bootstrap method” or BSM. •  In the case of lambda, the BSM is the metafactory. •  Invokedynamic linkage info is open-ended. •  BSM has up to 252 optional arguments from constant pool. •  For lambda, BSM takes a couple extra BSM args. •  Method handle reference to desugared body. •  Class reference to target type (functional interface). •  Added in Java 8: Method handle constant cracking. •  (Caveat: The BSM is hairier for serializables.) The image part with
  • 27. (That’s not very dynamic, is it?) •  (Invokedynamic also provides mutable call sites.) •  (But this feature is not used by Lambda.) •  Used for JRuby (1.7), Nashorn, Smalltalk, etc. •  Indy = linker macros + mutable call sites. •  Linker macros can help with any language implementation plagued by small class file infestations. The image part with
  • 28. Invokedynamic odds & ends (Java 7) For the record: Late developments from Java 7. •  Bootstrap method takes any constant arguments. •  Each invokedynamic instruction (potentially) has its own bootstrap method arguments. •  Constant pool holds method handles, method types. •  Method handles are fully competent with Java APIs. •  Including autoboxing & varargs conversions, when approp. •  Big exception: The types are erased. •  Small exception: “invokespecial <init>” not available. The image part with
  • 29. After 8 comes ∞ •  More stuff incubating in the Da Vinci Machine Project •  Some possibilities: •  Tailcall, coroutines, continuations •  Extended arrays •  Primitive / reference unions http://hg.openjdk.java.net/mlvm/mlvm/hotspot/file/tip/tagu.txt •  Tuples, value types https://blogs.oracle.com/jrose/entry/value_types_in_the_vm •  Species, larvae, typestate, reification https://blogs.oracle.com/jrose/entry/ larval_objects_in_the_vm The image part with
  • 30. Other channels to tune in on… •  Maxine project: Java as a system language. •  https://wikis.oracle.com/display/MaxineVM/Home •  Graal project (OpenJDK): Self-hosting JIT. •  http://openjdk.java.net/projects/graal/ •  JVM Language Summit 2012 •  July 30 – August 1; Oracle Santa Clara (same as last year) •  CFP coming in a few days The image part with
  • 31. http://openjdk.java.net/ ∞ … P.S. The Java/JVM team is hiring! https://blogs.oracle.com/jrose/entry/the_openjdk_group_at_oracle The image part with