ruby-changes:52480
From: shyouhei <ko1@a...>
Date: Wed, 12 Sep 2018 10:55:07 +0900 (JST)
Subject: [ruby-changes:52480] shyouhei:r64689 (trunk): make opt_case_dispatch leaf
shyouhei 2018-09-12 10:55:00 +0900 (Wed, 12 Sep 2018) New Revision: 64689 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64689 Log: make opt_case_dispatch leaf This instruction can be written without rb_funcall. It not only boosts performance of case statements, but also makes room of future JIT improvements. Because opt_case_dispatch is about optimization this should not be a bad thing to have. ---- trunk: ruby 2.6.0dev (2018-09-05 trunk 64634) [x86_64-darwin15] ours: ruby 2.6.0dev (2018-09-12 leaf-insn 64688) [x86_64-darwin15] last_commit=make opt_case_dispatch leaf Calculating ------------------------------------- trunk ours vm2_case_lit 1.366 2.012 i/s - 1.000 times in 0.731839s 0.497008s Comparison: vm2_case_lit ours: 2.0 i/s trunk: 1.4 i/s - 1.47x slower Modified files: trunk/compile.c trunk/insns.def trunk/vm_insnhelper.c Index: insns.def =================================================================== --- insns.def (revision 64688) +++ insns.def (revision 64689) @@ -1020,10 +1020,6 @@ opt_case_dispatch https://github.com/ruby/ruby/blob/trunk/insns.def#L1020 (CDHASH hash, OFFSET else_offset) (..., VALUE key) () -/* Case dispatch involves hash lookup, which inevitably compares - * several objects each other. The same discussion to - * opt_newarray_max also goes here. */ -// attr bool leaf = false; /* has rb_any_cmp() */ // attr rb_snum_t sp_inc = -1; { OFFSET dst = vm_case_dispatch(hash, else_offset, key); Index: compile.c =================================================================== --- compile.c (revision 64688) +++ compile.c (revision 64689) @@ -1743,27 +1743,57 @@ iseq_set_local_table(rb_iseq_t *iseq, co https://github.com/ruby/ruby/blob/trunk/compile.c#L1743 static int cdhash_cmp(VALUE val, VALUE lit) { - if (val == lit) return 0; - if (SPECIAL_CONST_P(lit)) { - return val != lit; + int tval, tlit; + + if (val == lit) { + return 0; + } + else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) { + return val != lit; + } + else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) { + return -1; + } + else if (tlit != tval) { + return -1; + } + else if (tlit == T_SYMBOL) { + return val != lit; + } + else if (tlit == T_STRING) { + return rb_str_hash_cmp(lit, val); + } + else if (tlit == T_BIGNUM) { + long x = FIX2LONG(rb_big_cmp(lit, val)); + + /* Given lit and val are both Bignum, x must be -1, 0, 1. + * There is no need to call rb_fix2int here. */ + RUBY_ASSERT((x == 1) || (x == 0) || (x == -1)); + return (int)x; } - if (SPECIAL_CONST_P(val) || BUILTIN_TYPE(val) != BUILTIN_TYPE(lit)) { - return -1; + else if (tlit == T_FLOAT) { + return rb_float_cmp(lit, val); } - if (BUILTIN_TYPE(lit) == T_STRING) { - return rb_str_hash_cmp(lit, val); + else { + UNREACHABLE_RETURN(-1); } - return !rb_eql(lit, val); } static st_index_t cdhash_hash(VALUE a) { - if (SPECIAL_CONST_P(a)) return (st_index_t)a; - if (RB_TYPE_P(a, T_STRING)) return rb_str_hash(a); - { - VALUE hval = rb_hash(a); - return (st_index_t)FIX2LONG(hval); + switch (OBJ_BUILTIN_TYPE(a)) { + case -1: + case T_SYMBOL: + return (st_index_t)a; + case T_STRING: + return rb_str_hash(a); + case T_BIGNUM: + return FIX2LONG(rb_big_hash(a)); + case T_FLOAT: + return rb_dbl_long_hash(RFLOAT_VALUE(a)); + default: + UNREACHABLE_RETURN(0); } } Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 64688) +++ vm_insnhelper.c (revision 64689) @@ -3355,7 +3355,7 @@ vm_case_dispatch(CDHASH hash, OFFSET els https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3355 } } if (st_lookup(RHASH_TBL_RAW(hash), key, &val)) { - return FIX2INT((VALUE)val); + return FIX2LONG((VALUE)val); } else { return else_offset; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/