[前][次][番号順一覧][スレッド一覧]

ruby-changes:31894

From: tmm1 <ko1@a...>
Date: Tue, 3 Dec 2013 17:11:15 +0900 (JST)
Subject: [ruby-changes:31894] tmm1:r43973 (trunk): * include/ruby/ruby.h (struct RClass): Add wrapper struct around

tmm1	2013-12-03 17:11:07 +0900 (Tue, 03 Dec 2013)

  New Revision: 43973

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43973

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

  Modified files:
    trunk/.gdbinit
    trunk/ChangeLog
    trunk/class.c
    trunk/eval.c
    trunk/gc.c
    trunk/include/ruby/ruby.h
    trunk/internal.h
    trunk/method.h
Index: .gdbinit
===================================================================
--- .gdbinit	(revision 43972)
+++ .gdbinit	(revision 43973)
@@ -757,7 +757,7 @@ define rb_method_entry https://github.com/ruby/ruby/blob/trunk/.gdbinit#L757
   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
Index: method.h
===================================================================
--- method.h	(revision 43972)
+++ method.h	(revision 43973)
@@ -136,6 +136,7 @@ VALUE rb_obj_method_location(VALUE obj, https://github.com/ruby/ruby/blob/trunk/method.h#L136
 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 */
Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 43972)
+++ include/ruby/ruby.h	(revision 43973)
@@ -787,7 +787,7 @@ typedef struct rb_classext_struct rb_cla https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L787
 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)
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43972)
+++ ChangeLog	(revision 43973)
@@ -1,3 +1,26 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Dec  3 17:01:45 2013  Aman Gupta <ruby@t...>
+
+	* 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@g...>
 
 	* array.c (rb_ary_uniq_bang): remove duplicate code.
Index: eval.c
===================================================================
--- eval.c	(revision 43972)
+++ eval.c	(revision 43973)
@@ -1119,7 +1119,8 @@ rb_using_refinement(NODE *cref, VALUE kl https://github.com/ruby/ruby/blob/trunk/eval.c#L1119
     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) {
Index: gc.c
===================================================================
--- gc.c	(revision 43972)
+++ gc.c	(revision 43973)
@@ -1414,12 +1414,21 @@ free_method_entry_i(ID key, rb_method_en https://github.com/ruby/ruby/blob/trunk/gc.c#L1414
 }
 
 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 https://github.com/ruby/ruby/blob/trunk/gc.c#L1493
 	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) https://github.com/ruby/ruby/blob/trunk/gc.c#L2402
 	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_met https://github.com/ruby/ruby/blob/trunk/gc.c#L3380
 }
 
 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 https://github.com/ruby/ruby/blob/trunk/gc.c#L3806
       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));
Index: class.c
===================================================================
--- class.c	(revision 43972)
+++ class.c	(revision 43973)
@@ -158,7 +158,7 @@ class_alloc(VALUE flags, VALUE klass) https://github.com/ruby/ruby/blob/trunk/class.c#L158
     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) https://github.com/ruby/ruby/blob/trunk/class.c#L189
     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 https://github.com/ruby/ruby/blob/trunk/class.c#L353
 	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(VALU https://github.com/ruby/ruby/blob/trunk/class.c#L402
 	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 https://github.com/ruby/ruby/blob/trunk/class.c#L723
 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 https://github.com/ruby/ruby/blob/trunk/class.c#L801
     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 mod https://github.com/ruby/ruby/blob/trunk/class.c#L952
 	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));
     }
Index: internal.h
===================================================================
--- internal.h	(revision 43972)
+++ internal.h	(revision 43973)
@@ -276,6 +276,11 @@ struct rb_classext_struct { https://github.com/ruby/ruby/blob/trunk/internal.h#L276
     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_subclass https://github.com/ruby/ruby/blob/trunk/internal.h#L288
 #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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]