Skip to content

Commit

Permalink
[Truffle] Profile argument values.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Oct 14, 2014
1 parent 13cd181 commit 6aefa72
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
101 changes: 101 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/RubyValueProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.nodes;

import com.oracle.truffle.api.CompilerDirectives;

public class RubyValueProfile {

private static final Object UNINITIALIZED = new Object();
private static final Object GENERIC = new Object();

@CompilerDirectives.CompilationFinal
private Object cachedValue = UNINITIALIZED;

public Object profile(Object value) {
if (cachedValue != GENERIC) {
if (cachedValue instanceof Boolean && value instanceof Boolean
&& (boolean) cachedValue == (boolean) value) {
return cachedValue;
} else if (cachedValue instanceof Integer && value instanceof Integer
&& (int) cachedValue == (int) value) {
return cachedValue;
} else if (cachedValue instanceof Long && value instanceof Long
&& (long) cachedValue == (long) value) {
return cachedValue;
} else if (cachedValue instanceof Double && value instanceof Double
&& exactCompare((double) cachedValue, (double) value)) {
return cachedValue;
} else {
cacheMiss(value);
}
}
return value;
}

public boolean profile(boolean value) {
if (cachedValue != GENERIC) {
if (cachedValue instanceof Integer && (boolean) cachedValue == value) {
return (boolean) cachedValue;
} else {
cacheMiss(value);
}
}
return value;
}

public int profile(int value) {
if (cachedValue != GENERIC) {
if (cachedValue instanceof Integer && (int) cachedValue == value) {
return (int) cachedValue;
} else {
cacheMiss(value);
}
}
return value;
}

public long profile(long value) {
if (cachedValue != GENERIC) {
if (cachedValue instanceof Integer && (long) cachedValue == value) {
return (long) cachedValue;
} else {
cacheMiss(value);
}
}
return value;
}

public double profile(double value) {
if (cachedValue != GENERIC) {
if (cachedValue instanceof Double && exactCompare((double) cachedValue, value)) {
return (int) cachedValue;
} else {
cacheMiss(value);
}
}
return value;
}

private void cacheMiss(Object value) {
CompilerDirectives.transferToInterpreterAndInvalidate();
if (cachedValue == UNINITIALIZED) {
cachedValue = value;
} else {
cachedValue = GENERIC;
}
}

private static boolean exactCompare(double a, double b) {
// -0.0 == 0.0, but you can tell the difference through other means so need to know the difference
return Double.doubleToRawLongBits(a) == Double.doubleToRawLongBits(b);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ReadPreArgumentNode extends RubyNode {
private final BranchProfile outOfRangeProfile = new BranchProfile();
private final MissingArgumentBehaviour missingArgumentBehaviour;

private final RubyValueProfile argumentValueProfile = new RubyValueProfile();

public ReadPreArgumentNode(RubyContext context, SourceSection sourceSection, int index, MissingArgumentBehaviour missingArgumentBehaviour) {
super(context, sourceSection);
this.index = index;
Expand All @@ -50,7 +52,7 @@ public Object execute(VirtualFrame frame) {
}
}

return RubyArguments.getUserArgument(frame.getArguments(), index);
return argumentValueProfile.profile(RubyArguments.getUserArgument(frame.getArguments(), index));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class SelfNode extends RubyNode {

private final RubyValueProfile valueProfile = new RubyValueProfile();

public SelfNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
Expand Down

0 comments on commit 6aefa72

Please sign in to comment.