|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.context.Context
javolution.context.AllocatorContext
javolution.context.StackContext
public abstract class StackContext
This class represents a stack 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
outerExecute(java.lang.Runnable)
or outerCopy(T)
:
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();
}
}
}
It should be noted that future versions of the JVM may provide some
limited support for stack allocation through escape analysis.
Users can always turn-off
stack allocation to
revert to standard heap allocation.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javolution.context.AllocatorContext |
---|
AllocatorContext.Reference<T> |
Field Summary | |
---|---|
static Configurable<java.lang.Class<? extends StackContext>> |
DEFAULT
Holds the default implementation. |
static Configurable<java.lang.Boolean> |
DISABLED
Indicates if stack allocations are globally disabled. |
Fields inherited from class javolution.context.Context |
---|
ROOT |
Constructor Summary | |
---|---|
StackContext()
|
Method Summary | ||
---|---|---|
static StackContext |
enter()
Enters the DEFAULT stack context. |
|
static StackContext |
exit()
Exits the current stack context. |
|
boolean |
isDisabled()
Indicates if this stack context is disabled. |
|
static
|
outerCopy(T value)
Performs a copy of the specified value allocated outside of the current stack context. |
|
static void |
outerCopy(ValueType[] values)
Performs a copy of the specified values outside of the current stack context (convenience methode). |
|
static void |
outerExecute(java.lang.Runnable logic)
Executes the specified logic outside of the current stack context. |
|
void |
setDisabled(boolean isDisabled)
Enables/disables this stack context. |
Methods inherited from class javolution.context.AllocatorContext |
---|
deactivate, getAllocator, getCurrent, getDefault |
Methods inherited from class javolution.context.Context |
---|
enter, enter, enterAction, exit, exitAction, getOuter, getOwner, setCurrent, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Configurable<java.lang.Class<? extends StackContext>> DEFAULT
ScopedMemory
for their stack allocations.
public static final Configurable<java.lang.Boolean> DISABLED
Constructor Detail |
---|
public StackContext()
Method Detail |
---|
public static StackContext enter()
DEFAULT
stack context.
public static StackContext exit()
java.lang.ClassCastException
- if the context is not a stack context.public static <T extends ValueType> T outerCopy(T value)
value
- the value to be copied.
public static void outerCopy(ValueType[] values)
StackContext.outerExecute(new Runnable() {
public void run() {
for (int i = 0; i < values.length; i++) {
values[i] = {ValueType) values[i].copy();
}
}
});
values
- the array whose elements are exported.public static void outerExecute(java.lang.Runnable logic)
logic
- the logic to be executed outside of the current stack
context.public final boolean isDisabled()
AllocatorContext
.
public final void setDisabled(boolean isDisabled)
isDisabled
- true
if disabled; false
otherwise.
|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |