-
Enhancement
-
Resolution: Unresolved
-
P4
-
10
-
None
We need a way to build "heisenboxes", which are value boxes with value-based semantics.
Example API (using Unsafe as the carrier):
Class boxType = Integer.class, valType = int.class;
MethodHandle hFactory = Unsafe.findHeisenBoxConstructor(boxType);
assert hFactory.type().equals( methodType(boxType, boxType) );
MethodHandle boxer = identity(valType).asType(methodType(boxType, valType));
MethodHandle hBoxer = filterReturnValue(boxer, hFactory);
The boxes returned from a heisen-boxing function have suppressed equality semantics, allowing easier optimization (free from the need for escape analysis). In exchange, their identity semantics (as detected by op==/acmp and synchronization/monitorenter) are reduced, in a VM-dependent way.
Typical reduced semantics: Synch. attempts cause an exception (IMSE), and pointer comparisons are guaranteed to return false if the boxed values differ, with no other guarantees.
This feature will be experimental, so that we can try a range of reduced semantics.
Background:
Value types and primitives may be boxed as objects, where the object contents, not object identity, is the only relevant payload. Value-based classes also behave like this.
But as objects, the boxes are required to have identity semantics and may be tested for identity against equivalent boxes, synchronized on, and even mutated (with setAccessible(true)).
The JVM should discourage treating boxes as if they had identity. One way to do this is to document that object identity, for some objects and/or box types, is unstable. Another independent way is to cause identity-sensitive operations (synchronization, System.identityHashCode) to take special action, such as throwing an exception, when the operation is applied to an identity-agnostic box.
Example API (using Unsafe as the carrier):
Class boxType = Integer.class, valType = int.class;
MethodHandle hFactory = Unsafe.findHeisenBoxConstructor(boxType);
assert hFactory.type().equals( methodType(boxType, boxType) );
MethodHandle boxer = identity(valType).asType(methodType(boxType, valType));
MethodHandle hBoxer = filterReturnValue(boxer, hFactory);
The boxes returned from a heisen-boxing function have suppressed equality semantics, allowing easier optimization (free from the need for escape analysis). In exchange, their identity semantics (as detected by op==/acmp and synchronization/monitorenter) are reduced, in a VM-dependent way.
Typical reduced semantics: Synch. attempts cause an exception (IMSE), and pointer comparisons are guaranteed to return false if the boxed values differ, with no other guarantees.
This feature will be experimental, so that we can try a range of reduced semantics.
Background:
Value types and primitives may be boxed as objects, where the object contents, not object identity, is the only relevant payload. Value-based classes also behave like this.
But as objects, the boxes are required to have identity semantics and may be tested for identity against equivalent boxes, synchronized on, and even mutated (with setAccessible(true)).
The JVM should discourage treating boxes as if they had identity. One way to do this is to document that object identity, for some objects and/or box types, is unstable. Another independent way is to cause identity-sensitive operations (synchronization, System.identityHashCode) to take special action, such as throwing an exception, when the operation is applied to an identity-agnostic box.