From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS6461 209.249.0.0/16 X-Spam-Status: No, score=-1.6 required=3.0 tests=AWL,BAYES_05,RCVD_IN_XBL, URIBL_BLOCKED shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (tor-exit.kartofelzik.info [209.249.180.198]) by dcvr.yhbt.net (Postfix) with ESMTP id BC7261F618 for ; Mon, 27 Jul 2015 10:17:52 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] memoize hashval for RSymbol Date: Mon, 27 Jul 2015 10:17:50 +0000 Message-Id: <1437992270-20549-1-git-send-email-e@80x24.org> List-Id: This speeds up the hash function for dynamic symbols. [ruby-core:70129] [Bug #11396], nearly up to Ruby 2.1 levels Power-of-two hash sizing [Feature #9425] speeds up cases where we have a good hash, but this means we can no longer hide behind weak hashes. Unfortunately, object IDs do not hash well, but we may use the extra space in the RSymbol struct to memoize the hash value. Further optimizations should be possible. For now, the st.c APIs force us to calculate rb_str_hash redundantly at dsym registration. --- common.mk | 1 + hash.c | 3 ++- symbol.c | 6 ++++++ symbol.h | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/common.mk b/common.mk index 9d00291..46d8a2a 100644 --- a/common.mk +++ b/common.mk @@ -1532,6 +1532,7 @@ hash.$(OBJEXT): {$(VPATH)}oniguruma.h hash.$(OBJEXT): {$(VPATH)}probes.h hash.$(OBJEXT): {$(VPATH)}st.h hash.$(OBJEXT): {$(VPATH)}subst.h +hash.$(OBJEXT): {$(VPATH)}symbol.h hash.$(OBJEXT): {$(VPATH)}util.h hash.$(OBJEXT): {$(VPATH)}vm_opts.h inits.$(OBJEXT): $(hdrdir)/ruby/ruby.h diff --git a/hash.c b/hash.c index 7b8733f..5b13d98 100644 --- a/hash.c +++ b/hash.c @@ -17,6 +17,7 @@ #include #include "probes.h" #include "id.h" +#include "symbol.h" #ifdef __APPLE__ # ifdef HAVE_CRT_EXTERNS_H @@ -149,7 +150,7 @@ rb_any_hash(VALUE a) hnum = rb_str_hash(a); } else if (BUILTIN_TYPE(a) == T_SYMBOL) { - hnum = rb_objid_hash((st_index_t)a); + return RSYMBOL(a)->hashval; } else if (BUILTIN_TYPE(a) == T_FLOAT) { return rb_dbl_hash(rb_float_value(a)); diff --git a/symbol.c b/symbol.c index 9fbe3dd..9e2fccd 100644 --- a/symbol.c +++ b/symbol.c @@ -505,12 +505,18 @@ static VALUE dsymbol_alloc(const VALUE klass, const VALUE str, rb_encoding * const enc, const ID type) { const VALUE dsym = rb_newobj_of(klass, T_SYMBOL | FL_WB_PROTECTED); + st_index_t hashval; rb_enc_associate(dsym, enc); OBJ_FREEZE(dsym); RB_OBJ_WRITE(dsym, &RSYMBOL(dsym)->fstr, str); RSYMBOL(dsym)->id = type; + /* we want hashval to be in Fixnum range [ruby-core:15713] r15672 */ + hashval = rb_str_hash(str); + hashval <<= 1; + RSYMBOL(dsym)->hashval = (st_index_t)RSHIFT(hashval, 1); + register_sym(str, dsym); rb_hash_aset(global_symbols.dsymbol_fstr_hash, str, Qtrue); diff --git a/symbol.h b/symbol.h index 549eabd..5c52b97 100644 --- a/symbol.h +++ b/symbol.h @@ -25,6 +25,7 @@ struct RSymbol { struct RBasic basic; + st_index_t hashval; VALUE fstr; ID id; }; -- EW