Project

General

Profile

Feature #11769 ยป 0001-optimize-case-when-for-nil.patch

tenderlovemaking (Aaron Patterson), 12/03/2015 09:42 PM

View differences:

compile.c
}
break;
}
case NODE_NIL:
return Qnil;
case NODE_STR:
return node->nd_lit = rb_fstring(node->nd_lit);
}
insns.def
SYMBOL_REDEFINED_OP_FLAG |
FIXNUM_REDEFINED_OP_FLAG |
BIGNUM_REDEFINED_OP_FLAG |
NIL_REDEFINED_OP_FLAG |
STRING_REDEFINED_OP_FLAG)) {
st_data_t val;
if (st_lookup(RHASH_TBL_RAW(hash), key, &val)) {
object.c
rb_define_method(rb_cNilClass, "&", false_and, 1);
rb_define_method(rb_cNilClass, "|", false_or, 1);
rb_define_method(rb_cNilClass, "^", false_xor, 1);
rb_define_method(rb_cNilClass, "===", rb_equal, 1);
rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
rb_undef_alloc_func(rb_cNilClass);
test/ruby/test_case.rb
end
}
end
module NilEqq
refine NilClass do
def === other
false
end
end
end
class NilEqqClass
using NilEqq
def eqq(a)
case a; when nil then nil; else :not_nil; end
end
end
def test_deoptimize_nil
assert_equal :not_nil, NilEqqClass.new.eqq(nil)
end
end
vm.c
if (klass == rb_cSymbol) return SYMBOL_REDEFINED_OP_FLAG;
if (klass == rb_cTime) return TIME_REDEFINED_OP_FLAG;
if (klass == rb_cRegexp) return REGEXP_REDEFINED_OP_FLAG;
if (klass == rb_cNilClass) return NIL_REDEFINED_OP_FLAG;
return 0;
}
......
OP(DIV, DIV), (C(Fixnum), C(Float));
OP(MOD, MOD), (C(Fixnum), C(Float));
OP(Eq, EQ), (C(Fixnum), C(Float), C(String));
OP(Eqq, EQQ), (C(Fixnum), C(Bignum), C(Float), C(Symbol), C(String));
OP(Eqq, EQQ), (C(Fixnum), C(Bignum), C(Float), C(Symbol), C(String), C(NilClass));
OP(LT, LT), (C(Fixnum), C(Float));
OP(LE, LE), (C(Fixnum), C(Float));
OP(GT, GT), (C(Fixnum), C(Float));
vm_core.h
#define SYMBOL_REDEFINED_OP_FLAG (1 << 6)
#define TIME_REDEFINED_OP_FLAG (1 << 7)
#define REGEXP_REDEFINED_OP_FLAG (1 << 8)
#define NIL_REDEFINED_OP_FLAG (1 << 9)
#define BASIC_OP_UNREDEFINED_P(op, klass) (LIKELY((GET_VM()->redefined_flag[(op)]&(klass)) == 0))
    (1-1/1)