Skip to content
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

Patch 4: removed unnecessary calls to strlen() in Curl_compareheader by using a new CONSTLEN macro for our string literals #8391

Closed
wants to merge 7 commits into from

Conversation

HenrikHolst
Copy link
Contributor

@HenrikHolst HenrikHolst commented Feb 4, 2022

removed unnecessary calls to strlen() in Curl_compareheader by using a new CONSTLEN macro for our string literals

HenrikHolst added 4 commits Feb 4, 2022
Added CONSTLEN, a macro that returns a string literal and it's length for functions that take "string,len"
removed unnecessary calls to strlen() in Curl_compareheader by using the CONSTLEN macro for our string literals
removed unnecessary calls to strlen() in Curl_compareheader by using the CONSTLEN macro for our string literals
removed unnecessary calls to strlen() in Curl_compareheader by using the CONSTLEN macro for our string literals
@HenrikHolst HenrikHolst changed the title Patch 4 Patch 4: removed unnecessary calls to strlen() in Curl_compareheader by using a new CONSTLEN macro for our string literals Feb 4, 2022
@jay
Copy link
Member

@jay jay commented Feb 4, 2022

Ref: https://curl.se/mail/lib-2022-02/0042.html

#define CONSTLEN(x) x,sizeof(x)-1

I'm not a fan of this style because it hides a parameter behind a macro. I'd rather we just leave the strlens but judging by that e-mail thread I seem to be in the minority.

@jay jay added the tidy-up label Feb 4, 2022
@HenrikHolst
Copy link
Contributor Author

@HenrikHolst HenrikHolst commented Feb 4, 2022

@dfandrich
Copy link
Collaborator

@dfandrich dfandrich commented Feb 4, 2022

@HenrikHolst
Copy link
Contributor Author

@HenrikHolst HenrikHolst commented Feb 4, 2022

@bagder
Copy link
Member

@bagder bagder commented Feb 4, 2022

I'm not a fan of this style because it hides a parameter behind a macro. I'd rather we just leave the strlens but judging by that e-mail thread I seem to be in the minority.

I'm generally against hiding code behind macros, but I find @HenrikHolst's argument about not having to repeat the string convincing. I can't think of a better way to do this change than what is being proposed here, so I'm a 👍 on it.

@HenrikHolst
Copy link
Contributor Author

@HenrikHolst HenrikHolst commented Feb 5, 2022

Not sure if this is the proper channel to continue this discussion in this manner but if/when this patch gets accepted then I have also identified a few other areas where the CONSTLEN macro will be useful.

One such case is many calls like this:
result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");

That should either be changed to:
result = Curl_dyn_addn(&stream->header_recvbuf, "\r\n", 2);

Or my preferred way:
result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST("\r\n"));

And I would prefer to do it that way (but I can do it the ,2 way if people tell me to) so that if the string is changed we are sure that the length is also changed.

And before the optimizing compiler comes up again, I have compiled curl with -O3 and still a trace gives:

            [226428] |                       Curl_http_bodysend() {
            [226428] |                         Curl_dyn_add() {
   0.180 us [226428] |                           strlen("^M\n") = 2;
            [226428] |                           dyn_nappend() {
   0.190 us [226428] |                             memcpy(0x557fb893f2cf, &__PRETTY_FUNCTION__.11279, 2);
   0.471 us [226428] |                           } /* dyn_nappend */
   1.143 us [226428] |                         } /* Curl_dyn_add */

So at least GCC does not magically switch to use the CUrl_dyn_addn function instead. We can also see the cost here, it's 0.180 µs on my Ryzen 1600X even for such a small string so even if we reduce the amount by say 10 such small strlen:s we are still only talking about saving ~2µs so the savings are not enormous, but they are there.

@bagder
Copy link
Member

@bagder bagder commented Feb 5, 2022

result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST("\r\n"));

I could live with this approach. This would just become a standard internal macro we would learn to live with and use. I think STRCONST() might be a better name than CONSTLEN().

HenrikHolst added 3 commits Feb 5, 2022
Renamed CONSTLEN -> STRCONST
Renamed CONSTLEN -> STRCONST
@HenrikHolst
Copy link
Contributor Author

@HenrikHolst HenrikHolst commented Feb 5, 2022

I agree that STRCONST is the better name so I've changed it in the pull request.

bagder
bagder approved these changes Feb 7, 2022
@bagder
Copy link
Member

@bagder bagder commented Feb 7, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants