mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
b5909db06b
commit
ff4b73601b
2 changed files with 51 additions and 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.
|
||||
|
|
63
variable.c
63
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);
|
||||
|
|
Loading…
Reference in a new issue