From 33c8171c65ac0f66d1c096561e253f6f068fc85c Mon Sep 17 00:00:00 2001 From: shyouhei Date: Wed, 12 Sep 2018 01:55:00 +0000 Subject: 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64689 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- compile.c | 56 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 13 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index 71fecf1069..c16721e080 100644 --- a/compile.c +++ b/compile.c @@ -1743,27 +1743,57 @@ iseq_set_local_table(rb_iseq_t *iseq, const ID *tbl) 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; } - if (SPECIAL_CONST_P(val) || BUILTIN_TYPE(val) != BUILTIN_TYPE(lit)) { - return -1; + 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); } - if (BUILTIN_TYPE(lit) == 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; + } + else if (tlit == T_FLOAT) { + return rb_float_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); } } -- cgit v1.2.3