Skip to content

Commit

Permalink
* include/ruby/ruby.h (struct RClass): Add wrapper struct around
Browse files Browse the repository at this point in the history
  RClass->m_tbl with serial. This prevents double marking method
  tables, since many classes/modules can share the same method table.
  This improves minor mark time in a large application by 30%.
* internal.h (struct method_table_wrapper): Define new
  wrapper struct with additional serial.
* internal.h (RCLASS_M_TBL_INIT): New macro for initializing method
  table wrapper and st_table.
* method.h (void rb_sweep_method_entry): Rename rb_free_m_table to
  rb_free_m_tbl for consistentcy
* .gdbinit (define rb_method_entry): Update rb_method_entry gdb helper
  for new method table structure.
* class.c: Use RCLASS_M_TBL_WRAPPER and
  RCLASS_M_TBL_INIT macros.
* class.c (rb_include_class_new): Share WRAPPER between module and
  iclass, so serial can prevent double marking.
* eval.c (rb_prepend_module): ditto.
* eval.c (rb_using_refinement): ditto.
* gc.c: Mark and free new wrapper struct.
* gc.c (obj_memsize_of): Count size of additional wrapper struct.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
tmm1 committed Dec 3, 2013
1 parent ee7fa8b commit 084b602
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .gdbinit
Expand Up @@ -757,7 +757,7 @@ define rb_method_entry
set $rb_method_entry_id = (ID)$arg1
set $rb_method_entry_me = (rb_method_entry_t *)0
while !$rb_method_entry_me && $rb_method_entry_klass
rb_numtable_entry $rb_method_entry_klass->m_tbl $rb_method_entry_id
rb_numtable_entry $rb_method_entry_klass->m_tbl_wrapper->tbl $rb_method_entry_id
set $rb_method_entry_me = (rb_method_entry_t *)$rb_numtable_rec
if !$rb_method_entry_me
set $rb_method_entry_klass = (struct RClass *)$rb_method_entry_klass->ptr->super
Expand Down
23 changes: 23 additions & 0 deletions ChangeLog
@@ -1,3 +1,26 @@
Tue Dec 3 17:01:45 2013 Aman Gupta <ruby@tmm1.net>

* include/ruby/ruby.h (struct RClass): Add wrapper struct around
RClass->m_tbl with serial. This prevents double marking method
tables, since many classes/modules can share the same method table.
This improves minor mark time in a large application by 30%.
* internal.h (struct method_table_wrapper): Define new
wrapper struct with additional serial.
* internal.h (RCLASS_M_TBL_INIT): New macro for initializing method
table wrapper and st_table.
* method.h (void rb_sweep_method_entry): Rename rb_free_m_table to
rb_free_m_tbl for consistentcy
* .gdbinit (define rb_method_entry): Update rb_method_entry gdb helper
for new method table structure.
* class.c: Use RCLASS_M_TBL_WRAPPER and
RCLASS_M_TBL_INIT macros.
* class.c (rb_include_class_new): Share WRAPPER between module and
iclass, so serial can prevent double marking.
* eval.c (rb_prepend_module): ditto.
* eval.c (rb_using_refinement): ditto.
* gc.c: Mark and free new wrapper struct.
* gc.c (obj_memsize_of): Count size of additional wrapper struct.

Tue Dec 3 14:05:49 2013 Masaki Matsushita <glass.saga@gmail.com>

* array.c (rb_ary_uniq_bang): remove duplicate code.
Expand Down
23 changes: 11 additions & 12 deletions class.c
Expand Up @@ -158,7 +158,7 @@ class_alloc(VALUE flags, VALUE klass)
obj->ptr = ALLOC(rb_classext_t);
RCLASS_IV_TBL(obj) = 0;
RCLASS_CONST_TBL(obj) = 0;
RCLASS_M_TBL(obj) = 0;
RCLASS_M_TBL_WRAPPER(obj) = 0;
RCLASS_SET_SUPER((VALUE)obj, 0);
RCLASS_ORIGIN(obj) = (VALUE)obj;
RCLASS_IV_INDEX_TBL(obj) = 0;
Expand Down Expand Up @@ -189,7 +189,7 @@ rb_class_boot(VALUE super)
VALUE klass = class_alloc(T_CLASS, rb_cClass);

RCLASS_SET_SUPER(klass, super);
RCLASS_M_TBL(klass) = st_init_numtable();
RCLASS_M_TBL_INIT(klass);

OBJ_INFECT(klass, super);
return (VALUE)klass;
Expand Down Expand Up @@ -353,10 +353,10 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)&arg);
}
if (RCLASS_M_TBL(orig)) {
if (RCLASS_M_TBL(clone)) {
rb_free_m_table(RCLASS_M_TBL(clone));
if (RCLASS_M_TBL_WRAPPER(clone)) {
rb_free_m_tbl_wrapper(RCLASS_M_TBL_WRAPPER(clone));
}
RCLASS_M_TBL(clone) = st_init_numtable();
RCLASS_M_TBL_INIT(clone);
st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
}

Expand Down Expand Up @@ -402,7 +402,7 @@ rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
if (attach != Qundef) {
rb_singleton_class_attached(clone, attach);
}
RCLASS_M_TBL(clone) = st_init_numtable();
RCLASS_M_TBL_INIT(clone);
st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
rb_singleton_class_attached(RBASIC(clone)->klass, clone);
FL_SET(clone, FL_SINGLETON);
Expand Down Expand Up @@ -723,9 +723,7 @@ VALUE
rb_module_new(void)
{
VALUE mdl = class_alloc(T_MODULE, rb_cModule);

RCLASS_M_TBL(mdl) = st_init_numtable();

RCLASS_M_TBL_INIT(mdl);
return (VALUE)mdl;
}

Expand Down Expand Up @@ -803,7 +801,8 @@ rb_include_class_new(VALUE module, VALUE super)
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);

RCLASS_M_TBL(OBJ_WB_UNPROTECT(klass)) = RCLASS_M_TBL(OBJ_WB_UNPROTECT(RCLASS_ORIGIN(module)));
RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(klass)) =
RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(RCLASS_ORIGIN(module)));

RCLASS_SET_SUPER(klass, super);
if (RB_TYPE_P(module, T_ICLASS)) {
Expand Down Expand Up @@ -953,8 +952,8 @@ rb_prepend_module(VALUE klass, VALUE module)
RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
RCLASS_SET_SUPER(klass, origin);
RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
RCLASS_M_TBL(klass) = st_init_numtable();
RCLASS_M_TBL_WRAPPER(origin) = RCLASS_M_TBL_WRAPPER(klass);
RCLASS_M_TBL_INIT(klass);
st_foreach(RCLASS_M_TBL(origin), move_refined_method,
(st_data_t) RCLASS_M_TBL(klass));
}
Expand Down
3 changes: 2 additions & 1 deletion eval.c
Expand Up @@ -1119,7 +1119,8 @@ rb_using_refinement(NODE *cref, VALUE klass, VALUE module)
c = iclass = rb_include_class_new(module, superclass);
RCLASS_REFINED_CLASS(c) = klass;

RCLASS_M_TBL(OBJ_WB_UNPROTECT(c)) = RCLASS_M_TBL(OBJ_WB_UNPROTECT(module));
RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(c)) =
RCLASS_M_TBL_WRAPPER(OBJ_WB_UNPROTECT(module));

module = RCLASS_SUPER(module);
while (module && module != klass) {
Expand Down
33 changes: 26 additions & 7 deletions gc.c
Expand Up @@ -1414,12 +1414,21 @@ free_method_entry_i(ID key, rb_method_entry_t *me, st_data_t data)
}

void
rb_free_m_table(st_table *tbl)
rb_free_m_tbl(st_table *tbl)
{
st_foreach(tbl, free_method_entry_i, 0);
st_free_table(tbl);
}

void
rb_free_m_tbl_wrapper(struct method_table_wrapper *wrapper)
{
if (wrapper->tbl) {
rb_free_m_tbl(wrapper->tbl);
}
xfree(wrapper);
}

static int
free_const_entry_i(ID key, rb_const_entry_t *ce, st_data_t data)
{
Expand Down Expand Up @@ -1484,8 +1493,8 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
break;
case T_MODULE:
case T_CLASS:
if (RCLASS_M_TBL(obj)) {
rb_free_m_table(RCLASS_M_TBL(obj));
if (RCLASS_M_TBL_WRAPPER(obj)) {
rb_free_m_tbl_wrapper(RCLASS_M_TBL_WRAPPER(obj));
}
if (RCLASS_IV_TBL(obj)) {
st_free_table(RCLASS_IV_TBL(obj));
Expand Down Expand Up @@ -2393,6 +2402,9 @@ obj_memsize_of(VALUE obj, int use_tdata)
break;
case T_MODULE:
case T_CLASS:
if (RCLASS_M_TBL_WRAPPER(obj)) {
size += sizeof(struct method_table_wrapper);
}
if (RCLASS_M_TBL(obj)) {
size += st_memsize(RCLASS_M_TBL(obj));
}
Expand Down Expand Up @@ -3368,12 +3380,19 @@ mark_method_entry_i(ID key, const rb_method_entry_t *me, st_data_t data)
}

static void
mark_m_tbl(rb_objspace_t *objspace, st_table *tbl)
mark_m_tbl_wrapper(rb_objspace_t *objspace, struct method_table_wrapper *wrapper)
{
struct mark_tbl_arg arg;
if (!tbl) return;
if (!wrapper || !wrapper->tbl) return;
if (LIKELY(objspace->mark_func_data == 0)) {
/* prevent multiple marking during same GC cycle,
* since m_tbl is shared between several T_ICLASS */
size_t serial = rb_gc_count();
if (wrapper->serial == serial) return;
wrapper->serial = serial;
}
arg.objspace = objspace;
st_foreach(tbl, mark_method_entry_i, (st_data_t)&arg);
st_foreach(wrapper->tbl, mark_method_entry_i, (st_data_t)&arg);
}

static int
Expand Down Expand Up @@ -3787,7 +3806,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr)
case T_ICLASS:
case T_CLASS:
case T_MODULE:
mark_m_tbl(objspace, RCLASS_M_TBL(obj));
mark_m_tbl_wrapper(objspace, RCLASS_M_TBL_WRAPPER(obj));
if (!RCLASS_EXT(obj)) break;
mark_tbl(objspace, RCLASS_IV_TBL(obj));
mark_const_tbl(objspace, RCLASS_CONST_TBL(obj));
Expand Down
2 changes: 1 addition & 1 deletion include/ruby/ruby.h
Expand Up @@ -787,7 +787,7 @@ typedef struct rb_classext_struct rb_classext_t;
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct method_table_wrapper *m_tbl_wrapper;
struct st_table *iv_index_tbl;
};
#define RCLASS_SUPER(c) rb_class_get_superclass(c)
Expand Down
18 changes: 17 additions & 1 deletion internal.h
Expand Up @@ -276,18 +276,34 @@ struct rb_classext_struct {
rb_alloc_func_t allocator;
};

struct method_table_wrapper {
st_table *tbl;
size_t serial;
};

/* class.c */
void rb_class_subclass_add(VALUE super, VALUE klass);
void rb_class_remove_from_super_subclasses(VALUE);

#define RCLASS_EXT(c) (RCLASS(c)->ptr)
#define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl)
#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
#define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
#define RCLASS_M_TBL_WRAPPER(c) (RCLASS(c)->m_tbl_wrapper)
#define RCLASS_M_TBL(c) (RCLASS_M_TBL_WRAPPER(c) ? RCLASS_M_TBL_WRAPPER(c)->tbl : 0)
#define RCLASS_IV_INDEX_TBL(c) (RCLASS(c)->iv_index_tbl)
#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin)
#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)

static inline void
RCLASS_M_TBL_INIT(VALUE c)
{
struct method_table_wrapper *wrapper;
wrapper = ALLOC(struct method_table_wrapper);
wrapper->tbl = st_init_numtable();
wrapper->serial = 0;
RCLASS_M_TBL_WRAPPER(c) = wrapper;
}

#undef RCLASS_SUPER
static inline VALUE
RCLASS_SUPER(VALUE klass)
Expand Down
3 changes: 2 additions & 1 deletion method.h
Expand Up @@ -136,6 +136,7 @@ VALUE rb_obj_method_location(VALUE obj, ID id);
void rb_mark_method_entry(const rb_method_entry_t *me);
void rb_free_method_entry(rb_method_entry_t *me);
void rb_sweep_method_entry(void *vm);
void rb_free_m_table(st_table *tbl);
void rb_free_m_tbl(st_table *tbl);
void rb_free_m_tbl_wrapper(struct method_table_wrapper *wrapper);

#endif /* METHOD_H */

0 comments on commit 084b602

Please sign in to comment.