Wed 2009/Oct/28
-
Do you see anything harmful in this chunk of code? You can read the GTK+ docs but please don't look at the implementation of the called methods.
GtkTreePath *path; GtkTreeIter filter_iter; for ( /* an iteration on all iters in a GtkTreeModel */) { path = gtk_tree_model_get_path (base_model, &iter); gtk_tree_model_filter_convert_child_iter_to_iter (filter, &filter_iter, &iter); if (gtk_tree_selection_iter_is_selected (selection, &filter_iter)) { /* do something with path */ } }
The code looks harmless, but it's not really. Looking at the implementation of gtk_tree_model_filter_convert_child_iter_to_iter() you'll see that the actual conversion is done by obtaining the GtkTreePath for the base model iterator and calling gtk_tree_model_filter_convert_child_path_to_path(). Similarly, gtk_tree_selection_iter_is_selected() internally calls gtk_tree_selection_path_is_selected(). So, you are calling gtk_tree_model_get_path() three times in total when you only need to do it once. Just use the GtkTreePath equivalents.
Is this obvious for everyone but me? I mean, without looking at an oprofile callgraph and/or the GTK+ sources. I am not so sure.
Syndicated 2009-10-28 17:19:00 from Claudio Saavedra's ChangeLog