From 084b602d9a52b62a04d17f65ba1a9b8a767d1e3e Mon Sep 17 00:00:00 2001 From: tmm1 Date: Tue, 3 Dec 2013 08:11:07 +0000 Subject: * 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 --- .gdbinit | 2 +- ChangeLog | 23 +++++++++++++++++++++++ class.c | 23 +++++++++++------------ eval.c | 3 ++- gc.c | 33 ++++++++++++++++++++++++++------- include/ruby/ruby.h | 2 +- internal.h | 18 +++++++++++++++++- method.h | 3 ++- 8 files changed, 83 insertions(+), 24 deletions(-) diff --git a/.gdbinit b/.gdbinit index 78ab191c40..d7113204cf 100644 --- a/.gdbinit +++ b/.gdbinit @@ -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 diff --git a/ChangeLog b/ChangeLog index 176dddbab1..862e889505 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +Tue Dec 3 17:01:45 2013 Aman Gupta + + * 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 * array.c (rb_ary_uniq_bang): remove duplicate code. diff --git a/class.c b/class.c index 2bd6bb33e4..eebb46c33c 100644 --- a/class.c +++ b/class.c @@ -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; @@ -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; @@ -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); } @@ -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); @@ -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; } @@ -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)) { @@ -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)); } diff --git a/eval.c b/eval.c index f3275ada0b..873d549739 100644 --- a/eval.c +++ b/eval.c @@ -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) { diff --git a/gc.c b/gc.c index 8a0151eddc..0e6aad6f14 100644 --- a/gc.c +++ b/gc.c @@ -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) { @@ -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)); @@ -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)); } @@ -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 @@ -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)); diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index bf874c830d..b2ec3d61ff 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -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) diff --git a/internal.h b/internal.h index 43a08a6063..f1c9e94041 100644 --- a/internal.h +++ b/internal.h @@ -276,6 +276,11 @@ 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); @@ -283,11 +288,22 @@ 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) diff --git a/method.h b/method.h index 9604722500..c93e4c39c1 100644 --- a/method.h +++ b/method.h @@ -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 */ -- cgit v1.2.3