Skip to content

Commit

Permalink
[GR-10852] Replace usages of Enum.values() to avoid allocations.
Browse files Browse the repository at this point in the history
PullRequest: graal/1830
  • Loading branch information
lukasstadler committed Jul 13, 2018
2 parents b540756 + 1d1bd75 commit aaa775c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ public enum ExceptionAction {
*/
ExitVM;

private static final ExceptionAction[] VALUES = values();

/**
* Gets the action that is one level less verbose than this action, bottoming out at the
* least verbose action.
*/
ExceptionAction quieter() {
assert ExceptionAction.Silent.ordinal() == 0;
int index = Math.max(ordinal() - 1, 0);
return values()[index];
return VALUES[index];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public enum NodeCycles {
CYCLES_512(512),
CYCLES_1024(1024);

private static final NodeCycles[] VALUES = values();

public final int value;

NodeCycles(int value) {
Expand All @@ -83,10 +85,9 @@ public static NodeCycles compute(NodeCycles base, int opCount) {
}
assert base.ordinal() > CYCLES_0.ordinal();
int log2 = log2(base.value * opCount);
NodeCycles[] values = values();
for (int i = base.ordinal(); i < values.length; i++) {
if (log2(values[i].value) == log2) {
return values[i];
for (int i = base.ordinal(); i < VALUES.length; i++) {
if (log2(VALUES[i].value) == log2) {
return VALUES[i];
}
}
return CYCLES_1024;
Expand All @@ -97,13 +98,12 @@ public static NodeCycles compute(int rawValue) {
if (rawValue == 0) {
return CYCLES_0;
}
NodeCycles[] values = values();
for (int i = CYCLES_0.ordinal(); i < values.length - 1; i++) {
if (values[i].value >= rawValue && rawValue <= values[i + 1].value) {
int r1 = values[i].value;
int r2 = values[i + 1].value;
for (int i = CYCLES_0.ordinal(); i < VALUES.length - 1; i++) {
if (VALUES[i].value >= rawValue && rawValue <= VALUES[i + 1].value) {
int r1 = VALUES[i].value;
int r2 = VALUES[i + 1].value;
int diff = r2 - r1;
return rawValue - r1 > diff / 2 ? values[i + 1] : values[i];
return rawValue - r1 > diff / 2 ? VALUES[i + 1] : VALUES[i];
}
}
return CYCLES_1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public enum NodeSize {
SIZE_512(512),
SIZE_1024(1024);

private static final NodeSize[] VALUES = values();

public final int value;

NodeSize(int value) {
Expand All @@ -80,10 +82,9 @@ public static NodeSize compute(NodeSize base, int opCount) {
}
assert base.ordinal() > SIZE_0.ordinal();
int log2 = log2(base.value * opCount);
NodeSize[] values = values();
for (int i = base.ordinal(); i < values.length; i++) {
if (log2(values[i].value) == log2) {
return values[i];
for (int i = base.ordinal(); i < VALUES.length; i++) {
if (log2(VALUES[i].value) == log2) {
return VALUES[i];
}
}
return SIZE_1024;
Expand All @@ -95,13 +96,12 @@ public static NodeSize compute(int rawValue) {
return SIZE_0;
}
assert rawValue > 0;
NodeSize[] values = values();
for (int i = SIZE_0.ordinal(); i < values.length - 1; i++) {
if (values[i].value >= rawValue && rawValue <= values[i + 1].value) {
int r1 = values[i].value;
int r2 = values[i + 1].value;
for (int i = SIZE_0.ordinal(); i < VALUES.length - 1; i++) {
if (VALUES[i].value >= rawValue && rawValue <= VALUES[i + 1].value) {
int r1 = VALUES[i].value;
int r2 = VALUES[i + 1].value;
int diff = r2 - r1;
return rawValue - r1 > diff / 2 ? values[i + 1] : values[i];
return rawValue - r1 > diff / 2 ? VALUES[i + 1] : VALUES[i];
}
}
return SIZE_1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public enum Kind {
*
* @since 1.0
*/
EXCEPTION,
EXCEPTION;

static final Kind[] VALUES = values();
}

private static final Breakpoint BUILDER_INSTANCE = new Breakpoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public List<Breakpoint> getBreakpoints() {
*/
@Deprecated
public void setBreakpointsActive(boolean active) {
for (Breakpoint.Kind kind : Breakpoint.Kind.values()) {
for (Breakpoint.Kind kind : Breakpoint.Kind.VALUES) {
setBreakpointsActive(kind, active);
}
}
Expand Down Expand Up @@ -642,7 +642,7 @@ public void setBreakpointsActive(Breakpoint.Kind breakpointKind, boolean active)
*/
@Deprecated
public boolean isBreakpointsActive() {
for (Breakpoint.Kind kind : Breakpoint.Kind.values()) {
for (Breakpoint.Kind kind : Breakpoint.Kind.VALUES) {
if (isBreakpointsActive(kind)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ private static void verifyTypeOrder(TypeSystemData typeSystem) {
}
}

private static final TypeKind[] TYPE_KIND_VALUES = TypeKind.values();

private boolean isPrimitiveWrapper(TypeMirror type) {
Types types = context.getEnvironment().getTypeUtils();
for (TypeKind kind : TypeKind.values()) {
for (TypeKind kind : TYPE_KIND_VALUES) {
if (!kind.isPrimitive()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ enum TypeTag {
FLOAT_ARRAY,
DOUBLE_ARRAY;

private static final TypeTag[] VALUES = values();

int encode(int offset) {
int encoded = (offset << 4) | (ordinal() & 0x0F);
assert getTag(encoded) == this && getOffset(encoded) == offset : "error encoding type tag, maybe offset is too big?";
return encoded;
}

static TypeTag getTag(int encoded) {
return values()[encoded & 0x0F];
return VALUES[encoded & 0x0F];
}

static int getOffset(int encoded) {
Expand Down

0 comments on commit aaa775c

Please sign in to comment.