В этом докладе мы рассмотрим мотивацию создания и текущее состояние следующих Java проектов:
- Проект Valhalla для внедрения встроенных типов (Inline Types) в Java
- Проект Loom для реализации облегченных потоков (Lightweight Threads) в Java
- GraalVM - полиглотная виртуальная машина для Java. Мы также поговорим о том, как GraalVM делает приложения на основе Java более доступными для внедрения Serverless парадигмы.
JavaFest. Вадим Казулькин. Projects Valhalla, Loom and GraalVM
1. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
NEW PROFESSIONAL
JAVA EVENT
MARCH 21ST, 2020
KYIV, UKRAINE
Vadym Kazulkin
Projects Valhalla, Loom and GraalVM
2. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Contact
Vadym Kazulkin, ip.labs GmbH
v.kazulkin@gmail.com
https://www.linkedin.com/in/vadymkazulkin/
@VKazulkin
Co-Org Java User Group Bonn and Serverless Bonn Meetup
7. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Goal:
• Reboot the layout of data in memory
Source: Brian Goetz, Oracle „Evolving the Java Language” https://www.youtube.com/watch?v=A-mxj2vhVAA
8. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Motivation:
• Hardware has changed
• Multi-core
• The cost of cache misses has increased
Source: Brian Goetz, Oracle „Evolving the Java Language” https://www.youtube.com/watch?v=A-mxj2vhVAA
9. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Hardware Memory Model
Source: https://www.enterpriseai.news/2014/06/30/shared-memory-clusters-101/
10. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Motivation
Source: „Latency Numbers Every Programmer Should Know”
https://people.eecs.berkeley.edu/~rcs/research/interactive_latency.html
11. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Motivation
Source: „Latency Numbers Every Programmer Should Know”
https://people.eecs.berkeley.edu/~rcs/research/interactive_latency.html
12. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Storing objects in the Java Heap has its price, because storing
object’s metadata consumes additional memory for :
• flags facilitating synchronization/locking
• Identity and polymorphismus
• garbage collection
13. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types
Inline Type is an immutable type that is distinguishable only
by the state of its properties
14. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types
Immutable: an instance of an inline-type can’t change, once it’s been
created
Identity-less: inline-types of the same type with the same contents are
indistinguishable from each other
Flattenable: JVMs are allowed to flatten an inline-type inside of its
container
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
15. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Source: „What Is Project Valhalla?” https://dzone.com/articles/what-is-project-valhalla
16. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Benefits:
• Reduced memory usage
• Reduced indirection
• Increased locality
Codes like a class, works like a primitive (Brian
Goetz)
17. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Benefit: Reduced Memory Usage
No additional memory to store object metadata, such as flags
facilitating synchronization, identity, polymorphismus and
garbage collection
18. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Benefit: Reduced indirection
• Since objects are stored as reference types in Java, each time
an object is accessed it must first be dereferenced, causing
additional instructions to be executed
• The flattened data associated with inline types are immediately
present in the location in which they are needed and therefore,
require no dereferencing
19. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Benefit: Increased locality
• Flattened value objects remove indirection which increases the
likelihood that values are adjacently stored in memory–
especially for arrays or other contiguous memory structures
such as classes (i.e. if a class contains inline type fields)
• Consequently increases the chance of cache hits, because of
hardware prefetch of the cache lines
20. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types
inline class Point {long x, y ;}
21. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types
Can
• have method and field
• implement interfaces
• use encapsulation
• be generic
Can’t
• be mutated
• be sub-classed
• be cloned
• be Enums
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
22. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types Hierarchy
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
Object
Point?
Point
Nullable Inline Type (Point or null)
Inline Type
23. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Inline Types Hierarchy
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
Object
Point?
Point
Nullable Inline Type (Point or null)
Inline Type
Object[]
Point?[]
Point[]
24. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Current Status:
• Released public prototype LW2
• Can declare and use inline types (inline classes)
• No support for generics yet List<Point> // compilation error
• No support for specialized generics (Point<T>)
• No Support for migration of existing classes (like Optional)
• Memory Layout optimizations implemented
• Compiler/Virtual Machine optimizations implemented
• A lot of challenges to solve (read the article
https://www.infoq.com/news/2019/07/valhalla-openjdk-lw2-released/)
Source: Brian Goetz, Oracle „Valhalla Update” https://www.youtube.com/watch?v=1H4vmT-Va4o
25. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
Open Questions:
• Migration of existing classes (Option, LocaDateTime) to inline classes
• Nullity
• Equality
• GraalVM Support
• How Java type system should look like
Source: Brian Goetz, Oracle „Valhalla Update” https://www.youtube.com/watch?v=1H4vmT-Va4o
26. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Valhalla
How Java type system should look like ?
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
Object
ValObject
Inline
Types
RefObject
References
27. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Lightweight Thread and Continuations
Source: http://openjdk.java.net/projects/loom
28. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Lightweight Threads = Fibers
Lightweight Threads = Virtual Threads in the
newest prototype?
29. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Motivation:
Developers currently have 2 choices to write concurrent code:
• use blocking/synchronous API, which is simple, but less scalable (number of
threads, that OS supports is far less that open and concurrent connections
required)
• asynchronous API (Spring Project Reactor, RXJava 2), which is scalable, but
complex, harder to debug and profile and limited (no asynchronous JDBC
standard in this area)
Sources: Alan Bateman, Oracle „Project Loom: Fibers and Continuations for Java” https://www.youtube.com/watch?v=vbGbXUjlRyQ
30. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Goal:
To write simple and scalable code
31. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Lightweight Thread
Lightweight thread scheduled not by the OS, but by the Java Runtime
with low memory footprint and low task-switching cost
32. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Continuation
Continuation is a program object, representing a computation that
may be suspended and resumed
33. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Continuation
package java.lang;
public class Continuation {
public Continuation (ContinuationScope scope, Runnable target)
public final void run()
public static void yield (ContinuationScope scope)
public boolean isDone()
}
34. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Continuations
example () {
var scope = new ContinuationScope(„Example_Scope“);
var continuation = new Continuation (scope, () -> {
out.print(„1“);
Continuation.yield(scope);
out.print(„2“);
Continuation.yield(scope);
out.print(„3“);
Continuation.yield(scope);
});
while (! continuation.isDone()) {
out.print(„ run.. “);
continuation.run();
}
}
Output: run.. 1 run.. 2 run.. 3
35. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Lightweight Thread & Continuations
Thread
=
Continuation + Schedular
36. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Schedular
Schedular executes the task on a pool of carrier threads
• java.util.concurrent.Executor API exposes the Schedular
• Default schedular is a ForJoinPool
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
37. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Lightweight Thread Implementation
• Currently Thread and Lightweight Thread don’t have a common
supertype
• Thread.currentThread() in context of Lightweight Thread
• Creates adaptor (Shadow Thread)
• Adaptor emulates legacy Thread API (except deprecated methods like stop,
suspend and resume)
• Thread Local becomes Lightweight Thread Local
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
38. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Structured Concurrency
Basic idea: Everytime that the control splits into multiple concurrent paths, we
want to guarantee that they join up again
try (var scope= ThreadScope. open()) {
scope.schedule(task1);
scope.schedule(task2);
} //blocks until task1 and task2 terminate
Sources: Nathanial J. Smith „Notes on structured concurrency, or: Go statement considered harmful”
https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/
Roman Elizarov: “Structured concurrency with Coroutines in Kotlin” https://medium.com/@elizarov/structured-concurrency-722d765aa952
39. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Structured Concurrency
Cancelation:
• Each Lightweight Thread has cancel status which can only be set once, which sets
the interrupt status and unparks the Lightweight Thread
• The task can poll canceled status
try (var scope= ThreadScope. open(PROPAGATE_CANCEL)) {
scope.schedule(task1);
scope.schedule(task2);
} //canceling the lightweight thread executing this code will task1 and task2
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
40. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Lightweight Thread
Current Status:
• Lightweight Thread currently supports:
• scheduling
• parking/unparking
• waiting for a Lightweight Thread to terminate
• Lightweight Thread -friendly APIs
• java.util.concurrent Locks
• java.net.Socket/ServerSocket (since JDK 13)
• java.nio.channels.SocketChannel and Pipes (since JDK 11)
• Thread.sleep
• JSSE implementation of TLS
• AccessControl.doPrivileged (since JDK 12)
Source: Ron Pressler, Project Loom: Helping Write Concurrent Applications on the Java Platform
https://www.youtube.com/watch?v=lIq-x_iI-kc
41. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Current Status:
• Implemented initial prototype with Continuation and Lightweight
Thread support
• Current prototype of Continuations and Lightweight Thread can run
existing code
• Debugger Support
Current focus on:
• Performance improvement
• Stable Lightweight Thread API
• Java Flight Recorder support
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
42. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Limitations:
• Can‘t yield with native frames
Further Work:
• java.net.InetAddress
• Console I/O
• File I/O
43. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Loom
Open Questions:
• Should the existing Thread API be completely re-examined?
• Can all existing code be run on top of Lightweight Threads?
44. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Polyglot GraalVM
Source: http://openjdk.java.net/projects/metropolis
45. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Project Metropolis
Goals:
• Low footprint ahead-of-time mode for JVM-based languages
• High performance for all languages
• Convenient language interoperability and polyglot tooling
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger”
https://www.youtube.com/watch?v=ANN9rxYo5Hg
46. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
JEP 317
Experimental Java-Based JIT Compiler
Graal, a Java-based JIT compiler on the Linux/x64 platform, is the basis of the
experimental Ahead-of-Time (AOT) compiler introduced in JDK 9.
To Enable:
-XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler
48. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM
Architecture
Sources: Practical Partial Evaluation for High-Performance Dynamic Language Runtimes http://chrisseaton.com/rubytruffle/pldi17-
truffle/pldi17-truffle.pdf
„The LLVM Compiler Infrastructure“ https://llvm.org/
49. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM
Benchmarks
Sources: Renaissance Suite https://renaissance.dev/
50. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM
libgraal Library
Sources: „libgraal: GraalVM compiler as a precompiled GraalVM native image“
https://medium.com/graalvm/libgraal-graalvm-compiler-as-a-precompiled-graalvm-native-image-26e354bee5c
libgraal jargraal
• libgraal is a shared library, produced by GraalVM Native Image with a pre-compiled
version of the GraalVM compiler
• In Java applications on GraalVM libgraal used as the top tier Just-In-Time compiler
• libgraal improves startup
51. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM
Architecture
Sources: Practical Partial Evaluation for High-Performance Dynamic Language Runtimes http://chrisseaton.com/rubytruffle/pldi17-
truffle/pldi17-truffle.pdf
„The LLVM Compiler Infrastructure“ https://llvm.org/
52. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
SubstrateVM
Source: Oleg Šelajev, Thomas Wuerthinger, Oracle: “Deep dive into using GraalVM for Java and JavaScript”
https://www.youtube.com/watch?v=a-XEZobXspo
53. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM and SubstrateVM
Source: Oleg Selajev, Oracle : “Run Code in Any Language Anywhere with GraalVM” https://www.youtube.com/watch?v=JoDOo4FyYMU
54. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM on SubstrateVM
A game changer for Java & Serverless?
Cold Start :
Source: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go
55. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
AWS Lambda cold start time
by supported language
Source: Yan Cui: https://read.acloud.guru/does-coding-language-memory-or-package-size-affect-cold-starts-of-aws-lambda-a15e26d12c76
56. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM on SubstrateVM
A game changer for Java & Serverless?
Java Function compiled into a native executable using GraalVM on
SubstrateVM reduces
• “cold start” times
• memory footprint
by order of magnitude compared to running on JVM.
And both memory and execution time are cost dimension, when using
Serverless in the cloud
57. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM on SubstrateVM
A game changer for Java & Serverless?
Current challenges native executable using GraalVM :
• Most Cloud Providers (AWS) doesn’t provide GraalVM as Java Runtime
out of the box, only Open JDK (e.g. AWS provides Corretto)
• Some Cloud Providers (AWS) provide Custom Runtime Option
• Docker Image with GraalVM instead of Open JDK
• Frameworks like Micronaut and Quarkus which provide tooling for generating
Custom Runtime for cloud providers
58. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM Complitation Modes
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger”
https://www.youtube.com/watch?v=ANN9rxYo5Hg
59. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
AOT vs JIT
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger”
https://www.youtube.com/watch?v=ANN9rxYo5Hg
60. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
Support of GraalVM native images in Frameworks
• Spring Framework: working toward GraalVM native image support without
requiring additional configuration or workaround is one of the themes of
upcoming Spring Framework 5.3
• Spring Boot: Ongoing work on experimental Spring Graal Native project. Probably
ready for the 2.4 release
• Quarkus: a Kubernetes Native Java framework developed by Red Hat tailored for
GraalVM and HotSpot, crafted from best-of-breed Java libraries and standards.
• Micronaut: a modern, JVM-based, full-stack framework for building modular,
easily testable microservice and serverless applications.
Source: „GraalVM native image support“ https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
61. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
GraalVM Current State
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger”
https://www.youtube.com/watch?v=ANN9rxYo5Hg
62. NEW PROFESSIONAL JAVA EVENT KYIV, 2020
is still an interesting and great
programming language