aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2021-08-22 09:54:21 +0200
committerBjörn Linse <bjorn.linse@gmail.com>2021-08-22 09:54:21 +0200
commit9e651a9d097c5715c0f025555814fa0ad18ca8cd (patch)
treea5ef7d056f6864f114e1285048e2a3f1e5bb9a51 /src
parent8331cd13c45fb75bff0cec328ccba79b3ae61fa5 (diff)
downloadrneovim-9e651a9d097c5715c0f025555814fa0ad18ca8cd.tar.gz
rneovim-9e651a9d097c5715c0f025555814fa0ad18ca8cd.tar.bz2
rneovim-9e651a9d097c5715c0f025555814fa0ad18ca8cd.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/nvim/eval.c2
-rw-r--r--src/nvim/map.c40
-rw-r--r--src/nvim/map.h8
3 files changed, 25 insertions, 25 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 789936fdff..36b04c24bf 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -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) {
diff --git a/src/nvim/map.c b/src/nvim/map.c
index 86dc257e40..10baa82f5a 100644
--- a/src/nvim/map.c
+++ b/src/nvim/map.c
@@ -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)
diff --git a/src/nvim/map.h b/src/nvim/map.h
index a35a2c1672..f670a70a89 100644
--- a/src/nvim/map.h
+++ b/src/nvim/map.h
@@ -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);