This class represents a stack
AllocatorContext allocator context ;
(using thread-local pools or RTSJ ScopedMemory ).
Stacks allocations reduce heap memory allocation and often result in
faster execution time for almost all objects but the smallest one.
Stack allocated objects should never be assigned to static members
(see
ImmortalContext ). Also, methods entering/exiting stack
contexts should ensure that stack allocated objects do not escape from
their context scope. If necessary, stack objects can be exported using
StackContext.outerExecute or
StackContext.outerCopy :[code]
public class LargeInteger implements ValueType, Realtime {
public LargeInteger sqrt() {
StackContext.enter();
try {
LargeInteger result = ZERO;
LargeInteger k = this.shiftRight(this.bitLength() / 2)); // First approximation.
while (true) { // Newton Iteration.
result = (k.plus(this.divide(k))).shiftRight(1);
if (result.equals(k)) return StackContext.outerCopy(result); // Exports result.
k = result;
}
} finally {
StackContext.exit();
}
}
}[/code]
It should be noted that future versions of the JVM may provide some
limited support for stack allocation through escape analysis.
Users can always
StackContext.DISABLED turn-off stack allocation to
revert to standard heap allocation.
author: Jean-Marie Dautelle version: 5.2, August 19, 2007 |