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
+3 -2
View File
@@ -790,11 +790,12 @@ zfs_name_to_prop(const char *propname)
boolean_t
zfs_prop_user(const char *name)
{
int i;
int i, len;
char c;
boolean_t foundsep = B_FALSE;
for (i = 0; i < strlen(name); i++) {
len = strlen(name);
for (i = 0; i < len; i++) {
c = name[i];
if (!zprop_valid_char(c))
return (B_FALSE);
+3 -2
View File
@@ -473,11 +473,12 @@ vdev_name_to_prop(const char *propname)
boolean_t
vdev_prop_user(const char *name)
{
int i;
int i, len;
char c;
boolean_t foundsep = B_FALSE;
for (i = 0; i < strlen(name); i++) {
len = strlen(name);
for (i = 0; i < len; i++) {
c = name[i];
if (!zprop_valid_char(c))
return (B_FALSE);