Skip to content

Commit

Permalink
[Truffle] Add ArrayStrategy.isDefaultValue(value) as a cleaner way to…
Browse files Browse the repository at this point in the history
… check for default value.

* Fixes Graal issue assuming the comparison is always false.
* Allows to not fill an array with its default value when just allocated.
eregon committed May 25, 2016
1 parent ea839cc commit 68003c1
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -974,7 +974,7 @@ public DynamicObject initializeWithSizeAndValue(DynamicObject array, int size, O
@Cached("forValue(value)") ArrayStrategy strategy,
@Cached("createBinaryProfile()") ConditionProfile needsFill) {
final ArrayMirror store = strategy.newArray(size);
if (needsFill.profile(size > 0 && store.get(0) != value)) {
if (needsFill.profile(size > 0 && strategy.isDefaultValue(value))) {
for (int i = 0; i < size; i++) {
store.set(i, value);
}
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ public boolean specializesFor(Object value) {
throw unsupported();
}

public boolean isDefaultValue(Object value) {
throw unsupported();
}

public abstract boolean matches(DynamicObject array);

public abstract ArrayMirror newArray(int size);
@@ -126,6 +130,10 @@ public boolean specializesFor(Object value) {
return value instanceof Integer;
}

public boolean isDefaultValue(Object value) {
return (int) value == 0;
}

public boolean matches(DynamicObject array) {
return ArrayGuards.isIntArray(array);
}
@@ -176,6 +184,10 @@ public boolean specializesFor(Object value) {
return value instanceof Long;
}

public boolean isDefaultValue(Object value) {
return (long) value == 0L;
}

public boolean matches(DynamicObject array) {
return ArrayGuards.isLongArray(array);
}
@@ -214,6 +226,10 @@ public boolean specializesFor(Object value) {
return value instanceof Double;
}

public boolean isDefaultValue(Object value) {
return (double) value == 0.0;
}

public boolean matches(DynamicObject array) {
return ArrayGuards.isDoubleArray(array);
}
@@ -252,6 +268,10 @@ public boolean specializesFor(Object value) {
return !(value instanceof Integer) && !(value instanceof Long) && !(value instanceof Double);
}

public boolean isDefaultValue(Object value) {
return value == null;
}

public boolean matches(DynamicObject array) {
return ArrayGuards.isObjectArray(array);
}

0 comments on commit 68003c1

Please sign in to comment.