]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
conncache: eliminate `conn->destination_len` as premature optimization
authorStefan Eissing <stefan@eissing.org>
Sat, 22 Mar 2025 09:47:08 +0000 (10:47 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 24 Mar 2025 08:56:13 +0000 (09:56 +0100)
Closes #16792

lib/conncache.c
lib/conncache.h
lib/url.c
lib/urldata.h

index 584185cc067a171f0ba25bf25fd3f273854158ea..5f8229303f9444e153d2cd5a416dab5778d50b71 100644 (file)
@@ -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) {
index ab99c309af7d27c324b170681f949ae13d35e17f..1e65660423da74d50001ff0257c7f22d1e1c3854 100644 (file)
@@ -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);
index a745090b5f6fc9fded97f3f348f81bb89a3c8794..06f198ac33883938aec4da50e3980a96fe2c1fc5 100644 (file)
--- 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;
 }
index 889d6029ef88f6fdc35a48a3f7238f8f8d99f3b4..0988b91fa7cab2240171553a1963c772448e0266 100644 (file)
@@ -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