Add fast path for construction with no params
authorAlexander Larsson <alexl@redhat.com>
Wed, 19 Aug 2009 15:24:16 +0000 (17:24 +0200)
committerAlexander Larsson <alexl@redhat.com>
Fri, 2 Oct 2009 19:02:48 +0000 (21:02 +0200)
This avoids a bunch of code and makes construction of simple objects
faster.

Object construction performance improvement:
         Non-Threaded   Threaded
Simple:           14%         5%
Complex:        -1.1%      -2.2%

Other tests stable.

https://bugzilla.gnome.org/show_bug.cgi?id=557100

gobject/gobject.c

index 175c2bf..8432488 100644 (file)
@@ -1139,7 +1139,7 @@ g_object_newv (GType       object_type,
               guint       n_parameters,
               GParameter *parameters)
 {
-  GObjectConstructParam *cparams, *oparams;
+  GObjectConstructParam *cparams = NULL, *oparams;
   GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
   GObject *object;
   GObjectClass *class, *unref_class = NULL;
@@ -1161,6 +1161,17 @@ g_object_newv (GType       object_type,
       n_total_cparams += 1;
     }
 
+  if (n_parameters == 0 && n_total_cparams == 0)
+    {
+      /* This is a simple object with no construct properties, and
+       * no properties are being set, so short circuit the parameter
+       * handling. This speeds up simple object construction.
+       */
+      oparams = NULL;
+      object = class->constructor (object_type, 0, NULL);
+      goto did_construction;
+    }
+
   /* collect parameters, sort into construction and normal ones */
   oparams = g_new (GObjectConstructParam, n_parameters);
   cparams = g_new (GObjectConstructParam, n_total_cparams);
@@ -1245,6 +1256,7 @@ g_object_newv (GType       object_type,
     g_value_unset (cvalues + n_cvalues);
   g_free (cvalues);
 
+ did_construction:
   /* adjust freeze_count according to g_object_init() and remaining properties */
   G_LOCK (construction_mutex);
   newly_constructed = slist_maybe_remove (&construction_objects, object);