Skip to content

Commit 107932a

Browse files
committed
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. Signed-off-by: rilysh <[email protected]>
1 parent c84a37a commit 107932a

File tree

6 files changed

+26
-17
lines changed

6 files changed

+26
-17
lines changed

cmd/zdb/zdb.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8788,7 +8788,7 @@ zdb_read_block(char *thing, spa_t *spa)
87888788
void *lbuf, *buf;
87898789
char *s, *p, *dup, *flagstr, *sizes, *tmp = NULL;
87908790
const char *vdev, *errmsg = NULL;
8791-
int i, error;
8791+
int i, len, error;
87928792
boolean_t borrowed = B_FALSE, found = B_FALSE;
87938793

87948794
dup = strdup(thing);
@@ -8816,7 +8816,7 @@ zdb_read_block(char *thing, spa_t *spa)
88168816
for (s = strtok_r(flagstr, ":", &tmp);
88178817
s != NULL;
88188818
s = strtok_r(NULL, ":", &tmp)) {
8819-
for (i = 0; i < strlen(flagstr); i++) {
8819+
for (i = 0, len = strlen(flagstr); i < len; i++) {
88208820
int bit = flagbits[(uchar_t)flagstr[i]];
88218821

88228822
if (bit == 0) {
@@ -9086,13 +9086,14 @@ zdb_embedded_block(char *thing)
90869086
static boolean_t
90879087
zdb_numeric(char *str)
90889088
{
9089-
int i = 0;
9089+
int i = 0, len;
90909090

9091-
if (strlen(str) == 0)
9091+
len = strlen(str);
9092+
if (len == 0)
90929093
return (B_FALSE);
90939094
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0)
90949095
i = 2;
9095-
for (; i < strlen(str); i++) {
9096+
for (; i < len; i++) {
90969097
if (!isxdigit(str[i]))
90979098
return (B_FALSE);
90989099
}

cmd/ztest.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,17 @@ static int
685685
str2shift(const char *buf)
686686
{
687687
const char *ends = "BKMGTPEZ";
688-
int i;
688+
int i, len;
689689

690690
if (buf[0] == '\0')
691691
return (0);
692-
for (i = 0; i < strlen(ends); i++) {
692+
693+
len = strlen(ends);
694+
for (i = 0; i < len; i++) {
693695
if (toupper(buf[0]) == ends[i])
694696
break;
695697
}
696-
if (i == strlen(ends)) {
698+
if (i == len) {
697699
(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
698700
buf);
699701
usage(B_FALSE);

lib/libzfs/libzfs_util.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,15 +1618,17 @@ static int
16181618
str2shift(libzfs_handle_t *hdl, const char *buf)
16191619
{
16201620
const char *ends = "BKMGTPEZ";
1621-
int i;
1621+
int i, len;
16221622

16231623
if (buf[0] == '\0')
16241624
return (0);
1625-
for (i = 0; i < strlen(ends); i++) {
1625+
1626+
len = strlen(ends);
1627+
for (i = 0; i < len; i++) {
16261628
if (toupper(buf[0]) == ends[i])
16271629
break;
16281630
}
1629-
if (i == strlen(ends)) {
1631+
if (i == len) {
16301632
if (hdl)
16311633
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16321634
"invalid numeric suffix '%s'"), buf);

module/zcommon/zfs_prop.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,12 @@ zfs_name_to_prop(const char *propname)
804804
boolean_t
805805
zfs_prop_user(const char *name)
806806
{
807-
int i;
807+
int i, len;
808808
char c;
809809
boolean_t foundsep = B_FALSE;
810810

811-
for (i = 0; i < strlen(name); i++) {
811+
len = strlen(name);
812+
for (i = 0; i < len; i++) {
812813
c = name[i];
813814
if (!zprop_valid_char(c))
814815
return (B_FALSE);

module/zcommon/zpool_prop.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,12 @@ vdev_name_to_prop(const char *propname)
494494
boolean_t
495495
vdev_prop_user(const char *name)
496496
{
497-
int i;
497+
int i, len;
498498
char c;
499499
boolean_t foundsep = B_FALSE;
500500

501-
for (i = 0; i < strlen(name); i++) {
501+
len = strlen(name);
502+
for (i = 0; i < len; i++) {
502503
c = name[i];
503504
if (!zprop_valid_char(c))
504505
return (B_FALSE);

udev/zvol_id.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ main(int argc, const char *const *argv)
5656
return (1);
5757
}
5858
const char *dev_name = argv[1];
59+
size_t i, len;
5960

6061
int fd;
6162
struct stat sb;
@@ -73,11 +74,12 @@ main(int argc, const char *const *argv)
7374
}
7475

7576
const char *dev_part = strrchr(dev_name, 'p');
77+
len = strlen(zvol_name);
7678
if (dev_part != NULL) {
77-
sprintf(zvol_name + strlen(zvol_name), "-part%s", dev_part + 1);
79+
sprintf(zvol_name + len, "-part%s", dev_part + 1);
7880
}
7981

80-
for (size_t i = 0; i < strlen(zvol_name); ++i)
82+
for (i = 0; i < len; ++i)
8183
if (isblank(zvol_name[i]))
8284
zvol_name[i] = '+';
8385

0 commit comments

Comments
 (0)