Skip to content

Commit

Permalink
Showing 11 changed files with 63 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;

/**
* Concatenate argument arrays (translating a org.jruby.ast.ArgsCatNode).
*/
@@ -63,7 +61,7 @@ private DynamicObject executeSingle(VirtualFrame frame) {
store = arrayBuilderNode.ensure(store, 1);
store = arrayBuilderNode.appendValue(store, 0, childObject);
}
return createArray(getContext(), arrayBuilderNode.finish(store, size), size);
return createArray(arrayBuilderNode.finish(store, size), size);
}

@ExplodeLoop
@@ -87,7 +85,7 @@ private DynamicObject executeMultiple(VirtualFrame frame) {
}
}

return createArray(getContext(), arrayBuilderNode.finish(store, length), length);
return createArray(arrayBuilderNode.finish(store, length), length);
}

@ExplodeLoop
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;

@NodeChildren({ @NodeChild(value = "array", type = RubyNode.class) })
@ImportStatic(ArrayGuards.class)
public abstract class ArrayDropTailNode extends RubyNode {
@@ -36,7 +34,7 @@ public ArrayDropTailNode(RubyContext context, SourceSection sourceSection, int i

@Specialization(guards = "isNullArray(array)")
public DynamicObject getHeadNull(DynamicObject array) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES")
@@ -45,11 +43,11 @@ public DynamicObject dropTail(DynamicObject array,
@Cached("createBinaryProfile()") ConditionProfile indexLargerThanSize) {
final int size = Layouts.ARRAY.getSize(array);
if (indexLargerThanSize.profile(index >= size)) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
} else {
final int newSize = size - index;
final Object newStore = strategy.newMirror(array).extractRange(0, newSize).getArray();
return createArray(getContext(), newStore, newSize);
return createArray(newStore, newSize);
}
}

Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;

@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
@ImportStatic(ArrayGuards.class)
public abstract class ArrayGetTailNode extends RubyNode {
@@ -36,7 +34,7 @@ public ArrayGetTailNode(RubyContext context, SourceSection sourceSection, int in

@Specialization(guards = "isNullArray(array)")
public DynamicObject getTailNull(DynamicObject array) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES")
@@ -45,10 +43,10 @@ public DynamicObject getTail(DynamicObject array,
@Cached("createBinaryProfile()") ConditionProfile indexLargerThanSize) {
final int size = Layouts.ARRAY.getSize(array);
if (indexLargerThanSize.profile(index >= size)) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
} else {
final Object newStore = strategy.newMirror(array).extractRange(index, size).getArray();
return createArray(getContext(), newStore, size - index);
return createArray(newStore, size - index);
}
}

59 changes: 29 additions & 30 deletions truffle/src/main/java/org/jruby/truffle/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
@@ -92,7 +92,6 @@
import java.util.Arrays;
import java.util.Comparator;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;
import static org.jruby.truffle.core.array.ArrayHelpers.getSize;
import static org.jruby.truffle.core.array.ArrayHelpers.getStore;
import static org.jruby.truffle.core.array.ArrayHelpers.setSize;
@@ -134,23 +133,23 @@ public abstract static class AddNode extends CoreMethodNode {

@Specialization(guards = { "isNullArray(a)", "isNullArray(b)" })
public DynamicObject addNullNull(DynamicObject a, DynamicObject b) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "isNullArray(a)", "!isNullArray(b)", "strategy.matches(b)" }, limit = "ARRAY_STRATEGIES")
public DynamicObject addNullOther(DynamicObject a, DynamicObject b,
@Cached("of(b)") ArrayStrategy strategy) {
final int size = getSize(b);
final ArrayMirror mirror = strategy.newMirror(b).extractRange(0, size);
return createArray(getContext(), mirror.getArray(), size);
return createArray(mirror.getArray(), size);
}

@Specialization(guards = { "!isNullArray(a)", "isNullArray(b)", "strategy.matches(a)" }, limit = "ARRAY_STRATEGIES")
public DynamicObject addOtherNull(DynamicObject a, DynamicObject b,
@Cached("of(a)") ArrayStrategy strategy) {
final int size = getSize(a);
final ArrayMirror mirror = strategy.newMirror(a).extractRange(0, size);
return createArray(getContext(), mirror.getArray(), size);
return createArray(mirror.getArray(), size);
}

// Same storage
@@ -164,7 +163,7 @@ public DynamicObject addSameType(DynamicObject a, DynamicObject b,
final ArrayMirror mirror = strategy.newArray(combinedSize);
strategy.newMirror(a).copyTo(mirror, 0, 0, aSize);
strategy.newMirror(b).copyTo(mirror, 0, aSize, bSize);
return createArray(getContext(), mirror.getArray(), combinedSize);
return createArray(mirror.getArray(), combinedSize);
}

// Generalizations
@@ -180,7 +179,7 @@ public DynamicObject addGeneralize(DynamicObject a, DynamicObject b,
final ArrayMirror mirror = generalized.newArray(combinedSize);
aStrategy.newMirror(a).copyTo(mirror, 0, 0, aSize);
bStrategy.newMirror(b).copyTo(mirror, 0, aSize, bSize);
return createArray(getContext(), mirror.getArray(), combinedSize);
return createArray(mirror.getArray(), combinedSize);
}

}
@@ -333,13 +332,13 @@ public DynamicObject slice(VirtualFrame frame, DynamicObject array, DynamicObjec
@Specialization(guards = { "!isInteger(a)", "!isIntRange(a)" })
public Object fallbackIndex(VirtualFrame frame, DynamicObject array, Object a, NotProvided length) {
Object[] objects = new Object[] { a };
return fallback(frame, array, createArray(getContext(), objects, objects.length));
return fallback(frame, array, createArray(objects, objects.length));
}

@Specialization(guards = { "!isIntRange(a)", "wasProvided(b)" })
public Object fallbackSlice(VirtualFrame frame, DynamicObject array, Object a, Object b) {
Object[] objects = new Object[] { a, b };
return fallback(frame, array, createArray(getContext(), objects, objects.length));
return fallback(frame, array, createArray(objects, objects.length));
}

public Object fallback(VirtualFrame frame, DynamicObject array, DynamicObject args) {
@@ -408,7 +407,7 @@ public Object setObject(VirtualFrame frame, DynamicObject array, int start, int
// Passing a non-array as value is the same as assigning a single-element array
ArrayMirror mirror = strategy.newArray(1);
mirror.set(0, value);
ary = createArray(getContext(), mirror.getArray(), 1);
ary = createArray(mirror.getArray(), 1);
}

return executeSet(frame, array, start, length, ary);
@@ -632,15 +631,15 @@ public abstract static class CompactNode extends ArrayCoreMethodNode {

@Specialization(guards = "isNullArray(array)")
public Object compactNull(DynamicObject array) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "!isObjectArray(array)", "strategy.matches(array)" }, limit = "ARRAY_STRATEGIES")
public DynamicObject compactPrimitive(DynamicObject array,
@Cached("of(array)") ArrayStrategy strategy) {
final int size = getSize(array);
Object store = strategy.newMirror(array).extractRange(0, size).getArray();
return createArray(getContext(), store, size);
return createArray(store, size);
}

@Specialization(guards = "isObjectArray(array)")
@@ -660,7 +659,7 @@ public Object compactObjects(DynamicObject array) {
}
}

return createArray(getContext(), newStore, m);
return createArray(newStore, m);
}

}
@@ -1293,7 +1292,7 @@ public abstract static class MapNode extends YieldingCoreMethodNode {

@Specialization(guards = "isNullArray(array)")
public DynamicObject mapNull(DynamicObject array, DynamicObject block) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES")
@@ -1316,7 +1315,7 @@ public Object map(VirtualFrame frame, DynamicObject array, DynamicObject block,
}
}

return createArray(getContext(), arrayBuilder.finish(mappedStore, size), size);
return createArray(arrayBuilder.finish(mappedStore, size), size);
}

}
@@ -1741,12 +1740,12 @@ public Object popNNegative(VirtualFrame frame, DynamicObject array, int n) {

@Specialization(guards = { "n >= 0", "isEmptyArray(array)" })
public Object popEmpty(VirtualFrame frame, DynamicObject array, int n) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "n == 0", "!isEmptyArray(array)" })
public Object popZeroNotEmpty(DynamicObject array, int n) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "n > 0", "!isEmptyArray(array)", "strategy.matches(array)" }, limit = "ARRAY_STRATEGIES")
@@ -1765,7 +1764,7 @@ public Object popNotEmpty(DynamicObject array, int n,
filler.copyTo(store, 0, size - numPop, numPop);
setSize(array, size - numPop);

return createArray(getContext(), popped.getArray(), numPop);
return createArray(popped.getArray(), numPop);
}

@Specialization(guards = { "wasProvided(n)", "!isInteger(n)", "!isLong(n)" })
@@ -1839,7 +1838,7 @@ public abstract static class RejectNode extends YieldingCoreMethodNode {

@Specialization(guards = "isNullArray(array)")
public Object rejectNull(DynamicObject array, DynamicObject block) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES")
@@ -1867,7 +1866,7 @@ public Object rejectOther(VirtualFrame frame, DynamicObject array, DynamicObject
}
}

return createArray(getContext(), arrayBuilder.finish(selectedStore, selectedSize), selectedSize);
return createArray(arrayBuilder.finish(selectedStore, selectedSize), selectedSize);
}

}
@@ -1978,7 +1977,7 @@ public abstract static class SelectNode extends YieldingCoreMethodNode {

@Specialization(guards = "isNullArray(array)")
public Object selectNull(DynamicObject array, DynamicObject block) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES")
@@ -2006,7 +2005,7 @@ public Object selectOther(VirtualFrame frame, DynamicObject array, DynamicObject
}
}

return createArray(getContext(), arrayBuilder.finish(selectedStore, selectedSize), selectedSize);
return createArray(arrayBuilder.finish(selectedStore, selectedSize), selectedSize);
}

}
@@ -2055,12 +2054,12 @@ public Object shiftNegative(DynamicObject array, int n) {

@Specialization(guards = "n == 0")
public Object shiftZero(DynamicObject array, int n) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "n > 0", "isEmptyArray(array)" })
public Object shiftManyEmpty(DynamicObject array, int n) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "n > 0", "strategy.matches(array)", "!isEmptyArray(array)" }, limit = "ARRAY_STRATEGIES")
@@ -2082,7 +2081,7 @@ public Object shiftMany(DynamicObject array, int n,
filler.copyTo(store, 0, size - numShift, numShift);
setSize(array, size - numShift);

return createArray(getContext(), result.getArray(), numShift);
return createArray(result.getArray(), numShift);
}

@Specialization(guards = { "wasProvided(n)", "!isInteger(n)", "!isLong(n)" })
@@ -2126,7 +2125,7 @@ public SortNode(RubyContext context, SourceSection sourceSection) {

@Specialization(guards = "isNullArray(array)")
public DynamicObject sortNull(DynamicObject array, Object unusedBlock) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@ExplodeLoop
@@ -2162,7 +2161,7 @@ public DynamicObject sortVeryShort(VirtualFrame frame, DynamicObject array, NotP
}
}

return createArray(getContext(), store.getArray(), size);
return createArray(store.getArray(), size);
}

@Specialization(guards = { "!isNullArray(array)", "!isSmall(array)" })
@@ -2177,7 +2176,7 @@ public Object sortObjectWithBlock(DynamicObject array, DynamicObject block) {
final int size = getSize(array);
Object[] copy = ((Object[]) getStore(array)).clone();
doSort(copy, size, block);
return createArray(getContext(), copy, size);
return createArray(copy, size);
}

@TruffleBoundary
@@ -2239,13 +2238,13 @@ public DynamicObject zipObjectIntegerFixnum(DynamicObject array, DynamicObject o
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);
zipped[n] = createArray(pair.getArray(), 2);
} else {
zipped[n] = createArray(getContext(), new Object[] { a.get(n), nil() }, 2);
zipped[n] = createArray(new Object[] { a.get(n), nil() }, 2);
}
}

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

@Specialization(guards = { "isRubyArray(other)", "fallback(array, other, others)" })
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;

@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
@ImportStatic(ArrayGuards.class)
public abstract class ArraySliceNode extends RubyNode {
@@ -40,7 +38,7 @@ public ArraySliceNode(RubyContext context, SourceSection sourceSection, int from

@Specialization(guards = "isNullArray(array)")
public DynamicObject sliceNull(DynamicObject array) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = { "strategy.matches(array)" }, limit = "ARRAY_STRATEGIES")
@@ -50,10 +48,10 @@ public DynamicObject readInBounds(DynamicObject array,
final int to = Layouts.ARRAY.getSize(array) + this.to;

if (emptyArray.profile(from >= to)) {
return createArray(getContext(), null, 0);
return createArray(null, 0);
} else {
final Object store = strategy.newMirror(array).extractRange(from, to).getArray();
return createArray(getContext(), store, to - from);
return createArray(store, to - from);
}

}
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.NonStandard;
import org.jruby.truffle.builtins.UnaryCoreMethodNode;
import org.jruby.truffle.core.array.ArrayHelpers;
import org.jruby.truffle.core.basicobject.BasicObjectNodesFactory.ReferenceEqualNodeFactory;
import org.jruby.truffle.core.cast.BooleanCastNodeGen;
import org.jruby.truffle.core.module.ModuleOperations;
@@ -260,32 +259,32 @@ public DynamicObject instanceVariables(DynamicObject self) {
}
}
final int size = names.size();
return ArrayHelpers.createArray(getContext(), names.toArray(new Object[size]), size);
return createArray(names.toArray(new Object[size]), size);
}

@Specialization
public DynamicObject instanceVariables(int self) {
return ArrayHelpers.createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization
public DynamicObject instanceVariables(long self) {
return ArrayHelpers.createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization
public DynamicObject instanceVariables(boolean self) {
return ArrayHelpers.createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "isNil(object)")
public DynamicObject instanceVariablesNil(DynamicObject object) {
return ArrayHelpers.createArray(getContext(), null, 0);
return createArray(null, 0);
}

@Specialization(guards = "isRubySymbol(object)")
public DynamicObject instanceVariablesSymbol(DynamicObject object) {
return ArrayHelpers.createArray(getContext(), null, 0);
return createArray(null, 0);
}

}
Original file line number Diff line number Diff line change
@@ -765,7 +765,7 @@ public DynamicObject getClassVariables(DynamicObject module) {
for (String variable : allClassVariables.keySet()) {
store[i++] = getSymbol(variable);
}
return ArrayHelpers.createArray(getContext(), store, size);
return createArray(store, size);
}
}

Original file line number Diff line number Diff line change
@@ -854,7 +854,7 @@ public DynamicObject dToA(double value) {

final int sign = value < 0 ? 1 : 0;

return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new Object[]{create7BitString(string, UTF8Encoding.INSTANCE), decimal, sign, string.length()}, 4);
return createArray(new Object[] { create7BitString(string, UTF8Encoding.INSTANCE), decimal, sign, string.length() }, 4);
}

}
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyBaseNode;
import org.jruby.truffle.language.control.RaiseException;
@@ -110,13 +109,13 @@ private DynamicObject divMod(long a, long b) {

if (integerDiv instanceof Long && ((long) integerDiv) >= Integer.MIN_VALUE && ((long) integerDiv) <= Integer.MAX_VALUE && mod >= Integer.MIN_VALUE && mod <= Integer.MAX_VALUE) {
useFixnumPairProfile.enter();
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new int[]{(int) (long) integerDiv, (int) mod}, 2);
return createArray(new int[] { (int) (long) integerDiv, (int) mod }, 2);
} else if (integerDiv instanceof Long) {
useObjectPairProfile.enter();
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new Object[]{integerDiv, mod}, 2);
return createArray(new Object[] { integerDiv, mod }, 2);
} else {
useObjectPairProfile.enter();
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new Object[]{
return createArray(new Object[] {
fixnumOrBignumQuotient.fixnumOrBignum((BigInteger) integerDiv),
mod}, 2);
}
@@ -142,7 +141,7 @@ private DynamicObject divMod(double a, double b) {
mod += b;
}

return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new Object[]{
return createArray(new Object[] {
fixnumOrBignumQuotient.fixnumOrBignum(div),
mod}, 2);
}
@@ -162,7 +161,7 @@ private DynamicObject divMod(BigInteger a, BigInteger b) {
bigIntegerResults[1] = b.add(bigIntegerResults[1]);
}

return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), new Object[]{
return createArray(new Object[] {
fixnumOrBignumQuotient.fixnumOrBignum(bigIntegerResults[0]),
fixnumOrBignumRemainder.fixnumOrBignum(bigIntegerResults[1])}, 2);
}
Original file line number Diff line number Diff line change
@@ -17,11 +17,14 @@
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;

import jnr.ffi.provider.MemoryManager;

import org.jcodings.Encoding;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.CoreLibrary;
import org.jruby.truffle.core.array.ArrayHelpers;
import org.jruby.truffle.core.exception.CoreExceptions;
import org.jruby.truffle.core.kernel.TraceManager;
import org.jruby.truffle.core.numeric.BignumOperations;
@@ -35,6 +38,7 @@
import org.jruby.truffle.platform.posix.TrufflePosix;
import org.jruby.truffle.stdlib.CoverageManager;
import org.jruby.util.ByteList;

import java.math.BigInteger;

@TypeSystemReference(RubyTypes.class)
@@ -97,6 +101,10 @@ protected DynamicObject createString(Rope rope) {
return StringOperations.createString(getContext(), rope);
}

protected DynamicObject createArray(Object store, int size) {
return ArrayHelpers.createArray(getContext(), store, size);
}

protected DynamicObject createBignum(BigInteger value) {
return BignumOperations.createBignum(getContext(), value);
}
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ public abstract static class AdjacentObjectsNode extends CoreMethodArrayArgument
@Specialization
public DynamicObject adjacentObjects(DynamicObject object) {
final Set<DynamicObject> objects = ObjectGraph.getAdjacentObjects(object);
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), objects.toArray(), objects.size());
return createArray(objects.toArray(), objects.size());
}

}
@@ -84,7 +84,7 @@ public abstract static class RootObjectsNode extends CoreMethodArrayArgumentsNod
@Specialization
public DynamicObject rootObjects() {
final Set<DynamicObject> objects = ObjectGraph.stopAndGetRootObjects(this, getContext());
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), objects.toArray(), objects.size());
return createArray(objects.toArray(), objects.size());
}

}

0 comments on commit 9c8ce9a

Please sign in to comment.