perf(map): reduce double pointer indirection to single pointer indirection

the only field of Map(...) was a pointer to a khash_t. make it contain
the struct by value instead.
This commit is contained in:
Björn Linse 2021-08-22 09:54:21 +02:00
parent 8331cd13c4
commit 9e651a9d09
3 changed files with 25 additions and 25 deletions

View File

@ -7331,7 +7331,7 @@ void add_timer_info(typval_T *rettv, timer_T *timer)
void add_timer_info_all(typval_T *rettv)
{
tv_list_alloc_ret(rettv, timers->table->n_occupied);
tv_list_alloc_ret(rettv, map_size(timers));
timer_T *timer;
map_foreach_value(timers, timer, {
if (!timer->stopped) {

View File

@ -56,14 +56,14 @@
\
Map(T, U) *map_##T##_##U##_new() \
{ \
Map(T, U) *rv = xmalloc(sizeof(Map(T, U))); \
rv->table = kh_init(T##_##U##_map); \
Map(T, U) *rv = xcalloc(1, sizeof(Map(T, U))); \
/* khash_t table member is zero-initialized */ \
return rv; \
} \
\
void map_##T##_##U##_free(Map(T, U) *map) \
{ \
kh_destroy(T##_##U##_map, map->table); \
kh_dealloc(T##_##U##_map, &map->table); \
xfree(map); \
} \
\
@ -71,39 +71,39 @@
{ \
khiter_t k; \
\
if ((k = kh_get(T##_##U##_map, map->table, key)) == kh_end(map->table)) { \
if ((k = kh_get(T##_##U##_map, &map->table, key)) == kh_end(&map->table)) { \
return INITIALIZER(T, U); \
} \
\
return kh_val(map->table, k); \
return kh_val(&map->table, k); \
} \
\
bool map_##T##_##U##_has(Map(T, U) *map, T key) \
{ \
return kh_get(T##_##U##_map, map->table, key) != kh_end(map->table); \
return kh_get(T##_##U##_map, &map->table, key) != kh_end(&map->table); \
} \
\
T map_##T##_##U##_key(Map(T, U) *map, T key) \
{ \
khiter_t k; \
\
if ((k = kh_get(T##_##U##_map, map->table, key)) == kh_end(map->table)) { \
if ((k = kh_get(T##_##U##_map, &map->table, key)) == kh_end(&map->table)) { \
abort(); /* Caller must check map_has(). */ \
} \
\
return kh_key(map->table, k); \
return kh_key(&map->table, k); \
} \
U map_##T##_##U##_put(Map(T, U) *map, T key, U value) \
{ \
int ret; \
U rv = INITIALIZER(T, U); \
khiter_t k = kh_put(T##_##U##_map, map->table, key, &ret); \
khiter_t k = kh_put(T##_##U##_map, &map->table, key, &ret); \
\
if (!ret) { \
rv = kh_val(map->table, k); \
rv = kh_val(&map->table, k); \
} \
\
kh_val(map->table, k) = value; \
kh_val(&map->table, k) = value; \
return rv; \
} \
\
@ -112,18 +112,18 @@
int ret; \
khiter_t k; \
if (put) { \
k = kh_put(T##_##U##_map, map->table, key, &ret); \
k = kh_put(T##_##U##_map, &map->table, key, &ret); \
if (ret) { \
kh_val(map->table, k) = INITIALIZER(T, U); \
kh_val(&map->table, k) = INITIALIZER(T, U); \
} \
} else { \
k = kh_get(T##_##U##_map, map->table, key); \
if (k == kh_end(map->table)) { \
k = kh_get(T##_##U##_map, &map->table, key); \
if (k == kh_end(&map->table)) { \
return NULL; \
} \
} \
\
return &kh_val(map->table, k); \
return &kh_val(&map->table, k); \
} \
\
U map_##T##_##U##_del(Map(T, U) *map, T key) \
@ -131,9 +131,9 @@
U rv = INITIALIZER(T, U); \
khiter_t k; \
\
if ((k = kh_get(T##_##U##_map, map->table, key)) != kh_end(map->table)) { \
rv = kh_val(map->table, k); \
kh_del(T##_##U##_map, map->table, k); \
if ((k = kh_get(T##_##U##_map, &map->table, key)) != kh_end(&map->table)) { \
rv = kh_val(&map->table, k); \
kh_del(T##_##U##_map, &map->table, k); \
} \
\
return rv; \
@ -141,7 +141,7 @@
\
void map_##T##_##U##_clear(Map(T, U) *map) \
{ \
kh_clear(T##_##U##_map, map->table); \
kh_clear(T##_##U##_map, &map->table); \
}
static inline khint_t String_hash(String s)

View File

@ -18,7 +18,7 @@
KHASH_DECLARE(T##_##U##_map, T, U) \
\
typedef struct { \
khash_t(T##_##U##_map) *table; \
khash_t(T##_##U##_map) table; \
} Map(T, U); \
\
Map(T, U) *map_##T##_##U##_new(void); \
@ -68,7 +68,7 @@ MAP_DECLS(ColorKey, ColorItem)
#define map_del(T, U) map_##T##_##U##_del
#define map_clear(T, U) map_##T##_##U##_clear
#define map_size(map) ((map)->table->size)
#define map_size(map) ((map)->table.size)
#define pmap_new(T) map_new(T, ptr_t)
#define pmap_free(T) map_free(T, ptr_t)
@ -82,10 +82,10 @@ MAP_DECLS(ColorKey, ColorItem)
#define pmap_clear(T) map_clear(T, ptr_t)
#define map_foreach(map, key, value, block) \
kh_foreach(map->table, key, value, block)
kh_foreach(&map->table, key, value, block)
#define map_foreach_value(map, value, block) \
kh_foreach_value(map->table, value, block)
kh_foreach_value(&map->table, value, block)
void pmap_del2(PMap(cstr_t) *map, const char *key);