diff options
author | Matthias Clasen <mclasen@redhat.com> | 2015-09-09 11:08:43 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2015-09-09 11:17:13 -0400 |
commit | a9481cb410c6e2c1b516b282909d04f9ec1d61c4 (patch) | |
tree | 63840fe33f1a2c03c431a7012a3b86afa6e47ac7 /gtk/gtkcssselector.c | |
parent | 2d29ae4159a084262e3aefd79d61987476e54a91 (diff) | |
download | gtk+-a9481cb410c6e2c1b516b282909d04f9ec1d61c4.tar.gz |
match_all: Only allocate an array when needed
My statistics show that more than half of all calls end up
with 0 matches, so we can avoid some overhead by not allocating
an array at all in this case.
Diffstat (limited to 'gtk/gtkcssselector.c')
-rw-r--r-- | gtk/gtkcssselector.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/gtk/gtkcssselector.c b/gtk/gtkcssselector.c index 760e630847..eed04f7b41 100644 --- a/gtk/gtkcssselector.c +++ b/gtk/gtkcssselector.c @@ -159,8 +159,8 @@ g_ptr_array_insert_sorted (GPtrArray *array, } static void -gtk_css_selector_tree_found_match (const GtkCssSelectorTree *tree, - GPtrArray *array) +gtk_css_selector_tree_found_match (const GtkCssSelectorTree *tree, + GPtrArray **array) { int i; gpointer *matches; @@ -168,8 +168,11 @@ gtk_css_selector_tree_found_match (const GtkCssSelectorTree *tree, matches = gtk_css_selector_tree_get_matches (tree); if (matches) { + if (!*array) + *array = g_ptr_array_sized_new (16); + for (i = 0; matches[i] != NULL; i++) - g_ptr_array_insert_sorted (array, matches[i]); + g_ptr_array_insert_sorted (*array, matches[i]); } } @@ -1757,15 +1760,13 @@ GPtrArray * _gtk_css_selector_tree_match_all (const GtkCssSelectorTree *tree, const GtkCssMatcher *matcher) { - GPtrArray *array; + GPtrArray *array = NULL; update_type_references (); - array = g_ptr_array_sized_new (16); - for (; tree != NULL; tree = gtk_css_selector_tree_get_sibling (tree)) - gtk_css_selector_foreach (&tree->selector, matcher, gtk_css_selector_tree_match_foreach, array); + gtk_css_selector_foreach (&tree->selector, matcher, gtk_css_selector_tree_match_foreach, &array); return array; } |