avoiding relocations in type registration



Sorry mitch, forgot to cc you. Here is mail I sent to Tim.


Hey Tim,

we discussed this on irc a bit, the static const GTypeInfo structs used
for g_type_register_static cause quite a few relocations, and also, in
90% of the cases, most of the structs is always the same. The cost of
relocations can in most cases alleviated by prelink (though
LD_DEBUG=statistics shows me that prelink is not as reliable as one
might hope, at least on ix86. Jakub tells me this is due to the kernel
mapping the vDSO in an area that conflicts with prelinked libraries...).

Here is a proposal to avoid the use of a static const struct (see the
attached patch):

GType
g_type_register_static_simple (GType             parent_type,
			       const gchar      *type_name,
                               guint16           class_size,
                               GClassInitFunc    class_init,
                               guint16           instance_size,
                               GInstanceInitFunc instance_init,
                               GTypeFlags        flags);


The current trivial implementation fills a GTypeInfo struct on the
stack, but it should be possible to avoid the use of a struct
altogether. When doing this conversion for a single type in GTK+,
I see .text grow by 16 bytes, while .data shrinks by 62, so it
should be an overall win.

Matthias


Index: gobject/gtype.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gtype.c,v
retrieving revision 1.85
diff -u -p -r1.85 gtype.c
--- gobject/gtype.c	1 Dec 2005 16:33:51 -0000	1.85
+++ gobject/gtype.c	3 Apr 2006 17:36:58 -0000
@@ -2204,6 +2204,31 @@ g_type_register_fundamental (GType      
 }
 
 GType
+g_type_register_static_simple (GType             parent_type,
+			       const gchar      *type_name,
+			       guint16           class_size,
+			       GClassInitFunc    class_init,
+			       guint16           instance_size,
+			       GInstanceInitFunc instance_init,
+			       GTypeFlags	 flags)
+{
+  GTypeInfo info;
+
+  info.class_size = class_size;
+  info.base_init = NULL;
+  info.base_finalize = NULL;
+  info.class_init = class_init;
+  info.class_finalize = NULL;
+  info.class_data = NULL;
+  info.instance_size = instance_size;
+  info.n_preallocs = 0;
+  info.instance_init = instance_init;
+  info.value_table = NULL;
+
+  g_type_register_static (parent_type, type_name, &info, flags);
+}
+
+GType
 g_type_register_static (GType            parent_type,
 			const gchar     *type_name,
 			const GTypeInfo *info,
Index: gobject/gtype.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gtype.h,v
retrieving revision 1.63
diff -u -p -r1.63 gtype.h
--- gobject/gtype.h	27 Sep 2005 23:19:16 -0000	1.63
+++ gobject/gtype.h	3 Apr 2006 17:36:58 -0000
@@ -287,6 +287,14 @@ GType g_type_register_static		(GType			 
 					 const gchar		    *type_name,
 					 const GTypeInfo	    *info,
 					 GTypeFlags		     flags);
+GType g_type_register_static_simple     (GType                       parent_type,
+					 const gchar                *type_name,
+					 guint16                     class_size,
+					 GClassInitFunc              class_init,
+					 guint16                     instance_size,
+					 GInstanceInitFunc           instance_init,
+					 GTypeFlags	             flags);
+  
 GType g_type_register_dynamic		(GType			     parent_type,
 					 const gchar		    *type_name,
 					 GTypePlugin		    *plugin,
@@ -362,19 +370,14 @@ type_name##_get_type (void) \
   static GType g_define_type_id = 0; \
   if (G_UNLIKELY (g_define_type_id == 0)) \
     { \
-      static const GTypeInfo g_define_type_info = { \
-        sizeof (TypeName##Class), \
-        (GBaseInitFunc) NULL, \
-        (GBaseFinalizeFunc) NULL, \
-        (GClassInitFunc) type_name##_class_intern_init, \
-        (GClassFinalizeFunc) NULL, \
-        NULL,   /* class_data */ \
-        sizeof (TypeName), \
-        0,      /* n_preallocs */ \
-        (GInstanceInitFunc) type_name##_init, \
-        NULL    /* value_table */ \
-      }; \
-      g_define_type_id = g_type_register_static (TYPE_PARENT, g_intern_static_string (#TypeName), &g_define_type_info, (GTypeFlags) flags); \
+      g_define_type_id = \
+        g_type_register_static_simple (TYPE_PARENT, \
+                                       g_intern_static_string (#TypeName), \
+                                       sizeof (TypeName##Class), \
+                                       type_name##_class_intern_init, \
+                                       sizeof (TypeName), \
+                                       type_name##_init, \
+                                       (GTypeFlags) flags); \
       { CODE ; } \
     } \
   return g_define_type_id; \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]