ruby-changes:38595
From: normal <ko1@a...>
Date: Sat, 30 May 2015 08:22:59 +0900 (JST)
Subject: [ruby-changes:38595] normal:r50676 (trunk): variable.c: extract common functions for generic ivar indices
normal 2015-05-30 08:22:40 +0900 (Sat, 30 May 2015) New Revision: 50676 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50676 Log: 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) Modified files: trunk/ChangeLog trunk/variable.c Index: ChangeLog =================================================================== --- ChangeLog (revision 50675) +++ ChangeLog (revision 50676) @@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat May 30 08:10:46 2015 Eric Wong <e@8...> + + * 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@a...> * tool/make_hgraph.rb: added. Index: variable.c =================================================================== --- variable.c (revision 50675) +++ variable.c (revision 50676) @@ -1163,6 +1163,45 @@ rb_attr_get(VALUE obj, ID id) https://github.com/ruby/ruby/blob/trunk/variable.c#L1163 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) https://github.com/ruby/ruby/blob/trunk/variable.c#L1214 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) https://github.com/ruby/ruby/blob/trunk/variable.c#L1228 } 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); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/