Skip to content

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

Closed
HenrikHolst wants to merge 7 commits into
curl:masterfrom
HenrikHolst:patch-4
Closed

Patch 4: removed unnecessary calls to strlen() in Curl_compareheader by using a new CONSTLEN macro for our string literals#8391
HenrikHolst wants to merge 7 commits into
curl:masterfrom
HenrikHolst:patch-4

Conversation

@HenrikHolst

Copy link
Copy Markdown
Contributor

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

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

jay commented Feb 4, 2022

Copy link
Copy Markdown
Member

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

HenrikHolst commented Feb 4, 2022 via email

Copy link
Copy Markdown
Contributor Author

@dfandrich

dfandrich commented Feb 4, 2022 via email

Copy link
Copy Markdown
Contributor

@HenrikHolst

HenrikHolst commented Feb 4, 2022 via email

Copy link
Copy Markdown
Contributor Author

@bagder

bagder commented Feb 4, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor Author

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

bagder commented Feb 5, 2022

Copy link
Copy Markdown
Member

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().

Renamed CONSTLEN -> STRCONST
Renamed CONSTLEN -> STRCONST
@HenrikHolst

Copy link
Copy Markdown
Contributor Author

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

@bagder bagder closed this in 4028892 Feb 7, 2022
@bagder

bagder commented Feb 7, 2022

Copy link
Copy Markdown
Member

Thanks!

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

Labels

Development

Successfully merging this pull request may close these issues.

4 participants