Skip to content

Commit

Permalink
* use RB_TYPE_P which is optimized for constant types, instead of
Browse files Browse the repository at this point in the history
  comparison with TYPE.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Sep 29, 2011
1 parent 68f97d7 commit 8e6e8e6
Show file tree
Hide file tree
Showing 37 changed files with 164 additions and 159 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Thu Sep 29 20:07:36 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>

* use RB_TYPE_P which is optimized for constant types, instead of
comparison with TYPE.

Wed Sep 28 09:20:37 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>

* configure.in (pthread_np.h): needs pthread.h to be included
Expand Down
8 changes: 4 additions & 4 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ enum {
sort_optimizable_count
};

#define STRING_P(s) (TYPE(s) == T_STRING && CLASS_OF(s) == rb_cString)
#define STRING_P(s) (RB_TYPE_P((s), T_STRING) && CLASS_OF(s) == rb_cString)

#define SORT_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(sort_opt_,type))
#define SORT_OPTIMIZABLE(data, type) \
Expand Down Expand Up @@ -3137,7 +3137,7 @@ rb_ary_rassoc(VALUE ary, VALUE value)

for (i = 0; i < RARRAY_LEN(ary); ++i) {
v = RARRAY_PTR(ary)[i];
if (TYPE(v) == T_ARRAY &&
if (RB_TYPE_P(v, T_ARRAY) &&
RARRAY_LEN(v) > 1 &&
rb_equal(RARRAY_PTR(v)[1], value))
return v;
Expand Down Expand Up @@ -3176,7 +3176,7 @@ static VALUE
rb_ary_equal(VALUE ary1, VALUE ary2)
{
if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) {
if (!RB_TYPE_P(ary2, T_ARRAY)) {
if (!rb_respond_to(ary2, rb_intern("to_ary"))) {
return Qfalse;
}
Expand Down Expand Up @@ -3211,7 +3211,7 @@ static VALUE
rb_ary_eql(VALUE ary1, VALUE ary2)
{
if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) return Qfalse;
if (!RB_TYPE_P(ary2, T_ARRAY)) return Qfalse;
if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
return rb_exec_recursive_paired(recursive_eql, ary1, ary2, ary2);
}
Expand Down
16 changes: 8 additions & 8 deletions bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ rb_cmpint(VALUE val, VALUE a, VALUE b)
if (l < 0) return -1;
return 0;
}
if (TYPE(val) == T_BIGNUM) {
if (RB_TYPE_P(val, T_BIGNUM)) {
if (BIGZEROP(val)) return 0;
if (RBIGNUM_SIGN(val)) return 1;
return -1;
Expand Down Expand Up @@ -269,7 +269,7 @@ bigfixize(VALUE x)
static VALUE
bignorm(VALUE x)
{
if (!FIXNUM_P(x) && TYPE(x) == T_BIGNUM) {
if (RB_TYPE_P(x, T_BIGNUM)) {
x = bigfixize(bigtrunc(x));
}
return x;
Expand Down Expand Up @@ -1628,7 +1628,7 @@ rb_big_eq(VALUE x, VALUE y)
static VALUE
rb_big_eql(VALUE x, VALUE y)
{
if (TYPE(y) != T_BIGNUM) return Qfalse;
if (!RB_TYPE_P(y, T_BIGNUM)) return Qfalse;
if (RBIGNUM_SIGN(x) != RBIGNUM_SIGN(y)) return Qfalse;
if (RBIGNUM_LEN(x) != RBIGNUM_LEN(y)) return Qfalse;
if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM_LEN(y)) != 0) return Qfalse;
Expand Down Expand Up @@ -3107,7 +3107,7 @@ rb_big_pow(VALUE x, VALUE y)
static inline VALUE
bit_coerce(VALUE x)
{
while (!FIXNUM_P(x) && TYPE(x) != T_BIGNUM) {
while (!FIXNUM_P(x) && !RB_TYPE_P(x, T_BIGNUM)) {
rb_raise(rb_eTypeError,
"can't convert %s into Integer for bitwise arithmetic",
rb_obj_classname(x));
Expand Down Expand Up @@ -3434,7 +3434,7 @@ rb_big_lshift(VALUE x, VALUE y)
}
break;
}
else if (TYPE(y) == T_BIGNUM) {
else if (RB_TYPE_P(y, T_BIGNUM)) {
if (!RBIGNUM_SIGN(y)) {
VALUE t = check_shiftdown(y, x);
if (!NIL_P(t)) return t;
Expand Down Expand Up @@ -3498,7 +3498,7 @@ rb_big_rshift(VALUE x, VALUE y)
}
break;
}
else if (TYPE(y) == T_BIGNUM) {
else if (RB_TYPE_P(y, T_BIGNUM)) {
if (RBIGNUM_SIGN(y)) {
VALUE t = check_shiftdown(y, x);
if (!NIL_P(t)) return t;
Expand Down Expand Up @@ -3586,7 +3586,7 @@ rb_big_aref(VALUE x, VALUE y)
VALUE shift;
long i, s1, s2;

if (TYPE(y) == T_BIGNUM) {
if (RB_TYPE_P(y, T_BIGNUM)) {
if (!RBIGNUM_SIGN(y))
return INT2FIX(0);
bigtrunc(y);
Expand Down Expand Up @@ -3646,7 +3646,7 @@ rb_big_coerce(VALUE x, VALUE y)
if (FIXNUM_P(y)) {
return rb_assoc_new(rb_int2big(FIX2LONG(y)), x);
}
else if (TYPE(y) == T_BIGNUM) {
else if (RB_TYPE_P(y, T_BIGNUM)) {
return rb_assoc_new(y, x);
}
else {
Expand Down
20 changes: 10 additions & 10 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ rb_class_boot(VALUE super)
void
rb_check_inheritable(VALUE super)
{
if (TYPE(super) != T_CLASS) {
if (!RB_TYPE_P(super, T_CLASS)) {
rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
rb_obj_classname(super));
}
Expand Down Expand Up @@ -463,7 +463,7 @@ rb_define_class(const char *name, VALUE super)
id = rb_intern(name);
if (rb_const_defined(rb_cObject, id)) {
klass = rb_const_get(rb_cObject, id);
if (TYPE(klass) != T_CLASS) {
if (!RB_TYPE_P(klass, T_CLASS)) {
rb_raise(rb_eTypeError, "%s is not a class", name);
}
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
Expand Down Expand Up @@ -530,7 +530,7 @@ rb_define_class_id_under(VALUE outer, ID id, VALUE super)

if (rb_const_defined_at(outer, id)) {
klass = rb_const_get_at(outer, id);
if (TYPE(klass) != T_CLASS) {
if (!RB_TYPE_P(klass, T_CLASS)) {
rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
}
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
Expand Down Expand Up @@ -581,7 +581,7 @@ rb_define_module(const char *name)
id = rb_intern(name);
if (rb_const_defined(rb_cObject, id)) {
module = rb_const_get(rb_cObject, id);
if (TYPE(module) == T_MODULE)
if (RB_TYPE_P(module, T_MODULE))
return module;
rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
}
Expand All @@ -605,7 +605,7 @@ rb_define_module_id_under(VALUE outer, ID id)

if (rb_const_defined_at(outer, id)) {
module = rb_const_get_at(outer, id);
if (TYPE(module) == T_MODULE)
if (RB_TYPE_P(module, T_MODULE))
return module;
rb_raise(rb_eTypeError, "%s::%s is not a module",
rb_class2name(outer), rb_obj_classname(module));
Expand Down Expand Up @@ -636,7 +636,7 @@ include_class_new(VALUE module, VALUE super)
RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
RCLASS_SUPER(klass) = super;
if (TYPE(module) == T_ICLASS) {
if (RB_TYPE_P(module, T_ICLASS)) {
RBASIC(klass)->klass = RBASIC(module)->klass;
}
else {
Expand All @@ -659,7 +659,7 @@ rb_include_module(VALUE klass, VALUE module)
rb_secure(4);
}

if (TYPE(module) != T_MODULE) {
if (!RB_TYPE_P(module, T_MODULE)) {
Check_Type(module, T_MODULE);
}

Expand Down Expand Up @@ -791,7 +791,7 @@ rb_mix_module(VALUE klass, VALUE module, st_table *constants, st_table *methods)
rb_secure(4);
}

if (TYPE(module) != T_MODULE) {
if (!RB_TYPE_P(module, T_MODULE)) {
Check_Type(module, T_MODULE);
}

Expand Down Expand Up @@ -1248,7 +1248,7 @@ rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
klass = RCLASS_SUPER(klass);
}
if (RTEST(recur)) {
while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
klass = RCLASS_SUPER(klass);
}
Expand Down Expand Up @@ -1435,7 +1435,7 @@ rb_singleton_class(VALUE obj)
VALUE klass = singleton_class_of(obj);

/* ensures an exposed class belongs to its own eigenclass */
if (TYPE(obj) == T_CLASS) (void)ENSURE_EIGENCLASS(klass);
if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);

return klass;
}
Expand Down
8 changes: 4 additions & 4 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ static st_index_t
cdhash_hash(VALUE a)
{
if (SPECIAL_CONST_P(a)) return (st_index_t)a;
if (TYPE(a) == T_STRING) return rb_str_hash(a);
if (RB_TYPE_P(a, T_STRING)) return rb_str_hash(a);
{
VALUE hval = rb_hash(a);
return (st_index_t)FIX2LONG(hval);
Expand Down Expand Up @@ -2335,7 +2335,7 @@ case_when_optimizable_literal(NODE * node)
case NODE_LIT: {
VALUE v = node->nd_lit;
double ival;
if (TYPE(v) == T_FLOAT &&
if (RB_TYPE_P(v, T_FLOAT) &&
modf(RFLOAT_VALUE(v), &ival) == 0.0) {
return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
}
Expand Down Expand Up @@ -5314,7 +5314,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
else if (FIXNUM_P(obj)) {
line_no = NUM2INT(obj);
}
else if (TYPE(obj) == T_ARRAY) {
else if (RB_TYPE_P(obj, T_ARRAY)) {
VALUE *argv = 0;
int argc = RARRAY_LENINT(obj) - 1;
st_data_t insn_id;
Expand Down Expand Up @@ -5356,7 +5356,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
case TS_ISEQ:
{
if (op != Qnil) {
if (TYPE(op) == T_ARRAY) {
if (RB_TYPE_P(op, T_ARRAY)) {
argv[j] = rb_iseq_load(op, iseq->self, Qnil);
}
else if (CLASS_OF(op) == rb_cISeq) {
Expand Down
10 changes: 5 additions & 5 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ f_mul(VALUE x, VALUE y)
if (FIXNUM_P(y)) {
long iy = FIX2LONG(y);
if (iy == 0) {
if (FIXNUM_P(x) || TYPE(x) == T_BIGNUM)
if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM))
return ZERO;
}
else if (iy == 1)
Expand All @@ -130,7 +130,7 @@ f_mul(VALUE x, VALUE y)
else if (FIXNUM_P(x)) {
long ix = FIX2LONG(x);
if (ix == 0) {
if (FIXNUM_P(y) || TYPE(y) == T_BIGNUM)
if (FIXNUM_P(y) || RB_TYPE_P(y, T_BIGNUM))
return ZERO;
}
else if (ix == 1)
Expand Down Expand Up @@ -166,14 +166,14 @@ fun1(real_p)
inline static VALUE
f_to_i(VALUE x)
{
if (TYPE(x) == T_STRING)
if (RB_TYPE_P(x, T_STRING))
return rb_str_to_inum(x, 10, 0);
return rb_funcall(x, id_to_i, 0);
}
inline static VALUE
f_to_f(VALUE x)
{
if (TYPE(x) == T_STRING)
if (RB_TYPE_P(x, T_STRING))
return DBL2NUM(rb_str_to_dbl(x, 0));
return rb_funcall(x, id_to_f, 0);
}
Expand Down Expand Up @@ -944,7 +944,7 @@ nucomp_coerce(VALUE self, VALUE other)
{
if (k_numeric_p(other) && f_real_p(other))
return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
if (TYPE(other) == T_COMPLEX)
if (RB_TYPE_P(other, T_COMPLEX))
return rb_assoc_new(other, self);

rb_raise(rb_eTypeError, "%s can't be coerced into %s",
Expand Down
2 changes: 1 addition & 1 deletion enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ enum_sort_by(VALUE obj)

RETURN_ENUMERATOR(obj, 0, 0);

if (TYPE(obj) == T_ARRAY && RARRAY_LEN(obj) <= LONG_MAX/2) {
if (RB_TYPE_P(obj, T_ARRAY) && RARRAY_LEN(obj) <= LONG_MAX/2) {
ary = rb_ary_new2(RARRAY_LEN(obj)*2);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion enumerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ enumerator_next_values(VALUE obj)
static VALUE
ary2sv(VALUE args, int dup)
{
if (TYPE(args) != T_ARRAY)
if (!RB_TYPE_P(args, T_ARRAY))
return args;

switch (RARRAY_LEN(args)) {
Expand Down
2 changes: 1 addition & 1 deletion error.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ syserr_initialize(int argc, VALUE *argv, VALUE self)
if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
klass = (VALUE)data;
/* change class */
if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
rb_raise(rb_eTypeError, "invalid instance type");
}
RBASIC(self)->klass = klass;
Expand Down
2 changes: 1 addition & 1 deletion eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ ruby_cleanup(volatile int ex)
if (!RTEST(err)) continue;

/* th->errinfo contains a NODE while break'ing */
if (TYPE(err) == T_NODE) continue;
if (RB_TYPE_P(err, T_NODE)) continue;

if (rb_obj_is_kind_of(err, rb_eSystemExit)) {
ex = sysexit_status(err);
Expand Down
4 changes: 2 additions & 2 deletions eval_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ rb_print_undef(VALUE klass, ID id, int scope)
}
rb_name_error(id, "undefined%s method `%s' for %s `%s'", v,
rb_id2name(id),
(TYPE(klass) == T_MODULE) ? "module" : "class",
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class2name(klass));
}

Expand All @@ -211,7 +211,7 @@ rb_print_undef_str(VALUE klass, VALUE name)
{
rb_name_error_str(name, "undefined method `%s' for %s `%s'",
RSTRING_PTR(name),
(TYPE(klass) == T_MODULE) ? "module" : "class",
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class2name(klass));
}

Expand Down
4 changes: 2 additions & 2 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3265,7 +3265,7 @@ count_objects(int argc, VALUE *argv, VALUE os)
VALUE hash;

if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (TYPE(hash) != T_HASH)
if (!RB_TYPE_P(hash, T_HASH))
rb_raise(rb_eTypeError, "non-hash given");
}

Expand Down Expand Up @@ -3384,7 +3384,7 @@ gc_stat(int argc, VALUE *argv, VALUE self)
VALUE hash;

if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (TYPE(hash) != T_HASH)
if (!RB_TYPE_P(hash, T_HASH))
rb_raise(rb_eTypeError, "non-hash given");
}

Expand Down
4 changes: 2 additions & 2 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ rb_any_cmp(VALUE a, VALUE b)
if (FIXNUM_P(a) && FIXNUM_P(b)) {
return a != b;
}
if (TYPE(a) == T_STRING && RBASIC(a)->klass == rb_cString &&
if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
TYPE(b) == T_STRING && RBASIC(b)->klass == rb_cString) {
return rb_str_hash_cmp(a, b);
}
Expand Down Expand Up @@ -1599,7 +1599,7 @@ hash_equal(VALUE hash1, VALUE hash2, int eql)
struct equal_data data;

if (hash1 == hash2) return Qtrue;
if (TYPE(hash2) != T_HASH) {
if (!RB_TYPE_P(hash2, T_HASH)) {
if (!rb_respond_to(hash2, rb_intern("to_hash"))) {
return Qfalse;
}
Expand Down
Loading

0 comments on commit 8e6e8e6

Please sign in to comment.