Skip to content
Permalink
Browse files
[Truffle] benchmark-ips sample file runs - some formatting errors.
  • Loading branch information
chrisseaton committed Dec 29, 2014
1 parent 0cd457e commit 6199915cd1f680897743f57726a835426f766ed9
@@ -32,6 +32,7 @@
import org.jruby.truffle.nodes.methods.arguments.MissingArgumentBehaviour;
import org.jruby.truffle.nodes.methods.arguments.ReadPreArgumentNode;
import org.jruby.truffle.nodes.methods.locals.ReadLevelVariableNodeFactory;
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.BreakException;
import org.jruby.truffle.runtime.control.NextException;
@@ -3438,31 +3439,34 @@ public RubyArray sliceLongFixnum(RubyArray array, int start, int length) {

}

@CoreMethod(names = "sort")
@CoreMethod(names = "sort", needsBlock = true)
public abstract static class SortNode extends ArrayCoreMethodNode {

@Child protected DispatchHeadNode compareDispatchNode;
@Child protected YieldDispatchHeadNode yieldNode;

public SortNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
compareDispatchNode = new DispatchHeadNode(context);
yieldNode = new YieldDispatchHeadNode(context);
}

public SortNode(SortNode prev) {
super(prev);
compareDispatchNode = prev.compareDispatchNode;
yieldNode = prev.yieldNode;
}

@Specialization(guards = "isNull")
public RubyArray sortNull(RubyArray array) {
public RubyArray sortNull(RubyArray array, UndefinedPlaceholder compare) {
notDesignedForCompilation();

return new RubyArray(getContext().getCoreLibrary().getArrayClass());
}

@ExplodeLoop
@Specialization(guards = {"isIntegerFixnum", "isSmall"})
public RubyArray sortVeryShortIntegerFixnum(VirtualFrame frame, RubyArray array) {
public RubyArray sortVeryShortIntegerFixnum(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
final int[] store = (int[]) array.getStore();

final int size = array.getSize();
@@ -3487,7 +3491,7 @@ public RubyArray sortVeryShortIntegerFixnum(VirtualFrame frame, RubyArray array)
}

@Specialization(guards = "isIntegerFixnum")
public RubyArray sortIntegerFixnum(VirtualFrame frame, RubyArray array) {
public RubyArray sortIntegerFixnum(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
notDesignedForCompilation();

final Object[] boxed = ArrayUtils.box((int[]) array.getStore());
@@ -3498,7 +3502,7 @@ public RubyArray sortIntegerFixnum(VirtualFrame frame, RubyArray array) {

@ExplodeLoop
@Specialization(guards = {"isLongFixnum", "isSmall"})
public RubyArray sortVeryShortLongFixnum(VirtualFrame frame, RubyArray array) {
public RubyArray sortVeryShortLongFixnum(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
final long[] store = (long[]) array.getStore();

final int size = array.getSize();
@@ -3523,7 +3527,7 @@ public RubyArray sortVeryShortLongFixnum(VirtualFrame frame, RubyArray array) {
}

@Specialization(guards = "isLongFixnum")
public RubyArray sortLongFixnum(VirtualFrame frame, RubyArray array) {
public RubyArray sortLongFixnum(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
notDesignedForCompilation();

final Object[] boxed = ArrayUtils.box((long[]) array.getStore());
@@ -3533,7 +3537,7 @@ public RubyArray sortLongFixnum(VirtualFrame frame, RubyArray array) {
}

@Specialization(guards = "isFloat")
public RubyArray sortDouble(VirtualFrame frame, RubyArray array) {
public RubyArray sortDouble(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
notDesignedForCompilation();

final Object[] boxed = ArrayUtils.box((double[]) array.getStore());
@@ -3543,7 +3547,7 @@ public RubyArray sortDouble(VirtualFrame frame, RubyArray array) {
}

@Specialization(guards = {"isObject", "isSmall"})
public RubyArray sortVeryShortObject(VirtualFrame frame, RubyArray array) {
public RubyArray sortVeryShortObject(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
final Object[] store = (Object[]) array.getStore();

// Insertion sort
@@ -3565,14 +3569,23 @@ public RubyArray sortVeryShortObject(VirtualFrame frame, RubyArray array) {
}

@Specialization(guards = "isObject")
public RubyArray sortObject(VirtualFrame frame, RubyArray array) {
public RubyArray sortObject(VirtualFrame frame, RubyArray array, UndefinedPlaceholder compare) {
notDesignedForCompilation();

final Object[] store = Arrays.copyOf((Object[]) array.getStore(), array.getSize());
sort(frame, store);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, array.getSize());
}

@Specialization(guards = "isObject")
public RubyArray sortWithCompareBlock(VirtualFrame frame, RubyArray array, RubyProc compare) {
notDesignedForCompilation();

final Object[] store = Arrays.copyOf((Object[]) array.getStore(), array.getSize());
sort(frame, store, compare);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, array.getSize());
}

private <T> void sort(VirtualFrame frame, T[] objects) {
final VirtualFrame finalFrame = frame;

@@ -3587,6 +3600,21 @@ public int compare(Object a, Object b) {
});
}

private <T> void sort(VirtualFrame frame, T[] objects, RubyProc compare) {
final VirtualFrame finalFrame = frame;
final RubyProc finalCompare = compare;

Arrays.sort(objects, new Comparator<Object>() {

@Override
public int compare(Object a, Object b) {
// TODO(CS): node for this cast
return (int) yieldNode.dispatch(finalFrame, finalCompare, a, b);
}

});
}

protected static boolean isSmall(RubyArray array) {
return array.getSize() <= RubyArray.ARRAYS_SMALL;
}
@@ -26,23 +26,20 @@
@CoreClass(name = "Proc")
public abstract class ProcNodes {

@CoreMethod(names = "initialize", needsBlock = true)
public abstract static class InitializeNode extends CoreMethodNode {
@CoreMethod(names = "arity")
public abstract static class ArityNode extends CoreMethodNode {

public InitializeNode(RubyContext context, SourceSection sourceSection) {
public ArityNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public InitializeNode(InitializeNode prev) {
public ArityNode(ArityNode prev) {
super(prev);
}

@Specialization
public RubyNilClass initialize(RubyProc proc, RubyProc block) {
proc.initialize(block.getSharedMethodInfo(), block.getCallTargetForMethods(), block.getCallTargetForMethods(),
block.getDeclarationFrame(), block.getDeclaringModule(), block.getMethod(), block.getSelfCapturedInScope(), block.getBlockCapturedInScope());

return getContext().getCoreLibrary().getNilObject();
public int arity(RubyProc proc) {
return 1;
}

}
@@ -101,6 +98,27 @@ public Object call(VirtualFrame frame, RubyProc proc, Object[] args, RubyProc bl

}

@CoreMethod(names = "initialize", needsBlock = true)
public abstract static class InitializeNode extends CoreMethodNode {

public InitializeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public InitializeNode(InitializeNode prev) {
super(prev);
}

@Specialization
public RubyNilClass initialize(RubyProc proc, RubyProc block) {
proc.initialize(block.getSharedMethodInfo(), block.getCallTargetForMethods(), block.getCallTargetForMethods(),
block.getDeclarationFrame(), block.getDeclaringModule(), block.getMethod(), block.getSelfCapturedInScope(), block.getBlockCapturedInScope());

return getContext().getCoreLibrary().getNilObject();
}

}

@CoreMethod(names = "lambda?")
public abstract static class LambdaNode extends CoreMethodNode {

@@ -20,6 +20,29 @@
@CoreClass(name = "Time")
public abstract class TimeNodes {

@CoreMethod(names = "+", required = 1)
public abstract static class AddNode extends CoreMethodNode {

public AddNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public AddNode(AddNode prev) {
super(prev);
}

@Specialization
public double add(RubyTime a, int b) {
return a.getRealSeconds() + b;
}

@Specialization
public double add(RubyTime a, RubyTime b) {
return a.getRealSeconds() + b.getRealSeconds();
}

}

@CoreMethod(names = "-", required = 1)
public abstract static class SubNode extends CoreMethodNode {

@@ -31,13 +54,36 @@ public SubNode(SubNode prev) {
super(prev);
}

@Specialization
public double sub(RubyTime a, double b) {
return a.getRealSeconds() - b;
}

@Specialization
public double sub(RubyTime a, RubyTime b) {
return a.getRealSeconds() - b.getRealSeconds();
}

}

@CoreMethod(names = "<", required = 1)
public abstract static class LessNode extends CoreMethodNode {

public LessNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public LessNode(LessNode prev) {
super(prev);
}

@Specialization
public boolean less(RubyTime a, double b) {
return a.getRealSeconds() < b;
}

}

@CoreMethod(names = "now", onSingleton = true)
public abstract static class NowNode extends CoreMethodNode {

@@ -79,7 +125,25 @@ public RubyTime fromArray(int second,
int nanoOfSecond,
boolean isdst,
RubyString zone) {
return RubyTime.fromArray(getContext().getCoreLibrary().getTimeClass(),second, minute, hour, dayOfMonth, month, year, nanoOfSecond, isdst, zone);
return RubyTime.fromArray(getContext().getCoreLibrary().getTimeClass(), second, minute, hour, dayOfMonth, month, year, nanoOfSecond, isdst, zone);
}

}

@CoreMethod(names = "to_f")
public abstract static class ToFNode extends CoreMethodNode {

public ToFNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ToFNode(ToFNode prev) {
super(prev);
}

@Specialization
public double toF(RubyTime time) {
return time.getRealSeconds();
}

}
@@ -111,6 +111,10 @@ public static void format(RubyContext context, PrintStream stream, String format
}

switch (type) {
case '%': {
stream.print("%");
break;
}
case 's': {
formatBuilder.append("s");
assert formatBuilder.toString().equals("%s");
@@ -143,7 +147,7 @@ public static void format(RubyContext context, PrintStream stream, String format
}

default:
throw new RuntimeException("Kernel#sprintf error");
throw new RuntimeException("Kernel#sprintf error " + type);
}

v++;
@@ -34,6 +34,10 @@ def self.print(*values)
Kernel.send(:print, *values)
end

def self.printf(*values)
Kernel.send(:printf, *values)
end

def self.write(value)
print value
end
@@ -42,6 +46,13 @@ def self.flush
Truffle::Debug.flush_stdout
end

def self.sync
false
end

def self.sync=(value)
end

def self.external_encoding
@external
end
@@ -173,25 +173,27 @@ def to_r
(seconds + subsec).to_r
end

def to_f
to_r.to_f
end

def +(other)
raise TypeError, 'time + time?' if other.kind_of?(Time)

case other = Rubinius::Type.coerce_to_exact_num(other)
when Integer
other_sec = other
other_nsec = 0
else
other_sec, nsec_frac = other.divmod(1)
other_nsec = (nsec_frac * 1_000_000_000).to_i
end
# TODO(CS)
#def to_f
# to_r.to_f
#end

# Don't use self.class, MRI doesn't honor subclasses here
Time.specific(seconds + other_sec, nsec + other_nsec, @is_gmt, @offset)
end
# TODO(CS)
#def +(other)
# raise TypeError, 'time + time?' if other.kind_of?(Time)
#
# case other = Rubinius::Type.coerce_to_exact_num(other)
# when Integer
# other_sec = other
# other_nsec = 0
# else
# other_sec, nsec_frac = other.divmod(1)
# other_nsec = (nsec_frac * 1_000_000_000).to_i
# end
#
# # Don't use self.class, MRI doesn't honor subclasses here
# Time.specific(seconds + other_sec, nsec + other_nsec, @is_gmt, @offset)
#end

# TODO(CS)
#def -(other)

2 comments on commit 6199915

@nirvdrum
Copy link
Contributor

@nirvdrum nirvdrum commented on 6199915 Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is a personal preference or a consistency issue, but I like that we use the parameter name "block" for Ruby blocks. It makes it easy for them to stick out in a sea of UndefinedPlaceholders.

@chrisseaton
Copy link
Contributor Author

@chrisseaton chrisseaton commented on 6199915 Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I guess unless you look at the @CoreMethod annotation you can't tell if it's a final argument that just happens to be a Proc or a block. We can check it's consistently called block when we next do a big refactor (we need to merge another branch first).

Please sign in to comment.