ruby-changes:28053
From: nobu <ko1@a...>
Date: Thu, 4 Apr 2013 17:23:21 +0900 (JST)
Subject: [ruby-changes:28053] nobu:r40105 (trunk): reduce overhead of TYPE
nobu 2013-04-04 17:23:10 +0900 (Thu, 04 Apr 2013) New Revision: 40105 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40105 Log: reduce overhead of TYPE * array.c (ary_join_1): reduce overhead of TYPE(). * eval.c (rb_frozen_class_p, rb_mod_{append,prepend}_features): ditto. * string.c (rb_to_id): ditto. Modified files: trunk/array.c trunk/eval.c trunk/string.c Index: array.c =================================================================== --- array.c (revision 40104) +++ array.c (revision 40105) @@ -1839,13 +1839,12 @@ ary_join_1(VALUE obj, VALUE ary, VALUE s https://github.com/ruby/ruby/blob/trunk/array.c#L1839 rb_str_buf_append(result, sep); val = RARRAY_PTR(ary)[i]; - switch (TYPE(val)) { - case T_STRING: + if (RB_TYPE_P(val, T_STRING)) { str_join: rb_str_buf_append(result, val); *first = FALSE; - break; - case T_ARRAY: + } + else if (RB_TYPE_P(val, T_ARRAY)) { obj = val; ary_join: if (val == ary) { @@ -1860,8 +1859,8 @@ ary_join_1(VALUE obj, VALUE ary, VALUE s https://github.com/ruby/ruby/blob/trunk/array.c#L1859 args[3] = (VALUE)first; rb_exec_recursive(recursive_join, obj, (VALUE)args); } - break; - default: + } + else { tmp = rb_check_string_type(val); if (!NIL_P(tmp)) { val = tmp; Index: string.c =================================================================== --- string.c (revision 40104) +++ string.c (revision 40105) @@ -8152,24 +8152,18 @@ rb_to_id(VALUE name) https://github.com/ruby/ruby/blob/trunk/string.c#L8152 { VALUE tmp; - switch (TYPE(name)) { - default: + if (SYMBOL_P(name)) { + return SYM2ID(name); + } + if (!RB_TYPE_P(name, T_STRING)) { tmp = rb_check_string_type(name); if (NIL_P(tmp)) { - tmp = rb_inspect(name); - rb_raise(rb_eTypeError, "%s is not a symbol", - RSTRING_PTR(tmp)); + rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol", + name); } name = tmp; - /* fall through */ - case T_STRING: - name = rb_str_intern(name); - /* fall through */ - case T_SYMBOL: - return SYM2ID(name); } - - UNREACHABLE; + return rb_intern_str(name); } /* Index: eval.c =================================================================== --- eval.c (revision 40104) +++ eval.c (revision 40105) @@ -34,6 +34,10 @@ VALUE rb_eSysStackError; https://github.com/ruby/ruby/blob/trunk/eval.c#L34 #include "eval_error.c" #include "eval_jump.c" +#define CLASS_OR_MODULE_P(obj) \ + (!SPECIAL_CONST_P(obj) && \ + (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE)) + /* Initializes the Ruby VM and builtin libraries. * @retval 0 if succeeded. * @retval non-zero an error occured. @@ -402,13 +406,17 @@ rb_mod_s_constants(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/eval.c#L406 void rb_frozen_class_p(VALUE klass) { - const char *desc = "something(?!)"; - + if (SPECIAL_CONST_P(klass)) { + noclass: + Check_Type(klass, T_CLASS); + } if (OBJ_FROZEN(klass)) { + const char *desc; + if (FL_TEST(klass, FL_SINGLETON)) desc = "object"; else { - switch (TYPE(klass)) { + switch (BUILTIN_TYPE(klass)) { case T_MODULE: case T_ICLASS: desc = "module"; @@ -416,6 +424,8 @@ rb_frozen_class_p(VALUE klass) https://github.com/ruby/ruby/blob/trunk/eval.c#L424 case T_CLASS: desc = "class"; break; + default: + goto noclass; } } rb_error_frozen(desc); @@ -953,13 +963,8 @@ rb_frame_pop(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L963 static VALUE rb_mod_append_features(VALUE module, VALUE include) { - switch (TYPE(include)) { - case T_CLASS: - case T_MODULE: - break; - default: + if (!CLASS_OR_MODULE_P(include)) { Check_Type(include, T_CLASS); - break; } rb_include_module(include, module); @@ -1006,13 +1011,8 @@ rb_mod_include(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/eval.c#L1011 static VALUE rb_mod_prepend_features(VALUE module, VALUE prepend) { - switch (TYPE(prepend)) { - case T_CLASS: - case T_MODULE: - break; - default: + if (!CLASS_OR_MODULE_P(prepend)) { Check_Type(prepend, T_CLASS); - break; } rb_prepend_module(prepend, module); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/