Optimize hash table when usage is only as a set

When searching for duplicate strings in the output list, a hash table is
used to keep track of which strings have been seen. This usage as a set
can be optimized as described in the documentation to not allocate or
free the hash table values:

http://developer.gnome.org/glib/stable/glib-Hash-Tables.html#glib-Hash-Tables.description
This commit is contained in:
Dan Nicholson 2012-12-03 08:25:40 -08:00
parent a7b465806f
commit 428335e2b5

12
pkg.c
View file

@ -433,10 +433,10 @@ string_list_strip_duplicates (GList *list)
table = g_hash_table_new (g_str_hash, g_str_equal); table = g_hash_table_new (g_str_hash, g_str_equal);
for (tmp = list; tmp != NULL; tmp = g_list_next (tmp)) for (tmp = list; tmp != NULL; tmp = g_list_next (tmp))
{ {
if (g_hash_table_lookup (table, tmp->data) == NULL) if (!g_hash_table_lookup_extended (table, tmp->data, NULL, NULL))
{ {
/* Unique string. Track it and and move to the next. */ /* Unique string. Track it and and move to the next. */
g_hash_table_insert (table, tmp->data, tmp->data); g_hash_table_replace (table, tmp->data, tmp->data);
} }
else else
{ {
@ -462,10 +462,10 @@ string_list_strip_duplicates_from_back (GList *list)
table = g_hash_table_new (g_str_hash, g_str_equal); table = g_hash_table_new (g_str_hash, g_str_equal);
for (tmp = g_list_last (list); tmp != NULL; tmp = g_list_previous (tmp)) for (tmp = g_list_last (list); tmp != NULL; tmp = g_list_previous (tmp))
{ {
if (g_hash_table_lookup (table, tmp->data) == NULL) if (!g_hash_table_lookup_extended (table, tmp->data, NULL, NULL))
{ {
/* Unique string. Track it and and move to the next. */ /* Unique string. Track it and and move to the next. */
g_hash_table_insert (table, tmp->data, tmp->data); g_hash_table_replace (table, tmp->data, tmp->data);
} }
else else
{ {
@ -681,7 +681,7 @@ package_list_strip_duplicates (GList *packages)
{ {
Package *pkg = cur->data; Package *pkg = cur->data;
if (g_hash_table_lookup (requires, pkg->key) != NULL) if (g_hash_table_lookup_extended (requires, pkg->key, NULL, NULL))
{ {
GList *dup = cur; GList *dup = cur;
@ -693,7 +693,7 @@ package_list_strip_duplicates (GList *packages)
else else
{ {
/* Unique package. Track it and move to the next. */ /* Unique package. Track it and move to the next. */
g_hash_table_insert (requires, pkg->key, pkg); g_hash_table_replace (requires, pkg->key, pkg);
} }
} }
g_hash_table_destroy (requires); g_hash_table_destroy (requires);