Skip to content

Commit ac50c7b

Browse files
committed
Make check_real? a Primitive
* Removes two calls per call site calling that.
1 parent c27de3c commit ac50c7b

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/main/java/org/truffleruby/core/support/TypeNodes.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Arrays;
1414
import java.util.List;
1515

16+
import com.oracle.truffle.api.profiles.ConditionProfile;
1617
import org.truffleruby.builtins.CoreMethod;
1718
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1819
import org.truffleruby.builtins.CoreModule;
@@ -43,6 +44,7 @@
4344
import org.truffleruby.language.RubyDynamicObject;
4445
import org.truffleruby.language.RubyNode;
4546
import org.truffleruby.language.control.RaiseException;
47+
import org.truffleruby.language.dispatch.DispatchNode;
4648
import org.truffleruby.language.library.RubyLibrary;
4749
import org.truffleruby.language.objects.IsANode;
4850
import org.truffleruby.language.objects.LogicalClassNode;
@@ -404,6 +406,35 @@ protected Object check(Object value,
404406
}
405407
}
406408

409+
@Primitive(name = "check_real?")
410+
@NodeChild(value = "value", type = RubyNode.class)
411+
public abstract static class CheckRealNode extends PrimitiveNode {
412+
@Specialization
413+
protected boolean check(int value) {
414+
return true;
415+
}
416+
417+
@Specialization
418+
protected boolean check(long value) {
419+
return true;
420+
}
421+
422+
@Specialization
423+
protected boolean check(double value) {
424+
return true;
425+
}
426+
427+
@Fallback
428+
protected boolean other(Object value,
429+
@Cached IsANode isANode,
430+
@Cached ConditionProfile numericProfile,
431+
@Cached DispatchNode isRealNode,
432+
@Cached BooleanCastNode booleanCastNode) {
433+
return numericProfile.profile(isANode.executeIsA(value, coreLibrary().numericClass)) &&
434+
booleanCastNode.executeToBoolean(isRealNode.call(value, "real?"));
435+
}
436+
}
437+
407438
@Primitive(name = "undefined?")
408439
@NodeChild(value = "value", type = RubyNode.class)
409440
public abstract static class IsUndefinedNode extends PrimitiveNode {

src/main/ruby/truffleruby/core/complex.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Complex < Numeric
4141
:round, :step, :truncate, :i, :negative?, :positive?
4242

4343
def self.convert(real, imag = undefined, exception: true)
44-
if check_real?(real) && check_real?(imag)
44+
if Primitive.check_real?(real) && Primitive.check_real?(imag)
4545
return new(real, imag)
4646
end
4747

@@ -99,22 +99,17 @@ def Complex.generic?(other) # :nodoc:
9999
end
100100

101101
def Complex.rect(real, imag=0)
102-
raise TypeError, 'not a real' unless check_real?(real) && check_real?(imag)
102+
raise TypeError, 'not a real' unless Primitive.check_real?(real) && Primitive.check_real?(imag)
103103
new(real, imag)
104104
end
105105
class << self; alias_method :rectangular, :rect end
106106

107107
def Complex.polar(r, theta=0)
108-
raise TypeError, 'not a real' unless check_real?(r) && check_real?(theta)
108+
raise TypeError, 'not a real' unless Primitive.check_real?(r) && Primitive.check_real?(theta)
109109

110110
Complex(r*Math.cos(theta), r*Math.sin(theta))
111111
end
112112

113-
def Complex.check_real?(obj)
114-
Primitive.object_kind_of?(obj, Numeric) && obj.real?
115-
end
116-
private_class_method :check_real?
117-
118113
attr_reader :real, :imag
119114
alias_method :imaginary, :imag
120115

0 commit comments

Comments
 (0)