Skip to content

Commit

Permalink
* include/ruby/intern.h: export rb_ivar_foreach.
Browse files Browse the repository at this point in the history
* include/ruby/ruby.h: modify struct RObject and RClass for optimizing
  T_OBJECT space.  [ruby-dev:31853]
  (ROBJECT_LEN, ROBJECT_PTR)
  (RCLASS_IV_TBL, RCLASS_M_TBL, RCLASS_SUPER, RCLASS_IV_INDEX_TBL)
  (RMODULE_IV_TBL, RMODULE_M_TBL, RMODULE_SUPER): abstract accessor
  defined.

* variable.c: support the modified RObject and RClass.

* object.c: ditto.

* class.c: ditto.

* gc.c: ditto.

* marshal.c: ditto.

* eval_method.ci: use the abstract accessor.

* insns.def: ditto.

* proc.c: ditto.

* struct.c: ditto.

* eval.c: ditto.

* error.c: ditto.

* vm.c: ditto.

* insnhelper.ci: ditto.

* ext/digest/digest.c: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13543 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
akr committed Sep 28, 2007
1 parent 041fbcb commit 5c0e68c
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 193 deletions.
39 changes: 39 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
Fri Sep 28 15:05:24 2007 Tanaka Akira <akr@fsij.org>

* include/ruby/intern.h: export rb_ivar_foreach.

* include/ruby/ruby.h: modify struct RObject and RClass for optimizing
T_OBJECT space. [ruby-dev:31853]
(ROBJECT_LEN, ROBJECT_PTR)
(RCLASS_IV_TBL, RCLASS_M_TBL, RCLASS_SUPER, RCLASS_IV_INDEX_TBL)
(RMODULE_IV_TBL, RMODULE_M_TBL, RMODULE_SUPER): abstract accessor
defined.

* variable.c: support the modified RObject and RClass.

* object.c: ditto.

* class.c: ditto.

* gc.c: ditto.

* marshal.c: ditto.

* eval_method.ci: use the abstract accessor.

* insns.def: ditto.

* proc.c: ditto.

* struct.c: ditto.

* eval.c: ditto.

* error.c: ditto.

* vm.c: ditto.

* insnhelper.ci: ditto.

* ext/digest/digest.c: ditto.

Fri Sep 28 13:20:10 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>

* io.c (rb_io_getline_fast, rb_io_getline_1): set encoding to the
Expand Down
117 changes: 60 additions & 57 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@

extern st_table *rb_class_tbl;

static VALUE
class_alloc(VALUE flags, VALUE klass)
{
rb_classext_t *ext = ALLOC(rb_classext_t);
NEWOBJ(obj, struct RClass);
OBJSETUP(obj, klass, flags);
obj->ptr = ext;
RCLASS_IV_TBL(obj) = 0;
RCLASS_M_TBL(obj) = 0;
RCLASS_SUPER(obj) = 0;
RCLASS_IV_INDEX_TBL(obj) = 0;
return (VALUE)obj;
}

VALUE
rb_class_boot(VALUE super)
{
NEWOBJ(klass, struct RClass);
OBJSETUP(klass, rb_cClass, T_CLASS);
VALUE klass = class_alloc(T_CLASS, rb_cClass);

klass->super = super;
klass->iv_tbl = 0;
klass->m_tbl = 0; /* safe GC */
klass->m_tbl = st_init_numtable();
RCLASS_SUPER(klass) = super;
RCLASS_M_TBL(klass) = st_init_numtable();

OBJ_INFECT(klass, super);
return (VALUE)klass;
Expand Down Expand Up @@ -87,21 +98,21 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
RBASIC(clone)->klass = rb_singleton_class_clone(orig);
}
RCLASS(clone)->super = RCLASS(orig)->super;
if (RCLASS(orig)->iv_tbl) {
RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
if (RCLASS_IV_TBL(orig)) {
ID id;

RCLASS(clone)->iv_tbl = st_copy(RCLASS(orig)->iv_tbl);
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
id = rb_intern("__classpath__");
st_delete(RCLASS(clone)->iv_tbl, (st_data_t*)&id, 0);
st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
id = rb_intern("__classid__");
st_delete(RCLASS(clone)->iv_tbl, (st_data_t*)&id, 0);
st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
}
if (RCLASS(orig)->m_tbl) {
if (RCLASS_M_TBL(orig)) {
struct clone_method_data data;
data.tbl = RCLASS(clone)->m_tbl = st_init_numtable();
data.tbl = RCLASS_M_TBL(clone) = st_init_numtable();
data.klass = clone;
st_foreach(RCLASS(orig)->m_tbl, clone_method,
st_foreach(RCLASS_M_TBL(orig), clone_method,
(st_data_t)&data);
}

Expand All @@ -112,7 +123,7 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
VALUE
rb_class_init_copy(VALUE clone, VALUE orig)
{
if (RCLASS(clone)->super != 0) {
if (RCLASS_SUPER(clone) != 0) {
rb_raise(rb_eTypeError, "already initialized class");
}
if (FL_TEST(orig, FL_SINGLETON)) {
Expand All @@ -131,8 +142,7 @@ rb_singleton_class_clone(VALUE obj)
else {
struct clone_method_data data;
/* copy singleton(unnamed) class */
NEWOBJ(clone, struct RClass);
OBJSETUP(clone, 0, RBASIC(klass)->flags);
VALUE clone = class_alloc(RBASIC(klass)->flags, 0);

if (BUILTIN_TYPE(obj) == T_CLASS) {
RBASIC(clone)->klass = (VALUE)clone;
Expand All @@ -141,16 +151,14 @@ rb_singleton_class_clone(VALUE obj)
RBASIC(clone)->klass = rb_singleton_class_clone(klass);
}

clone->super = RCLASS(klass)->super;
clone->iv_tbl = 0;
clone->m_tbl = 0;
if (RCLASS(klass)->iv_tbl) {
clone->iv_tbl = st_copy(RCLASS(klass)->iv_tbl);
RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
if (RCLASS_IV_TBL(klass)) {
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
}
clone->m_tbl = st_init_numtable();
data.tbl = clone->m_tbl;
RCLASS_M_TBL(clone) = st_init_numtable();
data.tbl = RCLASS_M_TBL(clone);
data.klass = (VALUE)clone;
st_foreach(RCLASS(klass)->m_tbl, clone_method,
st_foreach(RCLASS_M_TBL(klass), clone_method,
(st_data_t)&data);
rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
FL_SET(clone, FL_SINGLETON);
Expand All @@ -162,10 +170,10 @@ void
rb_singleton_class_attached(VALUE klass, VALUE obj)
{
if (FL_TEST(klass, FL_SINGLETON)) {
if (!RCLASS(klass)->iv_tbl) {
RCLASS(klass)->iv_tbl = st_init_numtable();
if (!RCLASS_IV_TBL(klass)) {
RCLASS_IV_TBL(klass) = st_init_numtable();
}
st_insert(RCLASS(klass)->iv_tbl, rb_intern("__attached__"), obj);
st_insert(RCLASS_IV_TBL(klass), rb_intern("__attached__"), obj);
}
}

Expand Down Expand Up @@ -223,7 +231,7 @@ rb_define_class(const char *name, VALUE super)
if (TYPE(klass) != T_CLASS) {
rb_raise(rb_eTypeError, "%s is not a class", name);
}
if (rb_class_real(RCLASS(klass)->super) != super) {
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
rb_name_error(id, "%s is already defined", name);
}
return klass;
Expand Down Expand Up @@ -252,7 +260,7 @@ rb_define_class_under(VALUE outer, const char *name, VALUE super)
if (TYPE(klass) != T_CLASS) {
rb_raise(rb_eTypeError, "%s is not a class", name);
}
if (rb_class_real(RCLASS(klass)->super) != super) {
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
rb_name_error(id, "%s is already defined", name);
}
return klass;
Expand All @@ -272,13 +280,9 @@ rb_define_class_under(VALUE outer, const char *name, VALUE super)
VALUE
rb_module_new(void)
{
NEWOBJ(mdl, struct RClass);
OBJSETUP(mdl, rb_cModule, T_MODULE);
VALUE mdl = class_alloc(T_MODULE, rb_cModule);

mdl->super = 0;
mdl->iv_tbl = 0;
mdl->m_tbl = 0;
mdl->m_tbl = st_init_numtable();
RCLASS_M_TBL(mdl) = st_init_numtable();

return (VALUE)mdl;
}
Expand Down Expand Up @@ -338,18 +342,17 @@ rb_define_module_under(VALUE outer, const char *name)
static VALUE
include_class_new(VALUE module, VALUE super)
{
NEWOBJ(klass, struct RClass);
OBJSETUP(klass, rb_cClass, T_ICLASS);
VALUE klass = class_alloc(T_ICLASS, rb_cClass);

if (BUILTIN_TYPE(module) == T_ICLASS) {
module = RBASIC(module)->klass;
}
if (!RCLASS(module)->iv_tbl) {
RCLASS(module)->iv_tbl = st_init_numtable();
if (!RCLASS_IV_TBL(module)) {
RCLASS_IV_TBL(module) = st_init_numtable();
}
klass->iv_tbl = RCLASS(module)->iv_tbl;
klass->m_tbl = RCLASS(module)->m_tbl;
klass->super = super;
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
RCLASS_SUPER(klass) = super;
if (TYPE(module) == T_ICLASS) {
RBASIC(klass)->klass = RBASIC(module)->klass;
}
Expand Down Expand Up @@ -382,13 +385,13 @@ rb_include_module(VALUE klass, VALUE module)
while (module) {
int superclass_seen = Qfalse;

if (RCLASS(klass)->m_tbl == RCLASS(module)->m_tbl)
if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
rb_raise(rb_eArgError, "cyclic include detected");
/* ignore if the module included already in superclasses */
for (p = RCLASS(klass)->super; p; p = RCLASS(p)->super) {
for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
switch (BUILTIN_TYPE(p)) {
case T_ICLASS:
if (RCLASS(p)->m_tbl == RCLASS(module)->m_tbl) {
if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
if (!superclass_seen) {
c = p; /* move insertion point */
}
Expand All @@ -400,10 +403,10 @@ rb_include_module(VALUE klass, VALUE module)
break;
}
}
c = RCLASS(c)->super = include_class_new(module, RCLASS(c)->super);
c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
changed = 1;
skip:
module = RCLASS(module)->super;
module = RCLASS_SUPER(module);
}
if (changed) rb_clear_cache();
}
Expand Down Expand Up @@ -431,7 +434,7 @@ rb_mod_included_modules(VALUE mod)
VALUE ary = rb_ary_new();
VALUE p;

for (p = RCLASS(mod)->super; p; p = RCLASS(p)->super) {
for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
if (BUILTIN_TYPE(p) == T_ICLASS) {
rb_ary_push(ary, RBASIC(p)->klass);
}
Expand Down Expand Up @@ -464,7 +467,7 @@ rb_mod_include_p(VALUE mod, VALUE mod2)
VALUE p;

Check_Type(mod2, T_MODULE);
for (p = RCLASS(mod)->super; p; p = RCLASS(p)->super) {
for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
if (BUILTIN_TYPE(p) == T_ICLASS) {
if (RBASIC(p)->klass == mod2) return Qtrue;
}
Expand Down Expand Up @@ -493,7 +496,7 @@ rb_mod_ancestors(VALUE mod)
{
VALUE p, ary = rb_ary_new();

for (p = mod; p; p = RCLASS(p)->super) {
for (p = mod; p; p = RCLASS_SUPER(p)) {
if (FL_TEST(p, FL_SINGLETON))
continue;
if (BUILTIN_TYPE(p) == T_ICLASS) {
Expand Down Expand Up @@ -599,8 +602,8 @@ class_instance_method_list(int argc, VALUE *argv, VALUE mod, int (*func) (ID, lo
}

list = st_init_numtable();
for (; mod; mod = RCLASS(mod)->super) {
st_foreach(RCLASS(mod)->m_tbl, method_entry, (st_data_t)list);
for (; mod; mod = RCLASS_SUPER(mod)) {
st_foreach(RCLASS_M_TBL(mod), method_entry, (st_data_t)list);
if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
if (FL_TEST(mod, FL_SINGLETON)) continue;
if (!recur) break;
Expand Down Expand Up @@ -756,13 +759,13 @@ rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
klass = CLASS_OF(obj);
list = st_init_numtable();
if (klass && FL_TEST(klass, FL_SINGLETON)) {
st_foreach(RCLASS(klass)->m_tbl, method_entry, (st_data_t)list);
klass = RCLASS(klass)->super;
st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list);
klass = RCLASS_SUPER(klass);
}
if (RTEST(recur)) {
while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
st_foreach(RCLASS(klass)->m_tbl, method_entry, (st_data_t)list);
klass = RCLASS(klass)->super;
st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list);
klass = RCLASS_SUPER(klass);
}
}
ary = rb_ary_new();
Expand Down
2 changes: 1 addition & 1 deletion error.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ syserr_eqq(VALUE self, VALUE exc)
VALUE klass = CLASS_OF(exc);

while (TYPE(klass) == T_ICLASS || FL_TEST(klass, FL_SINGLETON)) {
klass = (VALUE)RCLASS(klass)->super;
klass = (VALUE)RCLASS_SUPER(klass);
}
num = rb_const_get(klass, rb_intern("Errno"));
}
Expand Down
2 changes: 1 addition & 1 deletion eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2303,7 +2303,7 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
if (nd_type(fbody->nd_body->nd_body) != NODE_ZSUPER) {
break; /* normal case: need not to follow 'super' link */
}
m = RCLASS(m)->super;
m = RCLASS_SUPER(m);
if (!m)
break;
}
Expand Down
16 changes: 8 additions & 8 deletions eval_method.ci
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
st_data_t data;
NODE *old_node;

if (st_lookup(RCLASS(klass)->m_tbl, mid, &data)) {
if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
old_node = (NODE *)data;
if (old_node) {
if (nd_type(old_node->nd_body->nd_body) == NODE_CFUNC) {
Expand All @@ -166,7 +166,7 @@ rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
}
}

st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t) body);
st_insert(RCLASS_M_TBL(klass), mid, (st_data_t) body);

if (node && mid != ID_ALLOCATOR && ruby_running) {
if (FL_TEST(klass, FL_SINGLETON)) {
Expand Down Expand Up @@ -216,8 +216,8 @@ search_method(VALUE klass, ID id, VALUE *klassp)
return 0;
}

while (!st_lookup(RCLASS(klass)->m_tbl, id, &body)) {
klass = RCLASS(klass)->super;
while (!st_lookup(RCLASS_M_TBL(klass), id, &body)) {
klass = RCLASS_SUPER(klass);
if (!klass)
return 0;
}
Expand Down Expand Up @@ -305,11 +305,11 @@ remove_method(VALUE klass, ID mid)
if (mid == object_id || mid == __send || mid == __send_bang || mid == init) {
rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
}
if (st_lookup(RCLASS(klass)->m_tbl, mid, &data)) {
if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
body = (NODE *)data;
if (!body || !body->nd_body) body = 0;
else {
st_delete(RCLASS(klass)->m_tbl, &mid, &data);
st_delete(RCLASS_M_TBL(klass), &mid, &data);
}
}
if (!body) {
Expand Down Expand Up @@ -583,7 +583,7 @@ rb_alias(VALUE klass, ID name, ID def)

orig_fbody->nd_cnt++;

if (st_lookup(RCLASS(klass)->m_tbl, name, &data)) {
if (st_lookup(RCLASS_M_TBL(klass), name, &data)) {
node = (NODE *)data;
if (node) {
if (RTEST(ruby_verbose) && node->nd_cnt == 0 && node->nd_body) {
Expand All @@ -595,7 +595,7 @@ rb_alias(VALUE klass, ID name, ID def)
}
}

st_insert(RCLASS(klass)->m_tbl, name,
st_insert(RCLASS_M_TBL(klass), name,
(st_data_t) NEW_FBODY(
NEW_METHOD(orig_fbody->nd_body->nd_body,
orig_fbody->nd_body->nd_clss,
Expand Down
2 changes: 1 addition & 1 deletion ext/digest/digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ get_digest_base_metadata(VALUE klass)
VALUE obj;
rb_digest_metadata_t *algo;

for (p = klass; p; p = RCLASS(p)->super) {
for (p = klass; p; p = RCLASS_SUPER(p)) {
if (rb_ivar_defined(p, id_metadata)) {
obj = rb_ivar_get(p, id_metadata);
break;
Expand Down
Loading

0 comments on commit 5c0e68c

Please sign in to comment.