Share
Embed
Exploring value objects from Project Valhalla
Balkrishna Rawool
November 16, 2026
Transcript
29 slides
-
Object headers • In JVM, each object is accompanied by
a header. • This header contains metadata such as identity and locking information. • If an object doesn’t need to store this info, the header is just an overhead. Header x y 8 bytes* 4 bytes 4 bytes 1 Point object à 16 bytes 1,000,000 Point objects à ~16 Megabytes ~8 MB of space is occupied by headers. 3 -
Pointer dereferencing • In JVM, each object-reference (pointer) is a
memory address. • When an object-reference is used, the corresponding object needs to be fetched from that location. This is called pointer dereferencing. • Looping over an array of object-references and using the values of those objects would require us to do pointer dereferencing for each array element. Point[ ] points[0] points[1] … points[n-1] 4 -
Caches in modern CPUs • Modern CPUs have L1, L2,
L3 caches. • These caches store chunks of memory (cache-lines). • They can boost performance, if objects being used in a process are co-located in memory. Point[ ] Core 1 Core 2 L1 cache L1 cache points[0] points[1] L2 cache … L2 cache points[n-1] L3 cache 5 -
Problems • Object headers • Space wasted with unnecessary object-headers
• Pointer dereferencing • Pointer dereferencing when reading object values • CPU Caches • Suboptimal use of CPU caches Solution Project Valhalla 6 -
Project Valhalla • Value objects • Objects that lack identity
and can be optimized for storing and processing • Null-restricted types • Null-restricted storage for optimized memory layout • Unifying primitive types and classes • Same generics rules for primitives and classes • Parametric VM • Optimized generic class- and method- parameterizations “Six PhDs tied into a single knot!” ---- James Gosling 7 -
Value objects Wasted object-header space ✅ Pointer dereferencing ✅ Suboptimal
use of CPU caches ✅ No Object-Oriented Design benefits Complexity/ Unreadability We need something that “codes like a class but works like an int” 10 -
Value objects • Previewed with JDK 28 (JEP-401) • Flattened
objects • Possibility to store objects without headers (only values) • Speed • Performance similar to primitives • Object Oriented Programming • Benefits of Object-Oriented Design/ class abstractions 11 -
Value classes • Value class instances are immutable and interchangeable
• Immutable: All fields of a value class are implicitly final. • Interchangeable: No need to distinguish between two value objects of same class with same values. • Inheritance • A value class is implicitly final. It can’t be extended by an identity or value class. • But it can be marked abstract. • Abstract value class can be extended by value or identity class. • An abstract value class can also be sealed. • Effectively, a value class can extend an abstract value class or java.lang.Object . • Records • Records are good candidates for being value types. 12 -
Identity classes vs value classes Identity Classes Value Classes Have
object identity Have no identity Two objects can be different even if all field values Objects are defined solely by their field values are equal == compares references (checks if same object) == compares values (checks if same state) Can be mutable Immutable Suitable for entities (e.g., Customer, Account) Suitable for data carriers (e.g., Money, Point, ComplexNumber) 14 -
(Value class) constructor • With JEP-513 (Flexible constructor bodies), the
constructor execution is divided into 3 phases: 1 2 3 Prologue Explicit constructor invocation Epilogue • Explicit constructor invocation • This phase is the call to superclass constructor ‘super()’ or another constructor ‘this()’. • Prologue • This phase is what happens before ‘super()’ or ‘this()’. • Also known as Early Construction Phase. • Only setting the member-fields is allowed. • Reading fields or direct/indirect call to ‘this’ are not allowed. • Epilogue • This phase is what happens after ‘super()’ or ‘this()’. • Also known as Late Construction Phase. For identity classes, if an explicit super() is not present it is added at the top of the constructor. For value classes, if an explicit super() is not present it is added at the bottom of the constructor. 18 -
JVM optimizations • JVM can apply optimizations to value objects
• by deciding to store values of the fields of an object instead of object-reference • Reference flattening • When this optimization is done for object-fields or array-elements • Reference scalarization • When this optimization is done for local variables or method parameters 20 -
Value classes in Java platform 30 classes from Java platform
are now value classes. Package Classes java.lang Integer, Long, Float, Double, Byte, Short, Character, Boolean, Number, Record java.util Optional, OptionalInt, OptionalLong, OptionalDouble java.time LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetTime, OffsetDateTime, Duration, Instant, Period, Year, YearMonth, MonthDay java.time.chrono MinguoDate, HijrahDate, JapaneseDate, ThaiBuddhistDate 21 -
Valhalla and nullability • Nullability • Nullability is the possibility
of an object-reference to be null. • Value classes are reference types. So, a value-object-reference can be null. • So, when a value-object is flattened, you need a null-indicator-bit. • On 64-bit hardware, it might even require 64 bits extra • For true reference flattening, we need Null-Restricted Types. • Null Restricted Types • Types whose instances are not allowed to be null. • Two proposals: • Indicated with a ‘!’. e.g. Point! p; • Primitive classes • A null-restricted field must be initialized before it is used. 24 -
Valhalla and generics • Generics • Generics give a possibility
to add type parameters to classes. • Type parameters can also be value classes. • Due to type-erasure JVM does not know the exact type and we wouldn’t be able to apply valueclass optimizations. • For example, List<Point> becomes List<Object> and Point objects in the list are not flattened. • Problem • Due to type-erasure, JVM is unable to fully apply value-class optimisations • Solution • One solution would be to retain the type information. • But that would break existing code and affecting backwards compatibility. • So, a new proposal is to follow a 2-step approach: 1. Universal generics 2. Specialized generics/ Parametric VM 27 -
Valhalla and generics • Universal generics • This will allow
all types (including primitives) to be used as generic type parameters. • This provides a unified language syntax, establishing the foundation for future layout optimizations. • Parametric VM • This will make JVM parametric, meaning JVM will create specialised classes depending on the type-parameter. • JVM can optimize specialized classes with value objects. • Other reference types behave exactly the way they did before, thus maintaining backwards compatibility. 28 -
Calculating π using Monte Carlo Simulation • Monte Carlo Simulations
• A broad class of computational algorithms based on repeated random sampling for obtaining numerical results • Calculating pi • Draw a square and inscribe a quadrant (circular section) within it • 𝐴𝑟𝑒𝑎 𝑜𝑓 𝑢𝑛𝑖𝑡 𝑠𝑞𝑢𝑎𝑟𝑒 = 1 • Area 𝑜𝑓 𝑞𝑢𝑎𝑑𝑟𝑎𝑛𝑡 = • /.01 23 4516.178 /.01 23 9451.0 = , + !"#$ &' ()"(*# + = , ∗ .! + = , + 30 -
Calculating π using Monte Carlo Simulation • Calculating pi •
!"#$ &' ()$*"$+, . = !"#$ &' -()$"# / • Draw large number of points inside the square • 𝑡 = 𝑇𝑜𝑡𝑎𝑙 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑜𝑖𝑛𝑡𝑠 𝑖𝑛𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑠𝑞𝑢𝑎𝑟𝑒 • 𝑛 = 𝑇𝑜𝑡𝑎𝑙 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑜𝑖𝑛𝑡𝑠 𝑖𝑛𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑞𝑢𝑎𝑑𝑟𝑎𝑛𝑡 + !"#$ &' ()$*"$+, . • ≈ = , • !"#$ &' -()$"# + 𝜋 ≈ , ∗4 / 31 -
Calculating π using Monte Carlo Simulation • Demo • •
• Perform this exercise with 100 million points (retain all Point objects in memory) Repeat the exercise twice: • With Point as identity class • Then with Point as value class Compare memory usage • Record using JFR (JDK Flight Recorder) • View it with VisualVM 32