pidgin: f09b8fa2: Use a hash table for looking up PurpleGr...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Thu Jul 16 00:45:34 EDT 2009


-----------------------------------------------------------------
Revision: f09b8fa2cb4486c1ddd4fbc6343caac76d3134a8
Ancestor: b60c6e508d79ec72acbf30f8c59bc89c9cbb83c7
Author: aman at tmm1.net
Date: 2009-07-16T04:40:08
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/f09b8fa2cb4486c1ddd4fbc6343caac76d3134a8

Modified files:
        ChangeLog ChangeLog.API libpurple/blist.c

ChangeLog: 

Use a hash table for looking up PurpleGroup:s.

Apparently Aman "tmm1" Gupta has a lot of groups in his buddy list,
so he wrote this patch.

-------------- next part --------------
============================================================
--- ChangeLog	329b4fb3594ddb43df003161041495b993ff9568
+++ ChangeLog	430e8f894d5d02bcf1f1ac7695cd47f1f3cd9a57
@@ -7,7 +7,7 @@ version 2.6.0 (??/??/2009):
 	* Voice & Video framework in libpurple, thanks to Mike Ruprecht's summer
 	  of code project in 2008.
 	* It should no longer be possible to end up with duplicates of buddies
-	  in a group on the buddy list. (Paul Aurich)
+	  in a group on the buddy list.
 	* Removed the unmaintained and unneeded toc protocol plugin.
 	* Fixed NTLM authentication on big-endian systems.
 	* Various memory cleanups when unloading libpurple. (Nick Hebner)
@@ -35,7 +35,9 @@ version 2.6.0 (??/??/2009):
 	  from you on MSN.
 	* Support sending an invite message to buddies when requesting authorization
 	  from them on MSN.
-	* Better handle corrupt certificates in the TLS Peers cache.
+	* Better handling of corrupt certificates in the TLS Peers cache.
+	* More efficient purple_find_buddies() and purple_find_group() functions.
+	  (Jan Kaluza and Aman Gupta)
 
 	AIM and ICQ:
 	* Preliminary support for a new authentication scheme called
============================================================
--- ChangeLog.API	db8bdb5f7ea76c04c6e738e5693f1d0638a638c4
+++ ChangeLog.API	1dce09a26f44b3a5e82c94edd3268d201c6e5462
@@ -80,6 +80,9 @@ version 2.6.0 (??/??/2009):
 		* Added a client_type field in the get_ui_info core UI op. See
 		  core.h for details.
 		* Added introspection of signals exposed via the D-Bus API.
+		* purple_find_buddies is now more efficient in the case where
+		  it is enumerating all the buddies for an account.
+		* purple_find_group is now more efficient for large numbers of groups.
 
 		Deprecated:
 		* buddy-added and buddy-removed blist signals
============================================================
--- libpurple/blist.c	b24d022474bece4fc5494649a786ccafedaa67ad
+++ libpurple/blist.c	89f5d3a0ae4934e48f83144011ea7b0f0915ba09
@@ -48,6 +48,12 @@ static GHashTable *buddies_cache = NULL;
  */
 static GHashTable *buddies_cache = NULL;
 
+/**
+ * A hash table used for efficient lookups of groups by name.
+ * UTF-8 collate-key => PurpleGroup*.
+ */
+static GHashTable *groups_cache = NULL;
+
 static guint          save_timer = 0;
 static gboolean       blist_loaded = FALSE;
 
@@ -704,6 +710,10 @@ PurpleBuddyList *purple_blist_new()
 	buddies_cache = g_hash_table_new_full(g_direct_hash, g_direct_equal,
 					 NULL, (GDestroyNotify)g_hash_table_destroy);
 
+	groups_cache = g_hash_table_new_full((GHashFunc)g_str_hash,
+					 (GEqualFunc)g_str_equal,
+					 (GDestroyNotify)g_free, NULL);
+
 	for (account = purple_accounts_get_all(); account != NULL; account = account->next)
 	{
 		purple_blist_buddies_cache_add_account(account->data);
@@ -1203,6 +1213,7 @@ void purple_blist_rename_group(PurpleGro
 	} else {
 		/* A simple rename */
 		PurpleBlistNode *cnode, *bnode;
+		gchar* key;
 
 		/* Build a GList of all buddies in this group */
 		for (cnode = ((PurpleBlistNode *)source)->child; cnode != NULL; cnode = cnode->next) {
@@ -1213,6 +1224,13 @@ void purple_blist_rename_group(PurpleGro
 
 		old_name = source->name;
 		source->name = new_name;
+
+		key = g_utf8_collate_key(old_name, -1);
+		g_hash_table_remove(groups_cache, key);
+		g_free(key);
+
+		key = g_utf8_collate_key(new_name, -1);
+		g_hash_table_insert(groups_cache, key, source);
 	}
 
 	/* Save our changes */
@@ -1946,6 +1964,7 @@ void purple_blist_add_group(PurpleGroup 
 {
 	PurpleBlistUiOps *ops;
 	PurpleBlistNode *gnode = (PurpleBlistNode*)group;
+	gchar* key;
 
 	g_return_if_fail(group != NULL);
 	g_return_if_fail(PURPLE_BLIST_NODE_IS_GROUP((PurpleBlistNode *)group));
@@ -1989,6 +2008,9 @@ void purple_blist_add_group(PurpleGroup 
 		purplebuddylist->root = gnode;
 	}
 
+	key = g_utf8_collate_key(group->name, -1);
+	g_hash_table_insert(groups_cache, key, group);
+
 	purple_blist_schedule_save();
 
 	if (ops && ops->update) {
@@ -2174,6 +2196,7 @@ void purple_blist_remove_group(PurpleGro
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleBlistNode *node;
 	GList *l;
+	gchar* key;
 
 	g_return_if_fail(group != NULL);
 
@@ -2191,6 +2214,10 @@ void purple_blist_remove_group(PurpleGro
 	if (node->next)
 		node->next->prev = node->prev;
 
+	key = g_utf8_collate_key(group->name, -1);
+	g_hash_table_remove(groups_cache, key);
+	g_free(key);
+
 	purple_blist_schedule_save();
 
 	/* Update the UI */
@@ -2427,17 +2454,17 @@ PurpleGroup *purple_find_group(const cha
 
 PurpleGroup *purple_find_group(const char *name)
 {
-	PurpleBlistNode *node;
+	gchar* key;
+	PurpleGroup *group;
 
 	g_return_val_if_fail(purplebuddylist != NULL, NULL);
 	g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
 
-	for (node = purplebuddylist->root; node != NULL; node = node->next) {
-		if (!purple_utf8_strcasecmp(((PurpleGroup *)node)->name, name))
-			return (PurpleGroup *)node;
-	}
+	key = g_utf8_collate_key(name, -1);
+	group = g_hash_table_lookup(groups_cache, key);
+	g_free(key);
 
-	return NULL;
+	return group;
 }
 
 PurpleChat *
@@ -3117,7 +3144,11 @@ purple_blist_uninit(void)
 
 	g_hash_table_destroy(purplebuddylist->buddies);
 	g_hash_table_destroy(buddies_cache);
+	g_hash_table_destroy(groups_cache);
 
+	buddies_cache = NULL;
+	groups_cache = NULL;
+
 	PURPLE_DBUS_UNREGISTER_POINTER(purplebuddylist);
 	g_free(purplebuddylist);
 	purplebuddylist = NULL;


More information about the Commits mailing list