diff options
author | rilysh <[email protected]> | 2024-10-02 21:40:06 +0530 |
---|---|---|
committer | GitHub <[email protected]> | 2024-10-02 09:10:06 -0700 |
commit | 86737c5927c204dc866f1de7c95ab964e98be6af (patch) | |
tree | c17ceb5f4f69f9bad75964821b652ab22f22fb6e /cmd | |
parent | e8cbb5952d079e8a04b3c3bd58118b2c6b635493 (diff) |
Avoid computing strlen() inside loops
Compiling with -O0 (no proper optimizations), strlen() call
in loops for comparing the size, isn't being called/initialized
before the actual loop gets started, which causes n-numbers of
strlen() calls (as long as the string is). Keeping the length
before entering in the loop is a good idea.
On some places, even with -O2, both GCC and Clang can't
recognize this pattern, which seem to happen in an array
of char pointer.
Reviewed-by: Brian Behlendorf <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Signed-off-by: rilysh <[email protected]>
Closes #16584
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/zdb/zdb.c | 12 | ||||
-rw-r--r-- | cmd/ztest.c | 8 |
2 files changed, 12 insertions, 8 deletions
diff --git a/cmd/zdb/zdb.c b/cmd/zdb/zdb.c index f185e2460..2c136bacb 100644 --- a/cmd/zdb/zdb.c +++ b/cmd/zdb/zdb.c @@ -8800,7 +8800,7 @@ zdb_read_block(char *thing, spa_t *spa) void *lbuf, *buf; char *s, *p, *dup, *flagstr, *sizes, *tmp = NULL; const char *vdev, *errmsg = NULL; - int i, error; + int i, len, error; boolean_t borrowed = B_FALSE, found = B_FALSE; dup = strdup(thing); @@ -8828,7 +8828,8 @@ zdb_read_block(char *thing, spa_t *spa) for (s = strtok_r(flagstr, ":", &tmp); s != NULL; s = strtok_r(NULL, ":", &tmp)) { - for (i = 0; i < strlen(flagstr); i++) { + len = strlen(flagstr); + for (i = 0; i < len; i++) { int bit = flagbits[(uchar_t)flagstr[i]]; if (bit == 0) { @@ -9098,13 +9099,14 @@ zdb_embedded_block(char *thing) static boolean_t zdb_numeric(char *str) { - int i = 0; + int i = 0, len; - if (strlen(str) == 0) + len = strlen(str); + if (len == 0) return (B_FALSE); if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) i = 2; - for (; i < strlen(str); i++) { + for (; i < len; i++) { if (!isxdigit(str[i])) return (B_FALSE); } diff --git a/cmd/ztest.c b/cmd/ztest.c index c58d35c99..523f280aa 100644 --- a/cmd/ztest.c +++ b/cmd/ztest.c @@ -685,15 +685,17 @@ static int str2shift(const char *buf) { const char *ends = "BKMGTPEZ"; - int i; + int i, len; if (buf[0] == '\0') return (0); - for (i = 0; i < strlen(ends); i++) { + + len = strlen(ends); + for (i = 0; i < len; i++) { if (toupper(buf[0]) == ends[i]) break; } - if (i == strlen(ends)) { + if (i == len) { (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf); usage(B_FALSE); |