Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0238297dcaa7
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2972e42e39a7
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Apr 30, 2016

  1. [Truffle] Use ArrayStrategy in Array#zip.

    * Allow ArrayStrategy.of() to be called on non-Array.
    eregon committed Apr 30, 2016
    Copy the full SHA
    fcc6642 View commit details
  2. Copy the full SHA
    2972e42 View commit details
Showing with 30 additions and 52 deletions.
  1. +20 −48 truffle/src/main/java/org/jruby/truffle/core/array/ArrayNodes.java
  2. +10 −4 truffle/src/main/java/org/jruby/truffle/core/array/ArrayStrategy.java
68 changes: 20 additions & 48 deletions truffle/src/main/java/org/jruby/truffle/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
@@ -2122,60 +2122,32 @@ public abstract static class ZipNode extends ArrayCoreMethodNode {

@Child private CallDispatchHeadNode zipInternalCall;

@Specialization(guards = { "isObjectArray(array)", "isRubyArray(other)", "isIntArray(other)", "others.length == 0" })
@Specialization(guards = {
"isRubyArray(other)", "aStrategy.matches(array)", "bStrategy.matches(other)", "others.length == 0"
}, limit = "ARRAY_STRATEGIES")
public DynamicObject zipObjectIntegerFixnum(DynamicObject array, DynamicObject other, Object[] others, NotProvided block,
@Cached("createBinaryProfile()") ConditionProfile sameLengthProfile) {
final Object[] a = (Object[]) getStore(array);

final int[] b = (int[]) getStore(other);
final int bLength = getSize(other);

final int zippedLength = getSize(array);
final Object[] zipped = new Object[zippedLength];

if (sameLengthProfile.profile(zippedLength == bLength)) {
for (int n = 0; n < zippedLength; n++) {
zipped[n] = createArray(getContext(), new Object[] { a[n], b[n] }, 2);
}
} else {
for (int n = 0; n < zippedLength; n++) {
if (n < bLength) {
zipped[n] = createArray(getContext(), new Object[] { a[n], b[n] }, 2);
} else {
zipped[n] = createArray(getContext(), new Object[] { a[n], nil() }, 2);
}
}
}

return createArray(getContext(), zipped, zippedLength);
}

@Specialization(guards = { "isObjectArray(array)", "isRubyArray(other)", "isObjectArray(other)", "others.length == 0" })
public DynamicObject zipObjectObject(DynamicObject array, DynamicObject other, Object[] others, NotProvided block,
@Cached("createBinaryProfile()") ConditionProfile sameLengthProfile) {
final Object[] a = (Object[]) getStore(array);

final Object[] b = (Object[]) getStore(other);
final int bLength = getSize(other);

@Cached("of(array)") ArrayStrategy aStrategy,
@Cached("of(other)") ArrayStrategy bStrategy,
@Cached("aStrategy.generalize(bStrategy)") ArrayStrategy generalized,
@Cached("createBinaryProfile()") ConditionProfile bNotSmallerProfile) {
final ArrayMirror a = aStrategy.newMirror(array);
final ArrayMirror b = bStrategy.newMirror(other);

final int bSize = getSize(other);
final int zippedLength = getSize(array);
final Object[] zipped = new Object[zippedLength];

if (sameLengthProfile.profile(zippedLength == bLength)) {
for (int n = 0; n < zippedLength; n++) {
zipped[n] = createArray(getContext(), new Object[] { a[n], b[n] }, 2);
}
} else {
for (int n = 0; n < zippedLength; n++) {
if (n < bLength) {
zipped[n] = createArray(getContext(), new Object[] { a[n], b[n] }, 2);
} else {
zipped[n] = createArray(getContext(), new Object[] { a[n], nil() }, 2);
}
for (int n = 0; n < zippedLength; n++) {
if (bNotSmallerProfile.profile(n < bSize)) {
final ArrayMirror pair = generalized.newArray(2);
pair.set(0, a.get(n));
pair.set(1, b.get(n));
zipped[n] = createArray(getContext(), pair.getArray(), 2);
} else {
zipped[n] = createArray(getContext(), new Object[] { a.get(n), nil() }, 2);
}
}


return createArray(getContext(), zipped, zippedLength);
}

@@ -2206,7 +2178,7 @@ private Object zipRuby(VirtualFrame frame, DynamicObject array, DynamicObject bl
}

protected static boolean fallback(DynamicObject array, DynamicObject other, Object[] others) {
return !ArrayGuards.isObjectArray(array) || ArrayGuards.isNullArray(other) || ArrayGuards.isLongArray(other) || others.length > 0;
return ArrayGuards.isNullArray(array) || ArrayGuards.isNullArray(other) || others.length > 0;
}

}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.RubyGuards;

public abstract class ArrayStrategy {

@@ -62,6 +63,11 @@ protected RuntimeException unsupported() {

public static ArrayStrategy of(DynamicObject array) {
CompilerAsserts.neverPartOfCompilation();

if (!RubyGuards.isRubyArray(array)) {
return FallbackArrayStrategy.INSTANCE;
}

if (ArrayGuards.isIntArray(array)) {
return IntArrayStrategy.INSTANCE;
} else if (ArrayGuards.isLongArray(array)) {
@@ -72,7 +78,7 @@ public static ArrayStrategy of(DynamicObject array) {
return ObjectArrayStrategy.INSTANCE;
} else {
assert ArrayGuards.isNullArray(array);
return NullArrayStrategy.INSTANCE;
return FallbackArrayStrategy.INSTANCE;
}
}

@@ -316,11 +322,11 @@ public String toString() {

}

// Null-pattern strategy
// Fallback strategy

private static class NullArrayStrategy extends ArrayStrategy {
private static class FallbackArrayStrategy extends ArrayStrategy {

static final ArrayStrategy INSTANCE = new NullArrayStrategy();
static final ArrayStrategy INSTANCE = new FallbackArrayStrategy();

public boolean accepts(Object value) {
return false;