Skip to content

Commit 6ccdfc2

Browse files
newrengitster
authored andcommitted
strmap: enable faster clearing and reusing of strmaps
When strmaps are used heavily, such as is done by my new merge-ort algorithm, and strmaps need to be cleared but then re-used (because of e.g. picking multiple commits to cherry-pick, or due to a recursive merge having several different merges while recursing), free-ing and reallocating map->table repeatedly can add up in time, especially since it will likely be reallocated to a much smaller size but the previous merge provides a good guide to the right size to use for the next merge. Introduce strmap_partial_clear() to take advantage of this type of situation; it will act similar to strmap_clear() except that map->table's entries are zeroed instead of map->table being free'd. Making use of this function reduced the cost of clear_or_reinit_internal_opts() by about 20% in mert-ort, and dropped the overall runtime of my rebase testcase by just under 2%. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b70c82e commit 6ccdfc2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

strmap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ void strmap_clear(struct strmap *map, int free_values)
6464
hashmap_clear(&map->map);
6565
}
6666

67+
void strmap_partial_clear(struct strmap *map, int free_values)
68+
{
69+
strmap_free_entries_(map, free_values);
70+
hashmap_partial_clear(&map->map);
71+
}
72+
6773
void *strmap_put(struct strmap *map, const char *str, void *data)
6874
{
6975
struct strmap_entry *entry = find_strmap_entry(map, str);

strmap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ void strmap_init_with_options(struct strmap *map,
4242
*/
4343
void strmap_clear(struct strmap *map, int free_values);
4444

45+
/*
46+
* Similar to strmap_clear() but leaves map->map->table allocated and
47+
* pre-sized so that subsequent uses won't need as many rehashings.
48+
*/
49+
void strmap_partial_clear(struct strmap *map, int free_values);
50+
4551
/*
4652
* Insert "str" into the map, pointing to "data".
4753
*

0 commit comments

Comments
 (0)