strmap: take advantage of FLEXPTR_ALLOC_STR when relevant
By default, we do not use a mempool and strdup_strings is true; in this case, we can avoid both an extra allocation and an extra free by just over-allocating for the strmap_entry leaving enough space at the end to copy the key. FLEXPTR_ALLOC_STR exists for exactly this purpose, so make use of it. Also, adjust the case when we are using a memory pool and strdup_strings is true to just do one allocation from the memory pool instead of two so that the strmap_clear() and strmap_remove() code can just avoid freeing the key in all cases. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
a208ec1f0b
commit
23a276a9c4
35
strmap.c
35
strmap.c
@ -59,11 +59,8 @@ static void strmap_free_entries_(struct strmap *map, int free_values)
|
|||||||
hashmap_for_each_entry(&map->map, &iter, e, ent) {
|
hashmap_for_each_entry(&map->map, &iter, e, ent) {
|
||||||
if (free_values)
|
if (free_values)
|
||||||
free(e->value);
|
free(e->value);
|
||||||
if (!map->pool) {
|
if (!map->pool)
|
||||||
if (map->strdup_strings)
|
|
||||||
free((char*)e->key);
|
|
||||||
free(e);
|
free(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,16 +81,25 @@ static struct strmap_entry *create_entry(struct strmap *map,
|
|||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
struct strmap_entry *entry;
|
struct strmap_entry *entry;
|
||||||
const char *key = str;
|
|
||||||
|
|
||||||
entry = map->pool ? mem_pool_alloc(map->pool, sizeof(*entry))
|
if (map->strdup_strings) {
|
||||||
: xmalloc(sizeof(*entry));
|
if (!map->pool) {
|
||||||
|
FLEXPTR_ALLOC_STR(entry, key, str);
|
||||||
|
} else {
|
||||||
|
size_t len = st_add(strlen(str), 1); /* include NUL */
|
||||||
|
entry = mem_pool_alloc(map->pool,
|
||||||
|
st_add(sizeof(*entry), len));
|
||||||
|
memcpy(entry + 1, str, len);
|
||||||
|
entry->key = (void *)(entry + 1);
|
||||||
|
}
|
||||||
|
} else if (!map->pool) {
|
||||||
|
entry = xmalloc(sizeof(*entry));
|
||||||
|
} else {
|
||||||
|
entry = mem_pool_alloc(map->pool, sizeof(*entry));
|
||||||
|
}
|
||||||
hashmap_entry_init(&entry->ent, strhash(str));
|
hashmap_entry_init(&entry->ent, strhash(str));
|
||||||
|
if (!map->strdup_strings)
|
||||||
if (map->strdup_strings)
|
entry->key = str;
|
||||||
key = map->pool ? mem_pool_strdup(map->pool, str)
|
|
||||||
: xstrdup(str);
|
|
||||||
entry->key = key;
|
|
||||||
entry->value = data;
|
entry->value = data;
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
@ -139,11 +145,8 @@ void strmap_remove(struct strmap *map, const char *str, int free_value)
|
|||||||
return;
|
return;
|
||||||
if (free_value)
|
if (free_value)
|
||||||
free(ret->value);
|
free(ret->value);
|
||||||
if (!map->pool) {
|
if (!map->pool)
|
||||||
if (map->strdup_strings)
|
|
||||||
free((char*)ret->key);
|
|
||||||
free(ret);
|
free(ret);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
|
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
|
||||||
|
1
strmap.h
1
strmap.h
@ -14,6 +14,7 @@ struct strmap_entry {
|
|||||||
struct hashmap_entry ent;
|
struct hashmap_entry ent;
|
||||||
const char *key;
|
const char *key;
|
||||||
void *value;
|
void *value;
|
||||||
|
/* strmap_entry may be allocated extra space to store the key at end */
|
||||||
};
|
};
|
||||||
|
|
||||||
int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
|
int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
|
||||||
|
Loading…
Reference in New Issue
Block a user