diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2015-07-16 16:12:35 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2015-07-16 16:19:55 +0100 |
commit | e259b2f30d86fdde7dab27a25e8088d36421acc7 (patch) | |
tree | 7c3499bb6a4f9ecf237fe64cdc9f53efacc67c78 /modules/printbackends | |
parent | 3b41daca780e9e83d04dcad43e2fbd5a2ab8c120 (diff) | |
download | gtk+-e259b2f30d86fdde7dab27a25e8088d36421acc7.tar.gz |
Avoid O(n²) walking of string arrays
"Yo, we heard you like traversing NULL-terminated arrays to operate on
them, so we called g_strv_length() as the for condition, so you can
iterate the array while iterating the array."
Instead of making famed rapper and television producer Xzibit proud, we
should avoid calling g_strv_length() on an array while looping on the
array, to avoid quadratic complexity.
We do this in various places that deal with arrays of strings that we
cannot really guess are short enough not to matter — e.g. the list of
CSS selectors in the inspector, or the required authentication
information for printing.
Diffstat (limited to 'modules/printbackends')
-rw-r--r-- | modules/printbackends/cups/gtkcupssecretsutils.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/modules/printbackends/cups/gtkcupssecretsutils.c b/modules/printbackends/cups/gtkcupssecretsutils.c index d5df4a7326..925f7d561c 100644 --- a/modules/printbackends/cups/gtkcupssecretsutils.c +++ b/modules/printbackends/cups/gtkcupssecretsutils.c @@ -109,7 +109,7 @@ get_secret_cb (GObject *source_object, *key = NULL, *value = NULL; GVariantIter *iter = NULL; - guint i; + guint i, required_len; gint pw_field = -1; task = user_data; @@ -228,7 +228,8 @@ get_secret_cb (GObject *source_object, fail: /* Error out */ GTK_NOTE (PRINTING, g_print ("Failed to lookup secret.\n")); - for (i = 0; i < g_strv_length (task_data->auth_info_required); i++) + required_len = g_strv_length (task_data->auth_info_required); + for (i = 0; i < required_len; i++) { /* Not all fields of auth_info are neccessarily written so we can not use strfreev here */ |