SlideShare a Scribd company logo
1 of 31
Download to read offline
<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

Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
Nayden Gochev
 
JavaClassPresentation
JavaClassPresentationJavaClassPresentation
JavaClassPresentation
juliasceasor
 
Clojure A Dynamic Programming Language for the JVM
Clojure A Dynamic Programming Language for the JVMClojure A Dynamic Programming Language for the JVM
Clojure A Dynamic Programming Language for the JVM
elliando dias
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
Abdelmonaim Remani
 

What's hot (20)

Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
Real-world polyglot programming on the JVM  - Ben Summers (ONEIS)Real-world polyglot programming on the JVM  - Ben Summers (ONEIS)
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
SoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with SpringSoftwareUniversity seminar fast REST Api with Spring
SoftwareUniversity seminar fast REST Api with Spring
 
JavaClassPresentation
JavaClassPresentationJavaClassPresentation
JavaClassPresentation
 
Clojure A Dynamic Programming Language for the JVM
Clojure A Dynamic Programming Language for the JVMClojure A Dynamic Programming Language for the JVM
Clojure A Dynamic Programming Language for the JVM
 
Clojure talk at Münster JUG
Clojure talk at Münster JUGClojure talk at Münster JUG
Clojure talk at Münster JUG
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 
The State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila SzegediThe State of Managed Runtimes 2013, by Attila Szegedi
The State of Managed Runtimes 2013, by Attila Szegedi
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
 
Core Java
Core JavaCore Java
Core Java
 
Core java lessons
Core java lessonsCore java lessons
Core java lessons
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Core Java
Core JavaCore Java
Core Java
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
 

Similar to Java 8 selected updates

Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and Fallacies
Roman Elizarov
 

Similar to Java 8 selected updates (20)

Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
Java1 in mumbai
Java1 in mumbaiJava1 in mumbai
Java1 in mumbai
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
 
JS Essence
JS EssenceJS Essence
JS Essence
 
01 java intro
01 java intro01 java intro
01 java intro
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
New Java features: Simplified Design Patterns[LIT3826]
New Java features: Simplified Design Patterns[LIT3826]New Java features: Simplified Design Patterns[LIT3826]
New Java features: Simplified Design Patterns[LIT3826]
 
White and Black Magic on the JVM
White and Black Magic on the JVMWhite and Black Magic on the JVM
White and Black Magic on the JVM
 
Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)Polyglot and functional (Devoxx Nov/2011)
Polyglot and functional (Devoxx Nov/2011)
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
 
ITFT - Java Coding
ITFT - Java CodingITFT - Java Coding
ITFT - Java Coding
 
Java Programming concept
Java Programming concept Java Programming concept
Java Programming concept
 
Letest
LetestLetest
Letest
 
Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and Fallacies
 

More from Vinay H G (12)

Continuous integration using jenkins
Continuous integration using jenkinsContinuous integration using jenkins
Continuous integration using jenkins
 
Developers best practices_tutorial
Developers best practices_tutorialDevelopers best practices_tutorial
Developers best practices_tutorial
 
Javamagazine20140304 dl
Javamagazine20140304 dlJavamagazine20140304 dl
Javamagazine20140304 dl
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Why should i switch to Java SE 7
Why should i switch to Java SE 7Why should i switch to Java SE 7
Why should i switch to Java SE 7
 
Lambda Expressions
Lambda ExpressionsLambda Expressions
Lambda Expressions
 
Javase7 1641812
Javase7 1641812Javase7 1641812
Javase7 1641812
 
Tutorial storybook
Tutorial storybookTutorial storybook
Tutorial storybook
 
Virtual dev-day-java7-keynote-1641807
Virtual dev-day-java7-keynote-1641807Virtual dev-day-java7-keynote-1641807
Virtual dev-day-java7-keynote-1641807
 
Agile practice-2012
Agile practice-2012Agile practice-2012
Agile practice-2012
 
OAuth with Restful Web Services
OAuth with Restful Web Services OAuth with Restful Web Services
OAuth with Restful Web Services
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage Collection
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

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