Unboxed structs for Java

Sun, 06 Dec 2009 13:08:21 -0800
Java, JavaFX, draft

The cost of allocating and garbage-collecting heap objects is usually justified - but it is none-the-less often non-trivial. Furthermore, the concern about the cost (whether justified or not) causes programmers to write more convoluted code and design more complicated APIs. Some object-oriented languages (most notably C++ and C#) provide for struct types that may be stack-allocated and optimized in various ways. There has long been interest in such a mechanism for Java; here are some thoughts.

Examples

Co-ordinates, rectangles, and affine transforms in graphics programming.
Complex numbers.
Arbitrary-precision integer, as a pair of a 32-bit plus a pointer to a extenstions array. (The latter is non-null only when it is needed.)
Tagged immediate object types (as in unboxed fixnums), by using a pair of a int (or long) combined with a pointer.
A Swing Segment.
Document position and nodes (as in Swing or XML).
Iterators.
Design issues

A value type must be 'final'.

Mutable or immutable?

Equality and identity.

Boxing of structs.

Arrays of structs.

Backwards compatibility: Can we design the feature so it is "optional", in that older or more limited VMs can correctly execute bytecodes, that will be efficiently executed on supporting VMs?