mirror of
https://github.com/moparisthebest/curl
synced 2024-12-21 23:58:49 -05:00
hash: move key into hash struct to reduce mallocs
This removes one tiny malloc for each hash struct allocated. In a simple case like "curl localhost", this save three mallocs. Closes #1376
This commit is contained in:
parent
e60fe20fdf
commit
4f2e348f9b
26
lib/hash.c
26
lib/hash.c
@ -37,8 +37,6 @@ hash_element_dtor(void *user, void *element)
|
|||||||
struct curl_hash *h = (struct curl_hash *) user;
|
struct curl_hash *h = (struct curl_hash *) user;
|
||||||
struct curl_hash_element *e = (struct curl_hash_element *) element;
|
struct curl_hash_element *e = (struct curl_hash_element *) element;
|
||||||
|
|
||||||
Curl_safefree(e->key);
|
|
||||||
|
|
||||||
if(e->ptr) {
|
if(e->ptr) {
|
||||||
h->dtor(e->ptr);
|
h->dtor(e->ptr);
|
||||||
e->ptr = NULL;
|
e->ptr = NULL;
|
||||||
@ -87,23 +85,14 @@ Curl_hash_init(struct curl_hash *h,
|
|||||||
static struct curl_hash_element *
|
static struct curl_hash_element *
|
||||||
mk_hash_element(const void *key, size_t key_len, const void *p)
|
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) {
|
if(he) {
|
||||||
void *dupkey = malloc(key_len);
|
/* copy the key */
|
||||||
if(dupkey) {
|
memcpy(he->key, key, key_len);
|
||||||
/* copy the key */
|
he->key_len = key_len;
|
||||||
memcpy(dupkey, key, key_len);
|
he->ptr = (void *) p;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return he;
|
return he;
|
||||||
}
|
}
|
||||||
@ -145,7 +134,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
|
* "destructor" for the actual data 'p'. When we fail, we shall not touch
|
||||||
* that data.
|
* that data.
|
||||||
*/
|
*/
|
||||||
free(he->key);
|
|
||||||
free(he);
|
free(he);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +58,8 @@ struct curl_hash {
|
|||||||
|
|
||||||
struct curl_hash_element {
|
struct curl_hash_element {
|
||||||
void *ptr;
|
void *ptr;
|
||||||
char *key;
|
|
||||||
size_t key_len;
|
size_t key_len;
|
||||||
|
char key[1]; /* allocated memory following the struct */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct curl_hash_iterator {
|
struct curl_hash_iterator {
|
||||||
|
Loading…
Reference in New Issue
Block a user