Skip to content

Commit

Permalink
[Truffle] Continue to reduce duplication in array nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed May 12, 2015
1 parent ae14341 commit 2416cd3
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyHash;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

/**
* Read the rest of arguments after a certain point into an array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

@CoreClass(name = "BasicObject")
public abstract class BasicObjectNodes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.jruby.truffle.runtime.methods.Arity;
import org.jruby.truffle.runtime.methods.InternalMethod;
import org.jruby.truffle.runtime.methods.SharedMethodInfo;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.DirectCallNode;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import com.oracle.truffle.api.nodes.Node.Child;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
Expand Down Expand Up @@ -55,7 +54,7 @@
import org.jruby.truffle.runtime.methods.InternalMethod;
import org.jruby.truffle.runtime.subsystems.FeatureManager;
import org.jruby.truffle.runtime.subsystems.ThreadManager.BlockingActionWithoutGlobalLock;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;
import org.jruby.util.ByteList;

import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.array.ArrayMirror;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.util.ArrayUtils;

import java.util.Arrays;
import org.jruby.truffle.runtime.array.ArrayUtils;

@NodeChildren({
@NodeChild("array"),
Expand All @@ -32,103 +31,84 @@ public AppendOneNode(RubyContext context, SourceSection sourceSection) {

public abstract RubyArray executeAppendOne(RubyArray array, Object value);

// Append into an array with null storage
// Append into an empty array

// TODO CS 12-May-15 differentiate between null and empty but possibly having enough space

@Specialization(guards = "isNull(array)")
public RubyArray appendNull(RubyArray array, int value) {
@Specialization(guards = "isEmpty(array)")
public RubyArray appendOneEmpty(RubyArray array, int value) {
array.setStore(new int[]{value}, 1);
return array;
}

@Specialization(guards = "isNull(array)")
public RubyArray appendNull(RubyArray array, long value) {
@Specialization(guards = "isEmpty(array)")
public RubyArray appendOneEmpty(RubyArray array, long value) {
array.setStore(new long[]{value}, 1);
return array;
}

@Specialization(guards = "isNull(array)")
public RubyArray appendNull(RubyArray array, Object value) {
array.setStore(new Object[]{value}, 1);
return array;
}

// Append into empty, but non-null storage; we would be better off reusing any existing space, but don't worry for now

@Specialization(guards = {"!isNull(array)", "isEmpty(array)"})
public RubyArray appendEmpty(RubyArray array, int value) {
array.setStore(new int[]{value}, 1);
return array;
}

@Specialization(guards = {"!isNull(array)", "isEmpty(array)"})
public RubyArray appendEmpty(RubyArray array, long value) {
array.setStore(new long[]{value}, 1);
@Specialization(guards = "isEmpty(array)")
public RubyArray appendOneEmpty(RubyArray array, double value) {
array.setStore(new double[]{value}, 1);
return array;
}

@Specialization(guards ={"!isNull(array)", "isEmpty(array)"})
public RubyArray appendEmpty(RubyArray array, Object value) {
@Specialization(guards = "isEmpty(array)")
public RubyArray appendOneEmpty(RubyArray array, Object value) {
array.setStore(new Object[]{value}, 1);
return array;
}

// Append of the correct type

@Specialization(guards = "isIntegerFixnum(array)")
public RubyArray appendInteger(RubyArray array, int value,
public RubyArray appendOneSameType(RubyArray array, int value,
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;

int[] store = (int[]) array.getStore();

if (extendProfile.profile(newSize > store.length)) {
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
}

store[oldSize] = value;
array.setStore(store, newSize);
appendOneSameTypeGeneric(array, ArrayMirror.reflect((int[]) array.getStore()), value, extendProfile);
return array;
}

@Specialization(guards = "isLongFixnum(array)")
public RubyArray appendLong(RubyArray array, long value,
public RubyArray appendOneSameType(RubyArray array, long value,
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;

long[] store = (long[]) array.getStore();

if (extendProfile.profile(newSize > store.length)) {
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
}
appendOneSameTypeGeneric(array, ArrayMirror.reflect((long[]) array.getStore()), value, extendProfile);
return array;
}

store[oldSize] = value;
array.setStore(store, newSize);
@Specialization(guards = "isFloat(array)")
public RubyArray appendOneSameType(RubyArray array, double value,
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
appendOneSameTypeGeneric(array, ArrayMirror.reflect((double[]) array.getStore()), value, extendProfile);
return array;
}

@Specialization(guards = "isObject(array)")
public RubyArray appendObject(RubyArray array, Object value,
public RubyArray appendOneSameType(RubyArray array, Object value,
@Cached("createBinaryProfile()") ConditionProfile extendProfile) {
appendOneSameTypeGeneric(array, ArrayMirror.reflect((Object[]) array.getStore()), value, extendProfile);
return array;
}

public void appendOneSameTypeGeneric(RubyArray array, ArrayMirror storeMirror, Object value, ConditionProfile extendProfile) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;

Object[] store = (Object[]) array.getStore();
final ArrayMirror newStoreMirror;

if (extendProfile.profile(newSize > store.length)) {
store = Arrays.copyOf(store, ArrayUtils.capacity(store.length, newSize));
if (extendProfile.profile(newSize > storeMirror.getLength())) {
newStoreMirror = storeMirror.copyMirror(ArrayUtils.capacity(storeMirror.getLength(), newSize));
} else {
newStoreMirror = storeMirror;
}

store[oldSize] = value;
array.setStore(store, newSize);
return array;
newStoreMirror.set(oldSize, value);
array.setStore(newStoreMirror.getArray(), newSize);
}

// Append forcing a generalization
// Append forcing a generalization from int[] to long[]

@Specialization(guards = "isIntegerFixnum(array)")
public RubyArray appendLongIntoInteger(RubyArray array, long value) {
public RubyArray appendOneLongIntoInteger(RubyArray array, long value) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;

Expand All @@ -140,30 +120,32 @@ public RubyArray appendLongIntoInteger(RubyArray array, long value) {
return array;
}

// Append forcing a generalization to Object[]

@Specialization(guards = {"isIntegerFixnum(array)", "!isInteger(value)", "!isLong(value)"})
public RubyArray appendObjectIntoInteger(RubyArray array, Object value) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;
public RubyArray appendOneGeneralizeInteger(RubyArray array, Object value) {
appendOneGeneralizeGeneric(array, ArrayMirror.reflect((int[]) array.getStore()), value);
return array;
}

final int[] oldStore = (int[]) array.getStore();
Object[] newStore = ArrayUtils.box(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);
@Specialization(guards = {"isLongFixnum(array)", "!isInteger(value)", "!isLong(value)"})
public RubyArray appendOneGeneralizeLong(RubyArray array, Object value) {
appendOneGeneralizeGeneric(array, ArrayMirror.reflect((long[]) array.getStore()), value);
return array;
}

newStore[oldSize] = value;
array.setStore(newStore, newSize);
@Specialization(guards = {"isFloat(array)", "!isDouble(value)"})
public RubyArray appendOneGeneralizeDouble(RubyArray array, Object value) {
appendOneGeneralizeGeneric(array, ArrayMirror.reflect((double[]) array.getStore()), value);
return array;
}

@Specialization(guards = {"isLongFixnum(array)", "!isInteger(value)", "!isLong(value)"})
public RubyArray appendObjectIntoLong(RubyArray array, Object value) {
public void appendOneGeneralizeGeneric(RubyArray array, ArrayMirror storeMirror, Object value) {
final int oldSize = array.getSize();
final int newSize = oldSize + 1;

final long[] oldStore = (long[]) array.getStore();
Object[] newStore = ArrayUtils.box(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);

Object[] newStore = storeMirror.getBoxedCopy(ArrayUtils.capacity(storeMirror.getLength(), newSize));
newStore[oldSize] = value;
array.setStore(newStore, newSize);
return array;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.oracle.truffle.api.nodes.Node;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;
import org.jruby.util.cli.Options;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
@ImportStatic(ArrayGuards.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
@ImportStatic(ArrayGuards.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import org.jruby.truffle.runtime.methods.Arity;
import org.jruby.truffle.runtime.methods.InternalMethod;
import org.jruby.truffle.runtime.methods.SharedMethodInfo;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;
import org.jruby.util.ByteList;
import org.jruby.util.Memo;

Expand Down Expand Up @@ -3171,7 +3171,7 @@ public RubyArray pushIntegerFixnum(RubyArray array, Object... values) {

if (oldStore.length < newSize) {
extendBranch.enter();
store = ArrayUtils.box(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);
store = ArrayUtils.boxExtra(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);
} else {
store = ArrayUtils.box(oldStore);
}
Expand Down Expand Up @@ -3303,7 +3303,7 @@ public RubyArray pushIntegerFixnumObject(RubyArray array, Object value) {

if (oldStore.length < newSize) {
extendBranch.enter();
newStore = ArrayUtils.box(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);
newStore = ArrayUtils.boxExtra(oldStore, ArrayUtils.capacity(oldStore.length, newSize) - oldStore.length);
} else {
newStore = ArrayUtils.box(oldStore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.util.ArrayUtils;
import org.jruby.truffle.runtime.array.ArrayUtils;

@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
@ImportStatic(ArrayGuards.class)
Expand Down

This file was deleted.

Loading

0 comments on commit 2416cd3

Please sign in to comment.