summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-04-01 01:12:32 +0200
committerDaniel Stenberg <daniel@haxx.se>2017-04-01 01:12:32 +0200
commit32ccb71b640f42482b6ca7e365dcb47eea4d0018 (patch)
treed72f0ecd35476c95765415e6503ab9d4c73f6db8
parent76c21ed3fda20c55201c734a9d47af02ab7f6e46 (diff)
downloadcurl-bagder/hash-less-malloc.tar.gz
hash: move key into hash struct to reduce mallocsbagder/hash-less-malloc
This removes one malloc for each hash struct allocated. In the simplest case like "curl localhost", my test case went from 112 mallocs to 109.
-rw-r--r--lib/hash.c28
-rw-r--r--lib/hash.h4
2 files changed, 10 insertions, 22 deletions
diff --git a/lib/hash.c b/lib/hash.c
index c26be0118..6fb04704b 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -37,8 +37,6 @@ hash_element_dtor(void *user, void *element)
struct curl_hash *h = (struct curl_hash *) user;
struct curl_hash_element *e = (struct curl_hash_element *) element;
- Curl_safefree(e->key);
-
if(e->ptr) {
h->dtor(e->ptr);
e->ptr = NULL;
@@ -98,23 +96,14 @@ Curl_hash_init(struct curl_hash *h,
static struct curl_hash_element *
mk_hash_element(const void *key, size_t key_len, const void *p)
{
- struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
-
+ /* allocate the struct plus memory after it to store the key */
+ struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element) +
+ key_len);
if(he) {
- void *dupkey = malloc(key_len);
- if(dupkey) {
- /* copy the key */
- memcpy(dupkey, key, key_len);
-
- he->key = dupkey;
- he->key_len = key_len;
- he->ptr = (void *) p;
- }
- else {
- /* failed to duplicate the key, free memory and fail */
- free(he);
- he = NULL;
- }
+ /* copy the key */
+ memcpy(he->key, key, key_len);
+ he->key_len = key_len;
+ he->ptr = (void *) p;
}
return he;
}
@@ -156,7 +145,6 @@ Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
* "destructor" for the actual data 'p'. When we fail, we shall not touch
* that data.
*/
- free(he->key);
free(he);
}
diff --git a/lib/hash.h b/lib/hash.h
index a5a6cac72..bc03672f3 100644
--- a/lib/hash.h
+++ b/lib/hash.h
@@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -58,8 +58,8 @@ struct curl_hash {
struct curl_hash_element {
void *ptr;
- char *key;
size_t key_len;
+ char key[1]; /* allocated memory following the struct */
};
struct curl_hash_iterator {