Make empty ObClientSet objects not hold a hash table.
authorDana Jansens <danakj@orodu.net>
Fri, 29 Jul 2011 21:35:31 +0000 (17:35 -0400)
committerDana Jansens <danakj@orodu.net>
Sun, 16 Oct 2011 22:55:15 +0000 (18:55 -0400)
This saves creating one every time we make an empty set (boolean filter
  false result)

openbox/client_set.c

index fb47de0c7780fb1e673b43fe4cddb9f4df9bdba7..6b86b9795d4d2c48fdd1e697c6356e2b02a8a01f 100644 (file)
@@ -38,7 +38,7 @@ ObClientSet* client_set_empty(void)
 
     set = g_slice_new(ObClientSet);
     set->all = FALSE;
-    set->h = g_hash_table_new(g_int_hash, g_int_equal);
+    set->h = NULL;
     client_add_destroy_notify((ObClientCallback)client_destroyed, set);
     return set;
 }
@@ -50,7 +50,9 @@ ObClientSet* client_set_single(void)
 
     c = event_current_target();
     if (!c) return NULL;
-    set = client_set_empty();
+    set = g_slice_new(ObClientSet);
+    set->all = FALSE;
+    set->h = g_hash_table_new(g_int_hash, g_int_equal);
     g_hash_table_insert(set->h, &c->window, c);
     return set;
 }
@@ -72,7 +74,7 @@ void client_set_destroy(ObClientSet *set)
     if (!set->all) {
         client_remove_destroy_notify_data((ObClientCallback)client_destroyed,
                                           set);
-        g_hash_table_destroy(set->h);
+        if (set->h) g_hash_table_destroy(set->h);
     }
     g_slice_free(ObClientSet, set);
 }
@@ -99,6 +101,15 @@ ObClientSet* client_set_union(ObClientSet *a, ObClientSet *b)
         client_set_destroy(a);
         return b;
     }
+    if (!a->h) {
+        client_set_destroy(a);
+        return b;
+    }
+    if (!b->h) {
+        client_set_destroy(b);
+        return a;
+    }
+
     g_hash_table_foreach(b->h, foreach_union, a->h);
     client_set_destroy(b);
     return a;
@@ -126,6 +137,14 @@ ObClientSet* client_set_intersection(ObClientSet *a, ObClientSet *b)
         client_set_destroy(b);
         return a;
     }
+    if (!a->h) {
+        client_set_destroy(b);
+        return a;
+    }
+    if (!b->h) {
+        client_set_destroy(a);
+        return b;
+    }
 
     g_hash_table_foreach_remove(a->h, foreach_intersection, b->h);
     client_set_destroy(b);
@@ -155,6 +174,10 @@ ObClientSet* client_set_reduce(ObClientSet *set, ObClientSetReduceFunc f,
     d.f = f;
     d.data = data;
     g_hash_table_foreach_remove(set->h, foreach_reduce, &d);
+    if (g_hash_table_size(set->h) == 0) {
+        g_hash_table_destroy(set->h);
+        set->h = NULL;
+    }
     return set;
 }
 
@@ -168,8 +191,12 @@ ObClientSet* client_set_expand(ObClientSet *set, ObClientSetExpandFunc f,
 
     for (it = client_list; it; it = g_list_next(it)) {
         ObClient *c = it->data;
-        if (!g_hash_table_lookup(set->h, &c->window) && f(c, data))
-            g_hash_table_insert(set->h, &c->window, c);
+        if (!set->h || !g_hash_table_lookup(set->h, &c->window))
+            if (f(c, data)) {
+                if (!set->h)
+                    set->h = g_hash_table_new(g_int_hash, g_int_equal);
+                g_hash_table_insert(set->h, &c->window, c);
+            }
     }
     return set;
 }
@@ -177,12 +204,12 @@ ObClientSet* client_set_expand(ObClientSet *set, ObClientSetExpandFunc f,
 gboolean client_set_is_empty(ObClientSet *set)
 {
     if (set->all) return client_list == NULL;
-    else return g_hash_table_size(set->h) == 0;
+    else return set->h == NULL;
 }
 
 gboolean client_set_test_boolean(ObClientSet *set)
 {
     if (set->all) return TRUE;
-    else return g_hash_table_size(set->h) > 0;
+    else return set->h != NULL;
 }