diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-05-29 23:22:40 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-05-29 23:22:40 +0000 |
commit | 06024e8ebe5c8ba5d11e824e34abebcf232b8157 (patch) | |
tree | c8ba6eae3f6bfa68983384753abdfbc8967a4a7c | |
parent | 02fb7208ea7367c2aa9112ddf3bd0ea336097b1c (diff) | |
download | ruby-06024e8ebe5c8ba5d11e824e34abebcf232b8157.tar.gz |
variable.c: extract common functions for generic ivar indices
* variable.c (iv_index_tbl_make): extract from rb_ivar_set
(iv_index_tbl_extend): ditto
(iv_index_tbl_newsize): ditto
(rb_ivar_set): use extracted functions
[ruby-core:69323] (Part 1)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50676 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | variable.c | 63 |
2 files changed, 51 insertions, 20 deletions
@@ -1,3 +1,11 @@ +Sat May 30 08:10:46 2015 Eric Wong <e@80x24.org> + + * variable.c (iv_index_tbl_make): extract from rb_ivar_set + (iv_index_tbl_extend): ditto + (iv_index_tbl_newsize): ditto + (rb_ivar_set): use extracted functions + [ruby-core:69323] (Part 1) + Fri May 29 17:39:14 2015 Koichi Sasada <ko1@atdot.net> * tool/make_hgraph.rb: added. diff --git a/variable.c b/variable.c index a593437540..edb30f45ed 100644 --- a/variable.c +++ b/variable.c @@ -1163,6 +1163,45 @@ rb_attr_get(VALUE obj, ID id) return rb_ivar_lookup(obj, id, Qnil); } +static st_table * +iv_index_tbl_make(VALUE obj) +{ + VALUE klass = rb_obj_class(obj); + st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(klass); + + if (!iv_index_tbl) { + iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable(); + } + + return iv_index_tbl; +} + +static int +iv_index_tbl_extend(st_table *iv_index_tbl, ID id, st_data_t *index) +{ + if (st_lookup(iv_index_tbl, (st_data_t)id, index)) { + return 0; + } + *index = iv_index_tbl->num_entries; + if (*index >= INT_MAX) { + rb_raise(rb_eArgError, "too many instance variables"); + } + st_add_direct(iv_index_tbl, (st_data_t)id, *index); + return 1; +} + +static long +iv_index_tbl_newsize(st_table *iv_index_tbl, st_data_t index, int ivar_extended) +{ + long newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */ + + if (!ivar_extended && + iv_index_tbl->num_entries < (st_index_t)newsize) { + newsize = iv_index_tbl->num_entries; + } + return newsize; +} + VALUE rb_ivar_set(VALUE obj, ID id, VALUE val) { @@ -1175,21 +1214,8 @@ rb_ivar_set(VALUE obj, ID id, VALUE val) if (SPECIAL_CONST_P(obj)) goto generic; switch (BUILTIN_TYPE(obj)) { case T_OBJECT: - iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj); - if (!iv_index_tbl) { - VALUE klass = rb_obj_class(obj); - iv_index_tbl = RCLASS_IV_INDEX_TBL(klass); - if (!iv_index_tbl) { - iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable(); - } - } - ivar_extended = 0; - if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) { - index = iv_index_tbl->num_entries; - if (index >= INT_MAX) rb_raise(rb_eArgError, "too many instance variables"); - st_add_direct(iv_index_tbl, (st_data_t)id, index); - ivar_extended = 1; - } + iv_index_tbl = iv_index_tbl_make(obj); + ivar_extended = iv_index_tbl_extend(iv_index_tbl, id, &index); len = ROBJECT_NUMIV(obj); if (len <= (long)index) { VALUE *ptr = ROBJECT_IVPTR(obj); @@ -1202,11 +1228,8 @@ rb_ivar_set(VALUE obj, ID id, VALUE val) } else { VALUE *newptr; - long newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */ - if (!ivar_extended && - iv_index_tbl->num_entries < (st_index_t)newsize) { - newsize = iv_index_tbl->num_entries; - } + long newsize = iv_index_tbl_newsize(iv_index_tbl, index, + ivar_extended); if (RBASIC(obj)->flags & ROBJECT_EMBED) { newptr = ALLOC_N(VALUE, newsize); |