commit 50d37a404599820009def6cfc703e1b3d4384be3
parent 76e518454889cc3a869ed522ad2025a4c3033b61
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 6 Apr 2023 08:21:41 -0400
Pop links more efficiently by re-using link**
Diffstat:
2 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/include/hmap.h b/include/hmap.h
@@ -100,7 +100,7 @@ void hmap_clear(const struct hmap *map);
struct hmap_link **hmap_link_get(const struct hmap *map, struct hmap_key key);
struct hmap_link **hmap_link_pos(const struct hmap *map, struct hmap_key key);
-struct hmap_link *hmap_link_pop(const struct hmap *map, struct hmap_key key);
+struct hmap_link *hmap_link_pop(const struct hmap *map, struct hmap_link **linkp);
int hmap_link_alloc(const struct hmap *map, struct hmap_link **out,
struct hmap_key key, struct hmap_val value);
diff --git a/src/hmap.c b/src/hmap.c
@@ -154,19 +154,16 @@ hmap_link_pos(const struct hmap *map, struct hmap_key key)
}
struct hmap_link *
-hmap_link_pop(const struct hmap *map, struct hmap_key key)
+hmap_link_pop(const struct hmap *map, struct hmap_link **link)
{
- struct hmap_link **iter;
+ struct hmap_link *tmp;
- LIBHMAP_ABORT_ON_ARGS(!map);
+ LIBHMAP_ABORT_ON_ARGS(!map || !link);
- iter = hmap_link_get(map, key);
- if (iter) {
- *iter = (*iter)->next;
- return *iter;
- }
+ tmp = *link;
+ *link = (*link)->next;
- return NULL;
+ return tmp;
}
int
@@ -218,13 +215,14 @@ hmap_set(const struct hmap *map, struct hmap_key key, struct hmap_val value)
int
hmap_rm(const struct hmap *map, struct hmap_key key)
{
- struct hmap_link *link;
+ struct hmap_link *link, **linkp;
int rc;
LIBHMAP_ABORT_ON_ARGS(!map);
- link = hmap_link_pop(map, key);
- if (!link) return HMAP_KEY_MISSING;
+ linkp = hmap_link_get(map, key);
+ if (!linkp) return HMAP_KEY_MISSING;
+ link = hmap_link_pop(map, linkp);
rc = map->allocator->free(link);
if (rc) return -rc;