From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS24961 62.141.32.0/20 X-Spam-Status: No, score=-3.3 required=3.0 tests=AWL,BAYES_00,RCVD_IN_XBL, SPF_FAIL,SPF_HELO_FAIL,TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.1 Received: from 80x24.org (vps1650591.vs.webtropia-customer.com [62.141.37.236]) by dcvr.yhbt.net (Postfix) with ESMTP id 19D4D1F453 for ; Fri, 26 Oct 2018 05:09:10 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] hash.c: aset unconditionally deduplicate non-tainted string Date: Fri, 26 Oct 2018 05:09:08 +0000 Message-Id: <20181026050908.1183-1-e@80x24.org> List-Id: We revisit [Bug #9188] since st.c is much improved since then, and benchmarks against so_k_nucleotide seem to indicate little or no performance change compared to before. [ruby-core:89555] [Feature #15251] Original-patch-by: Anmol Chopra --- hash.c | 29 +---------------------------- test/ruby/test_hash.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/hash.c b/hash.c index fc38f2f0ab..a62769bc86 100644 --- a/hash.c +++ b/hash.c @@ -1564,37 +1564,10 @@ hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) return ST_CONTINUE; } -static VALUE -fstring_existing_str(VALUE str) -{ - st_data_t fstr; - st_table *tbl = rb_vm_fstring_table(); - - if (st_lookup(tbl, str, &fstr)) { - if (rb_objspace_garbage_object_p(fstr)) { - return rb_fstring(str); - } - else { - return (VALUE)fstr; - } - } - else { - return Qnil; - } -} - VALUE rb_hash_key_str(VALUE key) { - VALUE k; - - if (!RB_OBJ_TAINTED(key) && - (k = fstring_existing_str(key)) != Qnil) { - return k; - } - else { - return rb_str_new_frozen(key); - } + return RB_OBJ_TAINTED(key) ? rb_str_new_frozen(key) : rb_fstring(key); } static int diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 6aaeddc9d4..03ebd38f1f 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -280,6 +280,24 @@ def test_ASET_fstring_key assert_same a.keys[0], b.keys[0] end + def test_ASET_fstring_non_literal_key + underscore = "_" + non_literal_strings = Proc.new{ ["abc#{underscore}def", "abc" * 5, "abc" + "def", "" << "ghi" << "jkl"] } + + a, b = {}, {} + non_literal_strings.call.each do |string| + assert_equal 1, a[string] = 1 + end + + non_literal_strings.call.each do |string| + assert_equal 1, b[string] = 1 + end + + [a.keys, b.keys].transpose.each do |key_a, key_b| + assert_same key_a, key_b + end + end + def test_hash_aset_fstring_identity h = {}.compare_by_identity h['abc'] = 1 -- EW