ruby-changes:54502
From: ko1 <ko1@a...>
Date: Sat, 5 Jan 2019 08:48:39 +0900 (JST)
Subject: [ruby-changes:54502] ko1:r66717 (trunk): refactoring usage of ar_lookup().
ko1 2019-01-05 08:48:33 +0900 (Sat, 05 Jan 2019) New Revision: 66717 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66717 Log: refactoring usage of ar_lookup(). * hash.c (hash_stlike_lookup): introduce inline a function and use it instead of using ar_lookup()/st_lookup() directly. Modified files: trunk/hash.c Index: hash.c =================================================================== --- hash.c (revision 66716) +++ hash.c (revision 66717) @@ -1736,6 +1736,25 @@ rb_hash_default_value(VALUE hash, VALUE https://github.com/ruby/ruby/blob/trunk/hash.c#L1736 } } +static inline int +hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval) +{ + hash_verify(hash); + + if (RHASH_AR_TABLE_P(hash)) { + return ar_lookup(hash, key, pval); + } + else { + return st_lookup(RHASH_ST_TABLE(hash), key, pval); + } +} + +MJIT_FUNC_EXPORTED int +rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval) +{ + return hash_stlike_lookup(hash, key, pval); +} + /* * call-seq: * hsh[key] -> value @@ -1755,26 +1774,11 @@ rb_hash_aref(VALUE hash, VALUE key) https://github.com/ruby/ruby/blob/trunk/hash.c#L1774 { st_data_t val; - if (RHASH_AR_TABLE_P(hash) && ar_lookup(hash, key, &val)) { - return (VALUE)val; - } - else if (RHASH_ST_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, &val)) { + if (hash_stlike_lookup(hash, key, &val)) { return (VALUE)val; } - hash_verify(hash); - return rb_hash_default_value(hash, key); -} - -MJIT_FUNC_EXPORTED int -rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval) -{ - hash_verify(hash); - - if (RHASH_AR_TABLE_P(hash)) { - return ar_lookup(hash, key, pval); - } else { - return st_lookup(RHASH_ST_TABLE(hash), key, pval); + return rb_hash_default_value(hash, key); } } @@ -1840,23 +1844,22 @@ rb_hash_fetch_m(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/hash.c#L1844 if (block_given && argc == 2) { rb_warn("block supersedes default value argument"); } - if (RHASH_AR_TABLE_P(hash) && ar_lookup(hash, key, &val)) { - return (VALUE)val; - } - else if (RHASH_ST_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, &val)) { + + if (hash_stlike_lookup(hash, key, &val)) { return (VALUE)val; } - if (block_given) return rb_yield(key); - if (argc == 1) { - VALUE desc = rb_protect(rb_inspect, key, 0); - if (NIL_P(desc)) { - desc = rb_any_to_s(key); - } - desc = rb_str_ellipsize(desc, 65); - rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key); + else { + if (block_given) return rb_yield(key); + if (argc == 1) { + VALUE desc = rb_protect(rb_inspect, key, 0); + if (NIL_P(desc)) { + desc = rb_any_to_s(key); + } + desc = rb_str_ellipsize(desc, 65); + rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key); + } + return argv[1]; } - hash_verify(hash); - return argv[1]; } VALUE @@ -3262,13 +3265,12 @@ rb_hash_values(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3265 MJIT_FUNC_EXPORTED VALUE rb_hash_has_key(VALUE hash, VALUE key) { - if (RHASH_AR_TABLE_P(hash) && ar_lookup(hash, key, 0)) { + if (hash_stlike_lookup(hash, key, NULL)) { return Qtrue; } - else if (RHASH_ST_TABLE_P(hash) && st_lookup(RHASH_ST_TABLE(hash), key, 0)) { - return Qtrue; + else { + return Qfalse; } - return Qfalse; } static int @@ -3319,20 +3321,17 @@ eql_i(VALUE key, VALUE val1, VALUE arg) https://github.com/ruby/ruby/blob/trunk/hash.c#L3321 struct equal_data *data = (struct equal_data *)arg; st_data_t val2; - if (RHASH_AR_TABLE_P(data->hash) && !ar_lookup(data->hash, key, &val2)) { - data->result = Qfalse; - return ST_STOP; - } - else if (RHASH_ST_TABLE_P(data->hash) && !st_lookup(RHASH_ST_TABLE(data->hash), key, &val2)) { + if (!hash_stlike_lookup(data->hash, key, &val2)) { data->result = Qfalse; return ST_STOP; } - - if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) { - data->result = Qfalse; - return ST_STOP; + else { + if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) { + data->result = Qfalse; + return ST_STOP; + } + return ST_CONTINUE; } - return ST_CONTINUE; } static VALUE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/