Skip navigation links

Package org.ObjectLayout

ObjectLayout: An optimised memory layout package.

See: Description

Package org.ObjectLayout Description

ObjectLayout: An optimised memory layout package.

The ObjectLayout package provides some key data structure classes designed with optimised memory layout in mind. These classes are aimed at matching the natural speed benefits similar data structure constructs enable in most C-style languages, while maintaining an idiomatic Java feel and a natural fit with existing code and libraries.

The package classes provide full functionality on all JVMs (of Java SE 5 or above) at reasonable layouts and execution speeds. However, the classes are carefully designed with semantics that would allow an optimised JVM to implement them with improved memory layout not directly expressible in Java.

The StructuredArray class is a good example of this design pattern. StructuredArray is carefully designed to allow a JVM to store it in memory with a layout similar to an array of structs in C-like languages. When a JVM optimises StructuredArray in such a way, array access benefits from both direct (as opposed to de-referenced) dead-reckoning index access, as well as from fixed memory strides during streaming operations.

The three commonly used C-style container layout forms that ObjectLayout seeks to enable in Java are:

The speed benefits in these three forms of layout derive from two dominant benefits:

  1. 1. Dead reckoning: In all three forms, the address of the target data field accessed through the containing object can be directly derived from the containing object reference without a data-dependent load operation (no de-referencing or equivalent operation needed).
  2. 2. Streaming: In the case of an array of structs, sequential access through multiple members of the containing array result in predictable striding access in memory, enabling prefetch logic (hardware assisted or otherwise) to compensate for much of the latency involved in cache misses.

The matching ObjectLayout forms

StructuredArray provides an idiomatic Java collection form with speed (and semantics) similar to an "array of structs" form, supporting any constructable java Object as an array member.

@Intrinsic provides an idiomatic Java means for declaring member objects that are intrinsic to the instances of the class they are declared in. @Intrinsic provides a "struct in struct" equivalent relationship between Java objects, exposing the speed and layout benefits similar to the same form in the C family languages.

Sub-classable Primitive and Reference array classes (e.g. PrimitiveLongArray and ReferenceArray) provide an idiomatic Java means of declaring constructs with speed (and semantics) similar to "struct with array at the end". They do so by supporting the subclassing of arrays of the various primitive and reference forms possible in Java. StructuredArray similarly supports this capability (via subclassing) for the equivalent of "struct with array of structs at the end".

Skip navigation links