Skip to content

Conversation

@GermanAizek
Copy link

@navi-desu,

Example with clang-19 -O2

Before:

before

After:

after

@thesamesam
Copy link
Contributor

Note that there's at least one sight-impaired contributor to this repository who will be looking at this PR. Please include godbolt links as well as screenshots to make it accessible.

@N-R-K
Copy link
Contributor

N-R-K commented Mar 13, 2025

Technically, this should be fine; assuming that those strings don't change inside the loop (didn't verify myself).

But my gut feeling is that it falls into intangible micro-optimization. If we wanted to be serious about string performance, we'd rid of nul-strings entirely. (Not that I advocate doing it, as it'd be a pretty big and error prone rewrite for little gain).

@navi-desu
Copy link
Member

navi-desu commented Mar 13, 2025

the assembly shows, the call to strlen happens before label that is used for looping-- so in a way, the compiled code is already acting like this is merged

merging this is still good imo because it's clearer, and doesn't rely on compiler optimization to make the code not be awful

@N-R-K
Copy link
Contributor

N-R-K commented Mar 13, 2025

because it's clearer

Is it? It de-syncs the string pointer and the string length. And so if someone in the future edits it so that the pointer changes during the loop but then forgets to change the length then bad stuff happens.

}
}

if (!*prefixed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this codepath is called once per-line. while calling this function with multiline strings is certainly valid, and i'm sure done in places, it's not the common case, and I have a hard time seeing strlen being called on tiny strings showing up in any perf situation.

len_ec & ec_normal will be like 7 bytes each. len_prefix will be ~length of longest service name, and that's likely to also be quite small.

while ((utmp = getutxent()) != NULL) {
if (utmp->ut_type != USER_PROCESS || utmp->ut_user[0] == 0)
continue;
if (strncmp(utmp->ut_line, _PATH_DEV, strlen(_PATH_DEV)) == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PATH_DEV is a constant string. if the compiler can't optimize that to a constant integer at compile time, it's not worth caring about. drop this change please.

}
free(buffer);
if (pid == 0 && strlen(my_ns) && strlen (proc_ns) && strcmp(my_ns, proc_ns))
if (pid == 0 && len_my_ns && strlen (proc_ns) && strcmp(my_ns, proc_ns))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these strlen calls simply want to know if the string is empty. that can be checked by my_ns[0] and proc_ns[0] -- no need for strlen at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants