diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2021-02-07 15:03:11 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2021-02-07 15:33:35 +0000 |
| commit | 9ee392dc8ce678113ecc7827dedb23e0bdae3b84 (patch) | |
| tree | 39f7a8371c263410e42f5c2b241ffd154a318f25 | |
| parent | url-mode: be consistent; use xmalloc() + xrealloc() (diff) | |
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).
| -rw-r--r-- | url-mode.c | 15 |
1 files changed, 8 insertions, 7 deletions
@@ -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); |
