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 <behlendorf1@llnl.gov>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Signed-off-by: rilysh <nightquick@proton.me>
Closes #16584
This commit is contained in:
rilysh
2024-10-02 21:40:06 +05:30
committed by Tony Hutter
parent 4adc97ae15
commit e8f4592a19
6 changed files with 28 additions and 17 deletions
+7 -5
View File
@@ -8661,7 +8661,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);
@@ -8689,7 +8689,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) {
@@ -8960,13 +8961,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);
}