From: Stefan Eissing Date: Sat, 22 Mar 2025 09:47:08 +0000 (+0100) Subject: conncache: eliminate `conn->destination_len` as premature optimization X-Git-Tag: curl-8_13_0~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec4e2cd15d2abad0c2ab1d8f169343de8bdce025;p=thirdparty%2Fcurl.git conncache: eliminate `conn->destination_len` as premature optimization Closes #16792 --- diff --git a/lib/conncache.c b/lib/conncache.c index 584185cc06..5f8229303f 100644 --- a/lib/conncache.c +++ b/lib/conncache.c @@ -88,16 +88,17 @@ static void cpool_discard_conn(struct cpool *cpool, struct connectdata *conn, bool aborted); -static struct cpool_bundle *cpool_bundle_create(const char *dest, - size_t dest_len) +static struct cpool_bundle *cpool_bundle_create(const char *dest) { struct cpool_bundle *bundle; + size_t dest_len = strlen(dest); + bundle = calloc(1, sizeof(*bundle) + dest_len); if(!bundle) return NULL; Curl_llist_init(&bundle->conns, NULL); - bundle->dest_len = dest_len; - memcpy(bundle->dest, dest, dest_len); + bundle->dest_len = dest_len + 1; + memcpy(bundle->dest, dest, bundle->dest_len); return bundle; } @@ -176,7 +177,7 @@ static struct cpool_bundle *cpool_find_bundle(struct cpool *cpool, struct connectdata *conn) { return Curl_hash_pick(&cpool->dest2bundle, - conn->destination, conn->destination_len); + conn->destination, strlen(conn->destination) + 1); } @@ -276,7 +277,7 @@ cpool_add_bundle(struct cpool *cpool, struct connectdata *conn) { struct cpool_bundle *bundle; - bundle = cpool_bundle_create(conn->destination, conn->destination_len); + bundle = cpool_bundle_create(conn->destination); if(!bundle) return NULL; @@ -551,7 +552,7 @@ bool Curl_cpool_conn_now_idle(struct Curl_easy *data, } bool Curl_cpool_find(struct Curl_easy *data, - const char *destination, size_t dest_len, + const char *destination, Curl_cpool_conn_match_cb *conn_cb, Curl_cpool_done_match_cb *done_cb, void *userdata) @@ -567,7 +568,8 @@ bool Curl_cpool_find(struct Curl_easy *data, CPOOL_LOCK(cpool, data); bundle = Curl_hash_pick(&cpool->dest2bundle, - CURL_UNCONST(destination), dest_len); + CURL_UNCONST(destination), + strlen(destination) + 1); if(bundle) { struct Curl_llist_node *curr = Curl_llist_head(&bundle->conns); while(curr) { diff --git a/lib/conncache.h b/lib/conncache.h index ab99c309af..1e65660423 100644 --- a/lib/conncache.h +++ b/lib/conncache.h @@ -121,14 +121,13 @@ typedef bool Curl_cpool_done_match_cb(bool result, void *userdata); * All callbacks are invoked while the pool's lock is held. * @param data current transfer * @param destination match agaonst `conn->destination` in pool - * @param dest_len destination length, including terminating NUL * @param conn_cb must be present, called for each connection in the * bundle until it returns TRUE * @return combined result of last conn_db and result_cb or FALSE if no connections were present. */ bool Curl_cpool_find(struct Curl_easy *data, - const char *destination, size_t dest_len, + const char *destination, Curl_cpool_conn_match_cb *conn_cb, Curl_cpool_done_match_cb *done_cb, void *userdata); diff --git a/lib/url.c b/lib/url.c index a745090b5f..06f198ac33 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1288,7 +1288,7 @@ ConnectionExists(struct Curl_easy *data, /* Find a connection in the pool that matches what "data + needle" * requires. If a suitable candidate is found, it is attached to "data". */ - result = Curl_cpool_find(data, needle->destination, needle->destination_len, + result = Curl_cpool_find(data, needle->destination, url_match_conn, url_match_result, &match); /* wait_pipe is TRUE if we encounter a bundle that is undecided. There @@ -2064,9 +2064,8 @@ static CURLcode setup_connection_internals(struct Curl_easy *data, if(!conn->destination) return CURLE_OUT_OF_MEMORY; - conn->destination_len = strlen(conn->destination) + 1; Curl_strntolower(conn->destination, conn->destination, - conn->destination_len - 1); + strlen(conn->destination)); return CURLE_OK; } diff --git a/lib/urldata.h b/lib/urldata.h index 889d6029ef..0988b91fa7 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -770,7 +770,6 @@ struct connectdata { curl_off_t connection_id; /* Contains a unique number to make it easier to track the connections in the log output */ char *destination; /* string carrying normalized hostname+port+scope */ - size_t destination_len; /* strlen(destination) + 1 */ /* 'dns_entry' is the particular host we use. This points to an entry in the DNS cache and it will not get pruned while locked. It gets unlocked in