[glib] Allocate quarks in chunks to avoid overhead and fragmentation



commit 270a954b54521ce0eca6d408b8e8c081c70c3455
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Apr 27 09:23:07 2010 +0200

    Allocate quarks in chunks to avoid overhead and fragmentation
    
    See bug 616720 for some measurements.

 glib/gdataset.c |   36 ++++++++++++++++++++++++++++++++++--
 1 files changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/glib/gdataset.c b/glib/gdataset.c
index ffc4b96..60adfe4 100644
--- a/glib/gdataset.c
+++ b/glib/gdataset.c
@@ -1004,6 +1004,38 @@ g_quark_try_string (const gchar *string)
   return quark;
 }
 
+#define QUARK_STRING_BLOCK_SIZE (4096 - sizeof (gsize))
+static char *quark_block = NULL;
+static int quark_block_offset = 0;
+
+/* HOLDS: g_quark_global_lock */
+static char *
+quark_strdup(const gchar *string)
+{
+  gchar *copy;
+  gsize len;
+
+  len = strlen (string) + 1;
+
+  /* For strings longer than half the block size, fall back
+     to strdup so that we fill our blocks at least 50%. */
+  if (len > QUARK_STRING_BLOCK_SIZE / 2)
+    return g_strdup (string);
+
+  if (quark_block == NULL ||
+      QUARK_STRING_BLOCK_SIZE - quark_block_offset < len)
+    {
+      quark_block = g_malloc (QUARK_STRING_BLOCK_SIZE);
+      quark_block_offset = 0;
+    }
+
+  copy = quark_block + quark_block_offset;
+  memcpy (copy, string, len);
+  quark_block_offset += len;
+
+  return copy;
+}
+
 /* HOLDS: g_quark_global_lock */
 static inline GQuark
 g_quark_from_string_internal (const gchar *string, 
@@ -1015,8 +1047,8 @@ g_quark_from_string_internal (const gchar *string,
     quark = GPOINTER_TO_UINT (g_hash_table_lookup (g_quark_ht, string));
   
   if (!quark)
-    quark = g_quark_new (duplicate ? g_strdup (string) : (gchar *)string);
-  
+    quark = g_quark_new (duplicate ? quark_strdup (string) : (gchar *)string);
+
   return quark;
 }
 



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