-
Notifications
You must be signed in to change notification settings - Fork 8k
perf: save strlen to var instead of calc it each iterator #21089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
TimWolla
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me this decreases readability in something that is not in the hot path at all. I also expect the compiler to be able to do this optimization by itself.
status_len, ping_len is easy to connect with strlen(status), strlen(ping). Usually compiler will not do the opt. |
This is called loop-invariant code motion and is indeed a very common optimization. Such changes can be reasonable but only when demonstrating the generated assembly actually improves on a reasonably modern compiler. |
|
@iluuu1994 strlen is called inside loop. I think. |
|
this example with main and llvm-objdump: |
|
For the first snippet, the assembly doesn't make sense, that can simply be optimized into a simple For the second snippet you're compiling without optimizations. With -O2 the In any case: This is not a hot code path and readability is preferable. |
No description provided.