curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Fewer mallocs is better, episode #47

From: Henrik Holst via curl-library <curl-library_at_lists.haxx.se>
Date: Fri, 4 Feb 2022 16:51:59 +0100

Something like that yes, however instead of relying on every compiler to
optimize this and to not have to write the same string literal twice my
plan would be to add a macto

#define CONSTLEN(a,b) a,sizeof(b)-1

that way you can do

-Curl_compareheader(ptr, "Expect:", "100-continue");
+Curl_compareheader(ptr, CONSTLEN("Expect:"), CONSTLEN("100-continue"));

Just have to have consensus on what to call this macro, looking at the
codebase there is a strong use of CURL_ prefixes so perhaps CURL_CONSTLEN
or something like that?

/HH

Den fre 4 feb. 2022 kl 13:43 skrev Cristian Rodríguez <
crrodriguez_at_opensuse.org>:

> something like:
>
> diff --git a/lib/http.c b/lib/http.c
> index c036d877e..b62dec269 100644
> --- a/lib/http.c
> +++ b/lib/http.c
> _at__at_ -1423,17 +1423,17 _at__at_ CURLcode Curl_buffer_send(struct dynbuf *in,
> * Pass headers WITH the colon.
> */
> bool
> -Curl_compareheader(const char *headerline, /* line to check */
> +Curl_compareheader_ex(const char *headerline, /* line to check */
> const char *header, /* header keyword _with_ colon */
> - const char *content) /* content string to find */
> + size_t hlen,
> + const char *content,
> + size_t clen) /* content string to find */
> {
> /* RFC2616, section 4.2 says: "Each header field consists of a name
> followed
> * by a colon (":") and the field value. Field names are
> case-insensitive.
> * The field value MAY be preceded by any amount of LWS, though a
> single SP
> * is preferred." */
>
> - size_t hlen = strlen(header);
> - size_t clen;
> size_t len;
> const char *start;
> const char *end;
> _at__at_ -1456,11 +1456,10 _at__at_ Curl_compareheader(const char *headerline, /*
> line to check */
>
> if(!end)
> /* hm, there's no line ending here, use the zero byte! */
> - end = strchr(start, '\0');
> + end = start + strlen(start);
> }
>
> len = end-start; /* length of the content part of the input line */
> - clen = strlen(content); /* length of the word to find */
>
> /* find the content string in the rest of the line */
> for(; len >= clen; len--, start++) {
> diff --git a/lib/http.h b/lib/http.h
> index b4aaba2a2..da7409825 100644
> --- a/lib/http.h
> +++ b/lib/http.h
> _at__at_ -44,10 +44,15 _at__at_ extern const struct Curl_handler Curl_handler_http;
> extern const struct Curl_handler Curl_handler_https;
> #endif
>
> +#define Curl_compareheader(headerline, header, content) \
> + Curl_compareheader_ex(headerline, header, strlen(header),
> content, strlen(content))
> +
> /* Header specific functions */
> -bool Curl_compareheader(const char *headerline, /* line to check */
> +bool Curl_compareheader_ex(const char *headerline, /* line to check */
> const char *header, /* header keyword _with_
> colon */
> - const char *content); /* content string to find */
> + size_t hlen, /* strlen of header */
> + const char *content, /* content string to find */
> + size_t clen); /* content string length */
>
> char *Curl_copy_header_value(const char *header);
>
>
> With optimization enabled will probably do what you want.
>
> On Fri, Feb 4, 2022 at 8:00 AM Cristian Rodríguez
> <crrodriguez_at_opensuse.org> wrote:
> >
> > On Thu, Feb 3, 2022 at 10:09 PM Henrik Holst via curl-library
> > <curl-library_at_lists.haxx.se> wrote:
> >
> > > 23% of those for curl.se and 8% for google.com is due to
> Curl_compareheader, this looks like an internal only function and every
> single use inside curl is using string literals so we can replace them all
> with a compile time sizeof, if patches for this would be accepted then I
> would be happy to provide such.
> >
> > Well.. add a size_t header_len and content_len parameter to
> > Curl_compareheader .. or provide an inline version of it, any compiler
> > from the last decade will then do constant folding on strlen("test")
> > when optimization is enabled.
>


-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2022-02-04