Skip to content

Commit

Permalink
[Truffle] benchmark-ips sample file runs - some formatting errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 29, 2014
1 parent 0cd457e commit 6199915
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 38 deletions.
46 changes: 37 additions & 9 deletions core/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
36 changes: 27 additions & 9 deletions core/src/main/java/org/jruby/truffle/nodes/core/ProcNodes.java
Expand Up @@ -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;
}

}
Expand Down Expand Up @@ -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 {

Expand Down
66 changes: 65 additions & 1 deletion core/src/main/java/org/jruby/truffle/nodes/core/TimeNodes.java
Expand Up @@ -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 {

Expand All @@ -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 {

Expand Down Expand Up @@ -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();
}

}
Expand Down
Expand Up @@ -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");
Expand Down Expand Up @@ -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++;
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/ruby/jruby/truffle/core/main.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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)
Expand Down

2 comments on commit 6199915

@nirvdrum
Copy link
Contributor

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

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.