Taciturn

All entries (archive)

I was hacking together a GTK-based program that had to be capable of displaying a long list of items. It was running quite slowly — taking 174 seconds to load 50 000 items. That seemed like way too long for what should be one malloc per item and adjusting a few pointers.

The structure I used was a GtkTreeStore. It turns out it's ridiculously inefficient at appending items. Prepending takes about as long as you'd expect — about 0.83 seconds.

I tried another test with 280 000 lines. That took 4.25 seconds using gtk_tree_store_prepend, and I killed the process after nearly 30 minutes when it was using gtk_tree_store_append.

The slowness looks like it is in the GtkTreeStore's GNode backend where appending an item at a particular level involves walking the list of every item on that level:

while (sibling->next)
  sibling = sibling->next;

So, if you're building a GtkTreeStore with sequential data, reverse your data and use gtk_tree_store_prepend rather than gtk_tree_store_append — it's much faster.

I also tested GtkListStore and appending is as fast as prepending.

Tested with GTK 2.12.1 and Glib 2.14.1 (Debian). Source.

Comments

Jesper wrote:
Isn't that code buggy?

while(sibling = sibling->next)
sibling = sibling->next;

It essentially iterates over each second element in the list and in
case of an even numberet list it would call ->next on a null-pointer eventually.

Jesper Krogh <jesper@krogh.cc>
Sun 27 Jan 2008 [#]
Ted Percival wrote:
I think you misread the code. The condition does not do assignment.

It was a copy-paste from glib's gnode.c.
Mon 28 Jan 2008 [#]

Post comment


Also available in RSS.