Re: Massive speed improvement in GObject type checking code



Owen Taylor <otaylor redhat com> writes:

> Tim Janik <timj gtk org> writes:
> 
> > On Sat, 16 Jun 2001, Erik Walthinsen wrote:
> > 
> > > Parapraxis found that this reduced the number of calls in his code from
> > > 10489576 to 1391, or a 99.987% reduction.  The total CPU time taken by
> > > g_type_instance_is_a went from 26% to *not measurable*.
> > 
> > ok, that is quite alarming indeed.
> > 
> > >  This won't be
> > > quite as dramatic in something like a Gtk+ application, where more checks
> > > will be done between an object and one of its parent classes, but I would
> > > still expect *at least* a 50% reduction.
> > 
> > i'd second that.
> 
> I'm not sure I would. When I did profiling for GtkObject, a huge
> number of checks were from gtk_object_ref(), gtk_signal_emit()
> and such, so the really big win was:
> 
> #define GTK_IS_OBJECT_CLASS(klass)      ( \
>   (klass) != NULL && \
>   GTK_FUNDAMENTAL_TYPE (((GtkObjectClass*) (klass))->type) == GTK_TYPE_OBJECT \
> )
> 
> Of course, GDK and Pango have flatter inheritance trees than GTK+,
> so, checking against the object type is probably a lot more
> common than it used to be. 
> 
> But trying to second guess this is completely pointless. Someone
> needs to add a few lines of instrumentation, run testgtk and
> report the results.

Did some checking - patch follows for anybody who wants to 
try for themselves.

For the various tests in testgtk for the type of the object were 
between 7 and 33% of the total number of BLAH_IS_FOO checks.
(Around 15% was typical)

G_IS_OBJECT() checks were between 16% and 80% of the total
number of checks. (Around 40% was typical)

Obviously, it varies a _lot_ depending on what is going on.

Regards,
                                        Owen

Index: gtype.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gtype.c,v
retrieving revision 1.27
diff -u -r1.27 gtype.c
--- gtype.c	2001/05/10 13:58:40	1.27
+++ gtype.c	2001/06/17 17:35:05
@@ -2479,15 +2479,40 @@
   return type;
 }
 
+static int total = 0;
+static int same = 0;
+static int gobject = 0;
+
+void print_perc (void)
+{
+  g_print ("Percentage of checks 'a is a': %.2f (%d/%d)\n",
+	   100 * (double)same / total, same, total);
+  g_print ("Percentage of checks 'a is gobject': %.2f (%d/%d)\n",
+	   100 * (double)gobject / total, gobject, total);
+}
+
 gboolean
 g_type_instance_is_a (GTypeInstance *type_instance,
 		      GType          iface_type)
 {
   TypeNode *node, *iface;
   gboolean check;
+  static gboolean init = FALSE;
 
   if (!type_instance || !type_instance->g_class)
     return FALSE;
+
+  if (!init)
+    {
+      init = TRUE;
+      g_atexit (print_perc);
+    }
+
+  total++;
+  if (type_instance->g_class->g_type == iface_type)
+    same++;
+  if (G_TYPE_OBJECT == iface_type)
+    gobject++;
 
   G_READ_LOCK (&type_rw_lock);
   node = lookup_type_node_L (type_instance->g_class->g_type);




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