* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tmm1 2013-12-03 08:11:07 +00:00
parent ee7fa8b227
commit 084b602d9a
8 changed files with 83 additions and 24 deletions

View File

@ -757,7 +757,7 @@ define rb_method_entry
set $rb_method_entry_id = (ID)$arg1 set $rb_method_entry_id = (ID)$arg1
set $rb_method_entry_me = (rb_method_entry_t *)0 set $rb_method_entry_me = (rb_method_entry_t *)0
while !$rb_method_entry_me && $rb_method_entry_klass 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 set $rb_method_entry_me = (rb_method_entry_t *)$rb_numtable_rec
if !$rb_method_entry_me if !$rb_method_entry_me
set $rb_method_entry_klass = (struct RClass *)$rb_method_entry_klass->ptr->super set $rb_method_entry_klass = (struct RClass *)$rb_method_entry_klass->ptr->super

View File

@ -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> Tue Dec 3 14:05:49 2013 Masaki Matsushita <glass.saga@gmail.com>
* array.c (rb_ary_uniq_bang): remove duplicate code. * array.c (rb_ary_uniq_bang): remove duplicate code.

23
class.c
View File

@ -158,7 +158,7 @@ class_alloc(VALUE flags, VALUE klass)
obj->ptr = ALLOC(rb_classext_t); obj->ptr = ALLOC(rb_classext_t);
RCLASS_IV_TBL(obj) = 0; RCLASS_IV_TBL(obj) = 0;
RCLASS_CONST_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_SET_SUPER((VALUE)obj, 0);
RCLASS_ORIGIN(obj) = (VALUE)obj; RCLASS_ORIGIN(obj) = (VALUE)obj;
RCLASS_IV_INDEX_TBL(obj) = 0; RCLASS_IV_INDEX_TBL(obj) = 0;
@ -189,7 +189,7 @@ rb_class_boot(VALUE super)
VALUE klass = class_alloc(T_CLASS, rb_cClass); VALUE klass = class_alloc(T_CLASS, rb_cClass);
RCLASS_SET_SUPER(klass, super); RCLASS_SET_SUPER(klass, super);
RCLASS_M_TBL(klass) = st_init_numtable(); RCLASS_M_TBL_INIT(klass);
OBJ_INFECT(klass, super); OBJ_INFECT(klass, super);
return (VALUE)klass; return (VALUE)klass;
@ -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); st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)&arg);
} }
if (RCLASS_M_TBL(orig)) { if (RCLASS_M_TBL(orig)) {
if (RCLASS_M_TBL(clone)) { if (RCLASS_M_TBL_WRAPPER(clone)) {
rb_free_m_table(RCLASS_M_TBL(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); st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
} }
@ -402,7 +402,7 @@ rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
if (attach != Qundef) { if (attach != Qundef) {
rb_singleton_class_attached(clone, attach); 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); st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
rb_singleton_class_attached(RBASIC(clone)->klass, clone); rb_singleton_class_attached(RBASIC(clone)->klass, clone);
FL_SET(clone, FL_SINGLETON); FL_SET(clone, FL_SINGLETON);
@ -723,9 +723,7 @@ VALUE
rb_module_new(void) rb_module_new(void)
{ {
VALUE mdl = class_alloc(T_MODULE, rb_cModule); VALUE mdl = class_alloc(T_MODULE, rb_cModule);
RCLASS_M_TBL_INIT(mdl);
RCLASS_M_TBL(mdl) = st_init_numtable();
return (VALUE)mdl; return (VALUE)mdl;
} }
@ -803,7 +801,8 @@ rb_include_class_new(VALUE module, VALUE super)
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module); RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
RCLASS_CONST_TBL(klass) = RCLASS_CONST_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); RCLASS_SET_SUPER(klass, super);
if (RB_TYPE_P(module, T_ICLASS)) { if (RB_TYPE_P(module, T_ICLASS)) {
@ -953,8 +952,8 @@ rb_prepend_module(VALUE klass, VALUE module)
RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass)); RCLASS_SET_SUPER(origin, RCLASS_SUPER(klass));
RCLASS_SET_SUPER(klass, origin); RCLASS_SET_SUPER(klass, origin);
RCLASS_ORIGIN(klass) = origin; RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass); RCLASS_M_TBL_WRAPPER(origin) = RCLASS_M_TBL_WRAPPER(klass);
RCLASS_M_TBL(klass) = st_init_numtable(); RCLASS_M_TBL_INIT(klass);
st_foreach(RCLASS_M_TBL(origin), move_refined_method, st_foreach(RCLASS_M_TBL(origin), move_refined_method,
(st_data_t) RCLASS_M_TBL(klass)); (st_data_t) RCLASS_M_TBL(klass));
} }

3
eval.c
View File

@ -1119,7 +1119,8 @@ rb_using_refinement(NODE *cref, VALUE klass, VALUE module)
c = iclass = rb_include_class_new(module, superclass); c = iclass = rb_include_class_new(module, superclass);
RCLASS_REFINED_CLASS(c) = klass; 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); module = RCLASS_SUPER(module);
while (module && module != klass) { while (module && module != klass) {

33
gc.c
View File

@ -1414,12 +1414,21 @@ free_method_entry_i(ID key, rb_method_entry_t *me, st_data_t data)
} }
void void
rb_free_m_table(st_table *tbl) rb_free_m_tbl(st_table *tbl)
{ {
st_foreach(tbl, free_method_entry_i, 0); st_foreach(tbl, free_method_entry_i, 0);
st_free_table(tbl); 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 static int
free_const_entry_i(ID key, rb_const_entry_t *ce, st_data_t data) free_const_entry_i(ID key, rb_const_entry_t *ce, st_data_t data)
{ {
@ -1484,8 +1493,8 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
break; break;
case T_MODULE: case T_MODULE:
case T_CLASS: case T_CLASS:
if (RCLASS_M_TBL(obj)) { if (RCLASS_M_TBL_WRAPPER(obj)) {
rb_free_m_table(RCLASS_M_TBL(obj)); rb_free_m_tbl_wrapper(RCLASS_M_TBL_WRAPPER(obj));
} }
if (RCLASS_IV_TBL(obj)) { if (RCLASS_IV_TBL(obj)) {
st_free_table(RCLASS_IV_TBL(obj)); st_free_table(RCLASS_IV_TBL(obj));
@ -2393,6 +2402,9 @@ obj_memsize_of(VALUE obj, int use_tdata)
break; break;
case T_MODULE: case T_MODULE:
case T_CLASS: case T_CLASS:
if (RCLASS_M_TBL_WRAPPER(obj)) {
size += sizeof(struct method_table_wrapper);
}
if (RCLASS_M_TBL(obj)) { if (RCLASS_M_TBL(obj)) {
size += st_memsize(RCLASS_M_TBL(obj)); size += st_memsize(RCLASS_M_TBL(obj));
} }
@ -3368,12 +3380,19 @@ mark_method_entry_i(ID key, const rb_method_entry_t *me, st_data_t data)
} }
static void 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; 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; 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 static int
@ -3787,7 +3806,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr)
case T_ICLASS: case T_ICLASS:
case T_CLASS: case T_CLASS:
case T_MODULE: 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; if (!RCLASS_EXT(obj)) break;
mark_tbl(objspace, RCLASS_IV_TBL(obj)); mark_tbl(objspace, RCLASS_IV_TBL(obj));
mark_const_tbl(objspace, RCLASS_CONST_TBL(obj)); mark_const_tbl(objspace, RCLASS_CONST_TBL(obj));

View File

@ -787,7 +787,7 @@ typedef struct rb_classext_struct rb_classext_t;
struct RClass { struct RClass {
struct RBasic basic; struct RBasic basic;
rb_classext_t *ptr; rb_classext_t *ptr;
struct st_table *m_tbl; struct method_table_wrapper *m_tbl_wrapper;
struct st_table *iv_index_tbl; struct st_table *iv_index_tbl;
}; };
#define RCLASS_SUPER(c) rb_class_get_superclass(c) #define RCLASS_SUPER(c) rb_class_get_superclass(c)

View File

@ -276,6 +276,11 @@ struct rb_classext_struct {
rb_alloc_func_t allocator; rb_alloc_func_t allocator;
}; };
struct method_table_wrapper {
st_table *tbl;
size_t serial;
};
/* class.c */ /* class.c */
void rb_class_subclass_add(VALUE super, VALUE klass); void rb_class_subclass_add(VALUE super, VALUE klass);
void rb_class_remove_from_super_subclasses(VALUE); void rb_class_remove_from_super_subclasses(VALUE);
@ -283,11 +288,22 @@ void rb_class_remove_from_super_subclasses(VALUE);
#define RCLASS_EXT(c) (RCLASS(c)->ptr) #define RCLASS_EXT(c) (RCLASS(c)->ptr)
#define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl) #define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl)
#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_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_IV_INDEX_TBL(c) (RCLASS(c)->iv_index_tbl)
#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin) #define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin)
#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class) #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 #undef RCLASS_SUPER
static inline VALUE static inline VALUE
RCLASS_SUPER(VALUE klass) RCLASS_SUPER(VALUE klass)

View File

@ -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_mark_method_entry(const rb_method_entry_t *me);
void rb_free_method_entry(rb_method_entry_t *me); void rb_free_method_entry(rb_method_entry_t *me);
void rb_sweep_method_entry(void *vm); 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 */ #endif /* METHOD_H */