[PATCH] libbb: code shrink and speed up index_in_strings()
Denys Vlasenko
vda.linux at googlemail.com
Tue Feb 2 13:38:10 UTC 2021
Applied, thanks!
On Fri, Jan 29, 2021 at 2:23 PM Ron Yorston <rmy at pobox.com> wrote:
>
> Rewrite index_in_strings() to replace calls to strcmp()/strlen().
> With this change searching for valid names in the applet_names
> array (for example) is 40% faster.
>
> The code has to assume the strings aren't sorted, so will always scan
> the entire array when presented with an invalid name.
>
> function old new delta
> index_in_strings 63 56 -7
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-7) Total: -7 bytes
>
> Signed-off-by: Ron Yorston <rmy at pobox.com>
> ---
> libbb/compare_string_array.c | 14 ++++++++++----
> 1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
> index a06e57d3d..f38168d7e 100644
> --- a/libbb/compare_string_array.c
> +++ b/libbb/compare_string_array.c
> @@ -63,13 +63,19 @@ int FAST_FUNC index_in_str_array(const char *const string_array[], const char *k
>
> int FAST_FUNC index_in_strings(const char *strings, const char *key)
> {
> - int idx = 0;
> + int j, idx = 0;
>
> while (*strings) {
> - if (strcmp(strings, key) == 0) {
> - return idx;
> + /* Do we see "key\0" at current position in strings? */
> + for (j = 0; *strings == key[j]; ++j) {
> + if (*strings++ == '\0') {
> + //bb_error_msg("found:'%s' i:%u", key, idx);
> + return idx; /* yes */
> + }
> }
> - strings += strlen(strings) + 1; /* skip NUL */
> + /* No. Move to the start of the next string. */
> + while (*strings++ != '\0')
> + ;
> idx++;
> }
> return -1;
> --
> 2.29.2
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
More information about the busybox
mailing list