[Nautilus-list] Cacheing bonobo ui xml



Here is a dirty patch that caches the result of bonobo_ui_util_new_ui so 
that multiple parsing and fixups of the same file is avoided. This wastes 
a bit memory (haven't measured how much) but speeds up e.g. opening a new 
window in nautilus. I got about 9% speedup on open new window in nautilus.

Comments?

/ Alex


Index: bonobo-ui-util.c
===================================================================
RCS file: /cvs/gnome/bonobo/bonobo/bonobo-ui-util.c,v
retrieving revision 1.42
diff -u -p -r1.42 bonobo-ui-util.c
--- bonobo-ui-util.c	2001/04/15 16:12:32	1.42
+++ bonobo-ui-util.c	2001/08/21 21:42:20
@@ -1187,6 +1187,35 @@ bonobo_ui_util_fixup_icons (BonoboUINode
 }
 
 
+
+typedef struct {
+	const char *file_name;
+	const char *app_datadir;
+	const char *app_name;
+	BonoboUINode *root;
+} BonoboUINodeCacheEntry;
+
+static guint
+node_hash (gconstpointer key)
+{
+	BonoboUINodeCacheEntry *entry = (BonoboUINodeCacheEntry *)key;
+	/* Ignore the app_datadir in the hash, since that 
+	   is also in file_name (always?) */
+	return g_str_hash (entry->file_name) ^ g_str_hash (entry->app_name);
+}
+
+static gint
+node_equal (gconstpointer a,
+	    gconstpointer b)
+{
+	BonoboUINodeCacheEntry *entry_a = (BonoboUINodeCacheEntry *)a;
+	BonoboUINodeCacheEntry *entry_b = (BonoboUINodeCacheEntry *)b;
+
+	return (strcmp (entry_a->file_name, entry_b->file_name) == 0) &&
+		(strcmp (entry_a->app_name, entry_b->app_name) == 0) &&
+		(strcmp (entry_a->app_datadir, entry_b->app_datadir) == 0);
+}
+
 /**
  * bonobo_ui_util_new_ui:
  * @component: The component help callback should be on
@@ -1205,10 +1234,26 @@ bonobo_ui_util_new_ui (BonoboUIComponent
 		       const char        *app_name)
 {
 	BonoboUINode *node;
+	static GHashTable *loaded_node_cache = NULL;
+	BonoboUINodeCacheEntry entry, *cached;
 
 	g_return_val_if_fail (app_name != NULL, NULL);
 	g_return_val_if_fail (file_name != NULL, NULL);
 
+	if (loaded_node_cache == NULL) {
+		loaded_node_cache = g_hash_table_new (node_hash,
+						      node_equal);
+	}
+
+	entry.file_name = file_name;
+	entry.app_datadir = app_datadir;
+	entry.app_name = app_name;
+	
+	cached = g_hash_table_lookup (loaded_node_cache, &entry);
+	if (cached) {
+		return bonobo_ui_node_copy (cached->root, TRUE);
+	}
+
         node = bonobo_ui_node_from_file (file_name);
 
 	bonobo_ui_node_strip (&node);
@@ -1218,6 +1263,15 @@ bonobo_ui_util_new_ui (BonoboUIComponent
 	bonobo_ui_util_fixup_help (component, node, app_datadir, app_name);
 
 	bonobo_ui_util_fixup_icons (node);
+
+	cached = g_new (BonoboUINodeCacheEntry, 1);
+	cached->file_name = g_strdup (file_name);
+	cached->app_datadir = g_strdup (app_datadir);
+	cached->app_name = g_strdup (app_name);
+	cached->root = bonobo_ui_node_copy (node, TRUE);
+
+	g_hash_table_insert (loaded_node_cache,
+			     cached, cached);
 
 	return node;
 }





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