From 9ee392dc8ce678113ecc7827dedb23e0bdae3b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 7 Feb 2021 16:03:11 +0100 Subject: [PATCH] url-mode: generate-key-combos: minor efficiency tweaks * Use a do..while loop; this lets us drop the second half of the loop condition. * Call wcslen(prefix) once, *before* iterating the alphabet characters. * Step through the alphabet characters using a pointer, as this avoids an indexed load (with possibly an imul instruction in e.g. -Os builds). --- url-mode.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/url-mode.c b/url-mode.c index 4d52dab2..2a3ee41d 100644 --- a/url-mode.c +++ b/url-mode.c @@ -365,22 +365,23 @@ generate_key_combos(size_t count, wchar_t *combos[static count]) hints[0] = xwcsdup(L""); size_t offset = 0; - while (hints_count - offset < count || hints_count == 1) { + do { const wchar_t *prefix = hints[offset++]; + const size_t prefix_len = wcslen(prefix); hints = xrealloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); - for (size_t i = 0; i < alphabet_len; i++) { - wchar_t wc = alphabet[i]; - wchar_t *hint = xmalloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); + const wchar_t *wc = &alphabet[0]; + for (size_t i = 0; i < alphabet_len; i++, wc++) { + wchar_t *hint = xmalloc((prefix_len + 1 + 1) * sizeof(wchar_t)); + hints[hints_count + i] = hint; /* Will be reversed later */ - hint[0] = wc; + hint[0] = *wc; wcscpy(&hint[1], prefix); - hints[hints_count + i] = hint; } hints_count += alphabet_len; - } + } while (hints_count - offset < count); xassert(hints_count - offset >= count);