switch to tombstone-free khashl table
cover switch to tombstone-free khashl table
This is another step in slowly reducing memory usage of `git gc' and associated tasks. khashl is an updated version of khash which eliminates tombstones for deleted elements to save space.
The overall memory improvement with our codebase is tiny (a few dozen KB out of several GB at peak, about 10MB over the lifetime of a process). Any memory reduction at all is welcome at this point; and khashl comes with some new optional features and enhancements which may come in handy someday.
I haven't been able to validate CPU-dependent improvements claimed by the author due to system noise from working on a shared server. No aberrant behavior has been noticed in day-to-day `production' use on public facing servers.
Keep in mind the switch linear probing for performance is consistent with findings made for the open-coded ->obj_hash in object.c
Fortunately, this set of changes is unintrusive; but I'm hoping to have more time to make deeper changes this year.
Eric Wong (3):
list-objects-filter: use kh_size API
treewide: switch to khashl for memory savings
khashl: fix ensemble lookups on empty table
builtin/fast-import.c | 2 +- builtin/fsmonitor--daemon.c | 4 +- delta-islands.c | 4 +- khash.h | 338 ----------------------- khashl.h | 522 ++++++++++++++++++++++++++++++++++++ list-objects-filter.c | 2 +- object-store-ll.h | 2 +- object-store.h | 7 +- oidset.h | 2 +- pack-bitmap.h | 2 +- 10 files changed, 535 insertions(+), 350 deletions(-) delete mode 100644 khash.h create mode 100644 khashl.h
Range-diff:
-: ---------- > 1: 3bf3148cab list-objects-filter: use kh_size API
1: e74965907e ! 2: 09900edb48 treewide: switch to khashl for memory savings
@@ Commit message
khashl is an updated version of khash with less memory overhead
(one bit/bucket instead of two) than the original khash and
- similar overall performance. Insertions are simpler (linear
- probing) but deletions may be slightly slower[1]. Of course,
- the majority of hash tables in git do not delete individual
- elements.
+ similar overall performance. According to its author,
+ insertions are simpler (linear probing) but deletions may be
+ slightly slower[1]. Of course, the majority of hash tables in
+ git do not delete individual elements.
Overall memory usage did not decrease much, as the hash tables
and elements we store in them are big and currently dwarf the
overhead of the khash internals. Only around 10 MB in
- allocations (not peak use) is saved when doing a no-op `git gc'
- of a Linux kernel object store with thousands of refs and
- islands.
+ allocations (and a few dozen KB peak use out of ~6 GB) is saved
+ when doing a no-op `git gc' of a Linux kernel object store with
+ thousands of refs and islands.
A summary of differences I've found from khash to khashl:
@@ Commit message
* flesh out KHASHL_{SET,MAP}_INIT wrappers with *_clear, *_resize,
and *_release functions
+ * sparse fixes from Junio and Jeff
+
[1] https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/
[2] git clone https://github.com/attractivechaos/klib.git
2895a16cb55e (support an ensemble of hash tables, 2023-12-18)
@@ Commit message
typedef) and was the only place where I had to change a definition.
Signed-off-by: Eric Wong <[email protected]>
+ Helped-by: Junio C Hamano <[email protected]>
+ Helped-by: Jeff King <[email protected]>
## builtin/fast-import.c ##
@@
@@ khashl.h (new)
+#define __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
+ SCOPE khint_t prefix##_getp_core(const HType *h, const khkey_t *key, khint_t hash) { \
+ khint_t i, last, n_buckets, mask; \
-+ if (h->keys == 0) return 0; \
++ if (!h->keys) return 0; \
+ n_buckets = (khint_t)1U << h->bits; \
+ mask = n_buckets - 1U; \
+ i = last = __kh_h2b(hash, h->bits); \
@@ khashl.h (new)
+
+#define __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
+ SCOPE void prefix##_resize(HType *h, khint_t new_n_buckets) { \
-+ khint32_t *new_used = 0; \
++ khint32_t *new_used = NULL; \
+ khint_t j = 0, x = new_n_buckets, n_buckets, new_bits, new_mask; \
+ while ((x >>= 1) != 0) ++j; \
+ if (new_n_buckets & (new_n_buckets - 1)) ++j; \
@@ khashl.h (new)
+#define __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn) \
+ SCOPE int prefix##_del(HType *h, khint_t i) { \
+ khint_t j = i, k, mask, n_buckets; \
-+ if (h->keys == 0) return 0; \
++ if (!h->keys) return 0; \
+ n_buckets = (khint_t)1U<<h->bits; \
+ mask = n_buckets - 1U; \
+ while (1) { \2: 744e1b7198 = 3: bfb20eae37 khashl: fix ensemble lookups on empty table
PATCH 1/3 list-objects-filter: use kh_size API
In order to ease a potential migration to from khash to khashl, use the kh_size() macro instead of accessing the .size field directly.
--- list-objects-filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
list-objects-filter.c +1 -1
@@ -704,7 +704,7 @@ static void filter_combine__free(void *filter_data)for (sub =0 ; sub < d->nr; sub++) {list_objects_filter__free (d->sub[sub].filter);oidset_clear (&d->sub[sub].seen); -if (d->sub[sub].omits.set.size) +if (kh_size (&d->sub[sub].omits.set))BUG ("expected oidset to be cleared already" ); }free (d->sub);
PATCH 2/3 treewide: switch to khashl for memory savings
In reply to Eric Wongkhashl is an updated version of khash with less memory overhead (one bit/bucket instead of two) than the original khash and similar overall performance. According to its author, insertions are simpler (linear probing) but deletions may be slightly slower[1]. Of course, the majority of hash tables in git do not delete individual elements.
Overall memory usage did not decrease much, as the hash tables and elements we store in them are big and currently dwarf the overhead of the khash internals. Only around 10 MB in allocations (and a few dozen KB peak use out of ~6 GB) is saved when doing a no-op `git gc' of a Linux kernel object store with thousands of refs and islands.
A summary of differences I've found from khash to khashl:
* two 32-bit ints (instead of four) in the top-level struct
* 2 heap allocations (instead of 3) for maps
(though I wonder locality suffers when probing is necessary)
* 1 bit of metadata per-bucket (no tombstones for deleted elements)
* 0.75 load factor. Lowered slightly from 0.77, but no FP multiply and responsible for the aforementioned struct size reduction
* FNV-1A instead of x31 hash for strings
* Fibonacci hashing (__kh_h2b), probably good for FNV-1A, but I'm skeptical of its usefulness for our SHA-* using cases
* linear probing instead of quadratic
* Wang's integer hash functions (currently unused)
* optional hash value caching and ensemble APIs (currently unused)
* some API differences (see below), but not enough to easily use both khash and khashl in the same compilation unit
This patch was made with two additional goals to ease review:
1) minimize changes outside of khash*.h files
2) minimize and document all differences from upstream[2] khashl.h
Our khashl.h differences from upstream:
* favor portability constructs from our codebase:
MAYBE_UNUSED over klib_unused, inline over kh_inline, and various integer types
* disable packed attribute to satisfy -Werror=address-of-packed-member, AFAIK it doesn't change any of the data structures we use
* port the following commits over from our old khash.h:
9249ca26aca3 (khash: factor out kh_release_*, 2018-10-04)
2756ca4347cb (use REALLOC_ARRAY for changing the allocation size of arrays, 2014-09-16) 5632e838f8fa (khash: clarify that allocations never fail, 2021-07-03)
* use our memory allocation wrappers
* provide wrappers for compatibility with existing callers using the khash API. The khashl function naming convention is: ${NOUN}_${VERB} while the khash convention is: kh_${VERB}_${NOUN}. The kh_${NAME}_t typedef and naming convention are preserved via __KHASH_COMPAT macro to ease review (despite the `_t' suffix being reserved and typedefs being discouraged in the Linux kernel).
* copy relevant API docs over from khash.h for identically named macros
* preserve kh_begin, kh_foreach, kh_foreach_value from khash.h since khashl.h doesn't provide them
* flesh out KHASHL_{SET,MAP}_INIT wrappers with *_clear, *_resize, and *_release functions
* sparse fixes from Junio and Jeff
[1] https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/
[2] git clone https://github.com/attractivechaos/klib.git
2895a16cb55e (support an ensemble of hash tables, 2023-12-18)
khashl.h API differences from khash.h which affected this change:
* KHASHL_MAP_INIT and KHASHL_SET_INIT macros replace KHASH_INIT
* user-supplied hash and equality functions use different names
* object-store-ll.h avoided the kh_*_t convention (since I dislike typedef) and was the only place where I had to change a definition.
--- builtin/fast-import.c | 2 +- builtin/fsmonitor--daemon.c | 4 +- delta-islands.c | 4 +- khash.h | 338 ----------------------- khashl.h | 522 ++++++++++++++++++++++++++++++++++++ object-store-ll.h | 2 +- object-store.h | 7 +- oidset.h | 2 +- pack-bitmap.h | 2 +- 9 files changed, 534 insertions(+), 349 deletions(-) delete mode 100644 khash.h create mode 100644 khashl.h
builtin/fast-import.c +1 -1
@@ -24,7 +24,7 @@#include "object-store-ll.h" #include "mem-pool.h" #include "commit-reach.h" -#include "khash.h" +#include "khashl.h" #include "date.h" #define PACK_ID_BITS 16
builtin/fsmonitor--daemon.c +2 -2
@@ -13,7 +13,7 @@#include "fsmonitor--daemon.h" #include "repository.h" #include "simple-ipc.h" -#include "khash.h" +#include "khashl.h" #include "run-command.h" #include "trace.h" #include "trace2.h"
@@ -650,7 +650,7 @@ static int fsmonitor_parse_client_token(const char *buf_token,return 0 ; } -KHASH_INIT(str, const char *, int, 0, kh_str_hash_func, kh_str_hash_equal) +KHASHL_SET_INIT(KH_LOCAL, kh_str, str, const char *, kh_hash_str, kh_eq_str) static int do_handle_client(struct fsmonitor_daemon_state *state, const char *command,
delta-islands.c +2 -2
@@ -10,14 +10,14 @@#include "diff.h" #include "progress.h" #include "refs.h" -#include "khash.h" +#include "khashl.h" #include "pack-bitmap.h" #include "pack-objects.h" #include "delta-islands.h" #include "oid-array.h" #include "config.h" -KHASH_INIT(str, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) +KHASHL_MAP_INIT(KH_LOCAL, kh_str, str, const char *, void *, kh_hash_str, kh_eq_str)static kh_oid_map_t *island_marks;static unsigned island_counter;
khash.h deleted +0 -338
338 lines not on this page · raw message
khashl.h new file +522 -0
522 lines not on this page · raw message
object-store-ll.h +1 -1
@@ -160,7 +160,7 @@ struct raw_object_store {*/ struct object_directory *odb;struct object_directory **odb_tail; -struct kh_odb_path_map *odb_by_path; +struct odb_path_map *odb_by_path;int loaded_alternates;
object-store.h +4 -3
@@ -1,11 +1,12 @@#ifndef OBJECT_STORE_H #define OBJECT_STORE_H -#include "khash.h" +#include "khashl.h" #include "dir.h" #include "object-store-ll.h" -KHASH_INIT(odb_path_map, const char * /* key: odb_path */, - struct object_directory *, 1, fspathhash, fspatheq) +KHASHL_MAP_INIT (KH_LOCAL , odb_path_map, odb_path_map, + const char * /* key: odb_path */, struct object_directory *, + fspathhash, fspatheq)#endif /* OBJECT_STORE_H */
oidset.h +1 -1
@@ -1,7 +1,7 @@#ifndef OIDSET_H #define OIDSET_H -#include "khash.h" +#include "khashl.h" /** * This API is similar to oid-array, in that it maintains a set of object ids
pack-bitmap.h +1 -1
@@ -2,7 +2,7 @@#define PACK_BITMAP_H #include "ewah/ewok.h" -#include "khash.h" +#include "khashl.h" #include "pack.h" #include "pack-objects.h" #include "string-list.h"
PATCH 3/3 khashl: fix ensemble lookups on empty table
In reply to Eric WongThe ->bits field of regular khashl structs is invalid when the ->keys array is NULL. Thus the ensemble *_getp implementation must follow existing *_get and *_getp usage conventions and check the iterator against kh_end().
This fixes a fast-import crash on t3427-rebase-subtree.sh in an abandoned commit to use the ensemble implementation for oid_map and oid_pos. I've abandoned the aforementioned commit for now since it was more intrusive, more expensive for small tables, and realloc(3) on glibc is already optimized using mremap(2) for large hash resizes.
--- khashl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
khashl.h +1 -1
@@ -265,7 +265,7 @@ typedef struct { low = hash & ((1U <<g->bits) -1 ); \ h = &g->sub[low]; \ ret = prefix##_sub_getp_core(h, key, hash); \ -if (ret ==1U <<h->bits) r.sub = low, r.pos = (khint_t)-1 ; \ +if (ret >=kh_end (h)) r.sub = low, r.pos = (khint_t)-1 ; \else r.sub = low, r.pos = ret; \return r; \ } \
oops, forgot [v2]
In reply to Eric WongSorry, been running on fumes all week :<
Quoted 22 lines · Eric Wong
Fortunately, this set of changes is unintrusive; but I'm hoping to have more time to make deeper changes this year.
Eric Wong (3):
list-objects-filter: usekh_sizeAPI
treewide: switch to khashl for memory savings
khashl: fix ensemble lookups on empty tablebuiltin/fast-import.c | 2 +- builtin/fsmonitor--daemon.c | 4 +- delta-islands.c | 4 +- khash.h | 338 ----------------------- khashl.h | 522 ++++++++++++++++++++++++++++++++++++ list-objects-filter.c | 2 +- object-store-ll.h | 2 +- object-store.h | 7 +- oidset.h | 2 +- pack-bitmap.h | 2 +- 10 files changed, 535 insertions(+), 350 deletions(-) delete mode 100644 khash.h create mode 100644 khashl.h
Quoted 3 lines
Range-diff:
-: ---------- > 1: 3bf3148cab list-objects-filter: use kh_size API 1: e74965907e ! 2: 09900edb48 treewide: switch to khashl for memory savings
Do you have the correct range-diff? The previous round had the change to the list-object-filter.c to use kh_size() already.
But I see the 0 -> NULL fixes. Perhaps the left-side base was off by one when you took the range-diff and there is nothing else going on that we should be worried about...
Quoted 71 lines
@@ Commit message khashl is an updated version of khash with less memory overhead (one bit/bucket instead of two) than the original khash and - similar overall performance. Insertions are simpler (linear - probing) but deletions may be slightly slower[1]. Of course, - the majority of hash tables in git do not delete individual - elements. + similar overall performance. According to its author, + insertions are simpler (linear probing) but deletions may be + slightly slower[1]. Of course, the majority of hash tables in + git do not delete individual elements. Overall memory usage did not decrease much, as the hash tables and elements we store in them are big and currently dwarf the overhead of the khash internals. Only around 10 MB in - allocations (not peak use) is saved when doing a no-op `git gc' - of a Linux kernel object store with thousands of refs and - islands. + allocations (and a few dozen KB peak use out of ~6 GB) is saved + when doing a no-op `git gc' of a Linux kernel object store with + thousands of refs and islands. A summary of differences I've found from khash to khashl: @@ Commit message * flesh out KHASHL_{SET,MAP}_INIT wrappers with *_clear, *_resize, and *_release functions + * sparse fixes from Junio and Jeff + [1] https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/ [2] git clone https://github.com/attractivechaos/klib.git 2895a16cb55e (support an ensemble of hash tables, 2023-12-18) @@ Commit message typedef) and was the only place where I had to change a definition. Signed-off-by: Eric Wong <[email protected]> + Helped-by: Junio C Hamano <[email protected]> + Helped-by: Jeff King <[email protected]> ## builtin/fast-import.c ## @@ @@ khashl.h (new) +#define __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \ + SCOPE khint_t prefix##_getp_core(const HType *h, const khkey_t *key, khint_t hash) { \ + khint_t i, last, n_buckets, mask; \ -+ if (h->keys == 0) return 0; \ ++ if (!h->keys) return 0; \ + n_buckets = (khint_t)1U << h->bits; \ + mask = n_buckets - 1U; \ + i = last = __kh_h2b(hash, h->bits); \ @@ khashl.h (new) + +#define __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \ + SCOPE void prefix##_resize(HType *h, khint_t new_n_buckets) { \ -+ khint32_t *new_used = 0; \ ++ khint32_t *new_used = NULL; \ + khint_t j = 0, x = new_n_buckets, n_buckets, new_bits, new_mask; \ + while ((x >>= 1) != 0) ++j; \ + if (new_n_buckets & (new_n_buckets - 1)) ++j; \ @@ khashl.h (new) +#define __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn) \ + SCOPE int prefix##_del(HType *h, khint_t i) { \ + khint_t j = i, k, mask, n_buckets; \ -+ if (h->keys == 0) return 0; \ ++ if (!h->keys) return 0; \ + n_buckets = (khint_t)1U<<h->bits; \ + mask = n_buckets - 1U; \ + while (1) { \2:
744e1b7198= 3:bfb20eae37khashl: fix ensemble lookups on empty table
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Eric Wong
Range-diff:
-: ---------- > 1: 3bf3148cab list-objects-filter: use kh_size API 1: e74965907e ! 2: 09900edb48 treewide: switch to khashl for memory savingsDo you have the correct range-diff? The previous round had the change to the
list-object-filter.cto usekh_size()already.But I see the 0 -> NULL fixes. Perhaps the left-side base was off by one when you took the range-diff and there is nothing else going on that we should be worried about...
Odd... I switched to a different machine (w) for v2 due to connectivity problems to the original machine (m) I did v1 on and applied the patches sent to the list.
I did end up rebasing v1 on (w) against the newer master:c75fd8d815 (The eleventh batch, 2024-03-25) instead of commit 11c821f2f2 (The tenth batch, 2024-03-21) on (m).
On (w):
git format-patch -o $OUT/ khashl-base..khashl-v2 \ --cover-letter --range-diff=khashl-v1 -v2
Seems to mess up infer_range_diff_ranges and it chose khashl-v2' instead of khash-base' as the `a' part of the range for r1.
(m) doesn't do this, both running 2.44.0.32*-ish
Here it is from (w) with the explicit `a..' part for --range-diff:
git format-patch -o $OUT/ khashl-base..khashl-v2 \ --cover-letter --range-diff=khashl-base..khashl-v1 -v2
Range-diff against v1:
1: 3bf3148cab = 1: 3bf3148cab list-objects-filter: use kh_size API
2: e74965907e ! 2: 09900edb48 treewide: switch to khashl for memory savings
@@ Commit message
khashl is an updated version of khash with less memory overhead
(one bit/bucket instead of two) than the original khash and
- similar overall performance. Insertions are simpler (linear
- probing) but deletions may be slightly slower[1]. Of course,
- the majority of hash tables in git do not delete individual
- elements.
+ similar overall performance. According to its author,
+ insertions are simpler (linear probing) but deletions may be
+ slightly slower[1]. Of course, the majority of hash tables in
+ git do not delete individual elements.
Overall memory usage did not decrease much, as the hash tables
and elements we store in them are big and currently dwarf the
overhead of the khash internals. Only around 10 MB in
- allocations (not peak use) is saved when doing a no-op `git gc'
- of a Linux kernel object store with thousands of refs and
- islands.
+ allocations (and a few dozen KB peak use out of ~6 GB) is saved
+ when doing a no-op `git gc' of a Linux kernel object store with
+ thousands of refs and islands.
A summary of differences I've found from khash to khashl:
@@ Commit message
* flesh out KHASHL_{SET,MAP}_INIT wrappers with *_clear, *_resize,
and *_release functions
+ * sparse fixes from Junio and Jeff
+
[1] https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/
[2] git clone https://github.com/attractivechaos/klib.git
2895a16cb55e (support an ensemble of hash tables, 2023-12-18)
@@ Commit message
typedef) and was the only place where I had to change a definition.
Signed-off-by: Eric Wong <[email protected]>
+ Helped-by: Junio C Hamano <[email protected]>
+ Helped-by: Jeff King <[email protected]>
## builtin/fast-import.c ##
@@
@@ khashl.h (new)
+#define __KHASHL_IMPL_GET(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
+ SCOPE khint_t prefix##_getp_core(const HType *h, const khkey_t *key, khint_t hash) { \
+ khint_t i, last, n_buckets, mask; \
-+ if (h->keys == 0) return 0; \
++ if (!h->keys) return 0; \
+ n_buckets = (khint_t)1U << h->bits; \
+ mask = n_buckets - 1U; \
+ i = last = __kh_h2b(hash, h->bits); \
@@ khashl.h (new)
+
+#define __KHASHL_IMPL_RESIZE(SCOPE, HType, prefix, khkey_t, __hash_fn, __hash_eq) \
+ SCOPE void prefix##_resize(HType *h, khint_t new_n_buckets) { \
-+ khint32_t *new_used = 0; \
++ khint32_t *new_used = NULL; \
+ khint_t j = 0, x = new_n_buckets, n_buckets, new_bits, new_mask; \
+ while ((x >>= 1) != 0) ++j; \
+ if (new_n_buckets & (new_n_buckets - 1)) ++j; \
@@ khashl.h (new)
+#define __KHASHL_IMPL_DEL(SCOPE, HType, prefix, khkey_t, __hash_fn) \
+ SCOPE int prefix##_del(HType *h, khint_t i) { \
+ khint_t j = i, k, mask, n_buckets; \
-+ if (h->keys == 0) return 0; \
++ if (!h->keys) return 0; \
+ n_buckets = (khint_t)1U<<h->bits; \
+ mask = n_buckets - 1U; \
+ while (1) { \3: 744e1b7198 = 3: bfb20eae37 khashl: fix ensemble lookups on empty table