g_hash_table_freeze and g_hash_table_thaw




When exactly should the functions g_hash_table_freeze() and
g_hash_table_thaw() be used?

The following program is very fast, but when the freeze/pair as
enabled, it is extremely slow. This is not very surprising, given that
they turn the whole thing into an O(n^2) operation.

#include <glib.h>
#include <stdlib.h>

#define N 40000

gint a[N];

gint
main ()
{
  GHashTable *t;
  gint i;

  t = g_hash_table_new (g_int_hash, g_int_equal);
  for (i=0; i<N; i++)
    a[i] = rand ();

  //  g_hash_table_freeze (t);
  for (i=0; i<N; i++)
    g_hash_table_insert (t, &a[i], &a[i]);
  // g_hash_table_thaw (t);
  return 0;
}

Then, I suppose, they should be used when you are going to _remove_ a
lot of elements. Only then do they make some sort of sense. If this is
correct, I think it should be documented. 

Alternatively, the implementation of GHashTable could be changed to
make g_hash_table_insert O(1) operation, ignoring the possibility that
a user may insert the same key twice. I do not know if this will break
any existing code.



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