Skip to contentSkip to conversation

switch to tombstone-free khashl table

PATCH In review Eric Wong 3 participants 11 messages Mar 25, 2024 – Apr 19, 2024

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

cover switch to tombstone-free khashl table

The memory improvement is minor, but any memory reduction at all is welcome at this point. Fortunately, this set of changes is unintrusive.

I have some other ideas that I'll hopefully get to implement before swapping kills all my SSDs (see bottom).

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

TODO (some ideas stolen from khashl):

* obj_hash can probably use a 0.75 load factor (instead of 0.5) to save memory and not slow down too much. oid_* khash has always had 0.77 which was fine and now khashl has 0.75. 0.75 is cheaper to compute via (shifts + ORs) than 0.77.

* obj_hash can use `i = (i + 1) & mask' like khashl does to avoid branches in linear probe loops

* obj_hash can use realloc and copy in-place resize logic khashl does. khashl uses the ->used bitmap for this, but it should be possible to do for obj_hash w/o additional allocations via pointer tagging

(not khashl inspired):

* keep an identity pool of OIDs separately (similar to how Perl5 pools all hash keys) and only use tagged pointers for OIDs. Pointer tagging can be used to distinguish between 7 hash functions, leaving us room for 5 more in addition to SHA-(1|256). This change will be a large effort (with a hopefully large savings). If we ever need more than 7 hash functions, we can switch to storing hash type information in slabs that can be looked up using address ranges (AFAIK, jemalloc does this).

author
#1 lore raw

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
index 4346f8da45..440f112d23 100644
@@ -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);
author
#2 lore raw

PATCH 2/3 treewide: switch to khashl for memory savings

In reply to Eric Wong

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.

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.

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

[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.

---
The git-specific changes are absolutely minimal :>

 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
index 71a195ca22..29e50fd675 100644
@@ -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
index 1593713f4c..1c71d96c6d 100644
@@ -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
index ee2318d45a..aa35839f15 100644
@@ -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
index 26a3895c82..401c4beff5 100644
@@ -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
index 1b3e3d7d01..3db4802e86 100644
@@ -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
index 262f4256d6..17af1b6708 100644
@@ -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
index c7dea13217..d018365f24 100644
@@ -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"
#3 lore raw

Re: [PATCH 2/3] treewide: switch to khashl for memory savings

Quoted 6 lines · Eric Wong

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.

$ make builtin/fast-import.sp ;# part of make sparse
    SP builtin/fast-import.c
builtin/fast-import.c: note: in included file (through oidset.h, packfile.h):
khashl.h:516:1: error: Using plain integer as NULL pointer
khashl.h:516:1: error: Using plain integer as NULL pointer
make: *** [Makefile:3237: builtin/fast-import.sp] Error 1

I found IMPL_GET and IMPL_DEL's use of (h->keys == 0) were giving one of these two, and managed to reduce the error to just one with the attached patch, but I don't know what the other error is coming from.

 khashl.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
khashl.h +2 -2
index 8ffe80fbb2..663e36cd2d 100644
@@ -101,7 +101,7 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
 #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); \
@@ -183,7 +183,7 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
 #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) { \
#4 lore raw

Re: [PATCH 2/3] treewide: switch to khashl for memory savings

Quoted 11 lines · Junio C Hamano
$ make builtin/fast-import.sp ;# part of make sparse
    SP builtin/fast-import.c
builtin/fast-import.c: note: in included file (through oidset.h, packfile.h):
khashl.h:516:1: error: Using plain integer as NULL pointer
khashl.h:516:1: error: Using plain integer as NULL pointer
make: *** [Makefile:3237: builtin/fast-import.sp] Error 1

I found IMPL_GET and IMPL_DEL's use of (h->keys == 0) were giving one of these two, and managed to reduce the error to just one with the attached patch, but I don't know what the other error is coming from.

Probably:

khashl.h +1 -1
index 8fcebed237..1e724bbf88 100644
@@ -116,7 +116,7 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
 
 #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; \

-Peff

#5 lore raw

Re: [PATCH 2/3] treewide: switch to khashl for memory savings

Quoted 21 lines · Jeff King
Quoted 11 lines · Junio C Hamano
$ make builtin/fast-import.sp ;# part of make sparse
    SP builtin/fast-import.c
builtin/fast-import.c: note: in included file (through oidset.h, packfile.h):
khashl.h:516:1: error: Using plain integer as NULL pointer
khashl.h:516:1: error: Using plain integer as NULL pointer
make: *** [Makefile:3237: builtin/fast-import.sp] Error 1

I found IMPL_GET and IMPL_DEL's use of (h->keys == 0) were giving one of these two, and managed to reduce the error to just one with the attached patch, but I don't know what the other error is coming from.

Probably:

khashl.h +0 -0
index 8fcebed237..1e724bbf88 100644

khashl.h static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26

Quoted 10 lines
khashl.h +1 -2
@@ -116,7 +116,7 @@ static kh_inline khint_t __kh_h2b(khint_t hash, khint_t bits) { return hash * 26
 
 #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; \
 
-Peff

Spot on. With this (and the other two 0 -> NULL fixes), and with the SWAP() thing in brian's credential series fixed, the tip of 'seen' passes the static-analysis and sparse CI jobs again.

Thanks.

author
#6 lore raw

PATCH 3/3 khashl: fix ensemble lookups on empty table

In reply to Eric Wong

The ->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
index 3660fd2ce4..8ffe80fbb2 100644
@@ -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; \
 	} \
author
#7 lore raw

[REJECT 4/3] switch to khashl ensemble

In reply to Eric Wong

Using an ensemble of hash tables to implement a larger one can reduce the temporary space required to resize a hash table. However, I haven't been able to measure an improvement using glibc memusage(1), yet.

I could be tuning it wrong (too few or too many sub hash tables), and it may not be useful with glibc malloc since large realloc(3) are optimized with mremap(2) to provide in-place growth.

---
 builtin/fast-import.c | 10 ++++----
 builtin/replay.c      | 10 ++++----
 delta-islands.c       | 54 +++++++++++++++++++-------------------
 fsck.c                | 10 ++++----
 khashl.h              | 60 ++++++++++++++++++++++++++++++++-----------
 pack-bitmap-write.c   |  4 +--
 pack-bitmap.c         | 32 +++++++++++------------
 7 files changed, 105 insertions(+), 75 deletions(-)
builtin/fast-import.c +5 -5
index 29e50fd675..190e136e2e 100644
@@ -2198,7 +2198,7 @@ static uintmax_t change_note_fanout(struct tree_entry *root,
 static int parse_mapped_oid_hex(const char *hex, struct object_id *oid, const char **end)
 {
 	int algo;
-	khiter_t it;
+	kh_ensitr_t it;
 
 	/* Make SHA-1 object IDs have all-zero padding. */
 	memset(oid->hash, 0, sizeof(oid->hash));
@@ -2209,13 +2209,13 @@ static int parse_mapped_oid_hex(const char *hex, struct object_id *oid, const ch
 
 	it = kh_get_oid_map(sub_oid_map, *oid);
 	/* No such object? */
-	if (it == kh_end(sub_oid_map)) {
+	if (kh_ens_is_end(it)) {
 		/* If we're using the same algorithm, pass it through. */
 		if (hash_algos[algo].format_id == the_hash_algo->format_id)
 			return 0;
 		return -1;
 	}
-	oidcpy(oid, kh_value(sub_oid_map, it));
+	oidcpy(oid, kh_ens_val(sub_oid_map, it));
 	return 0;
 }
 
@@ -3083,13 +3083,13 @@ static void insert_mapped_mark(uintmax_t mark, void *object, void *cbp)
 	struct object_id *fromoid = object;
 	struct object_id *tooid = find_mark(cbp, mark);
 	int ret;
-	khiter_t it;
+	kh_ensitr_t it;
 
 	it = kh_put_oid_map(sub_oid_map, *fromoid, &ret);
 	/* We've already seen this object. */
 	if (ret == 0)
 		return;
-	kh_value(sub_oid_map, it) = tooid;
+	kh_ens_val(sub_oid_map, it) = tooid;
 }
 
 static void build_mark_map_one(struct mark_set *from, struct mark_set *to)
builtin/replay.c +5 -5
index 6bc4b47f09..e084da8a94 100644
@@ -227,10 +227,10 @@ static struct commit *mapped_commit(kh_oid_map_t *replayed_commits,
 				    struct commit *commit,
 				    struct commit *fallback)
 {
-	khint_t pos = kh_get_oid_map(replayed_commits, commit->object.oid);
-	if (pos == kh_end(replayed_commits))
+	kh_ensitr_t pos = kh_get_oid_map(replayed_commits, commit->object.oid);
+	if (kh_ens_is_end(pos))
 		return fallback;
-	return kh_value(replayed_commits, pos);
+	return kh_ens_val(replayed_commits, pos);
 }
 
 static struct commit *pick_regular_commit(struct commit *pickme,
@@ -381,7 +381,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	replayed_commits = kh_init_oid_map();
 	while ((commit = get_revision(&revs))) {
 		const struct name_decoration *decoration;
-		khint_t pos;
+		kh_ensitr_t pos;
 		int hr;
 
 		if (!commit->parents)
@@ -399,7 +399,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 		if (hr == 0)
 			BUG("Duplicate rewritten commit: %s\n",
 			    oid_to_hex(&commit->object.oid));
-		kh_value(replayed_commits, pos) = last_commit;
+		kh_ens_val(replayed_commits, pos) = last_commit;
 
 		/* Update any necessary branches */
 		if (advance_name)
delta-islands.c +27 -27

149 lines not on this page · raw message

fsck.c +5 -5
index 8ded0a473a..4c67e1e64c 100644
@@ -266,13 +266,13 @@ void fsck_enable_object_names(struct fsck_options *options)
 const char *fsck_get_object_name(struct fsck_options *options,
 				 const struct object_id *oid)
 {
-	khiter_t pos;
+	kh_ensitr_t pos;
 	if (!options->object_names)
 		return NULL;
 	pos = kh_get_oid_map(options->object_names, *oid);
-	if (pos >= kh_end(options->object_names))
+	if (kh_ens_is_end(pos))
 		return NULL;
-	return kh_value(options->object_names, pos);
+	return kh_ens_val(options->object_names, pos);
 }
 
 void fsck_put_object_name(struct fsck_options *options,
@@ -281,7 +281,7 @@ void fsck_put_object_name(struct fsck_options *options,
 {
 	va_list ap;
 	struct strbuf buf = STRBUF_INIT;
-	khiter_t pos;
+	kh_ensitr_t pos;
 	int hashret;
 
 	if (!options->object_names)
@@ -292,7 +292,7 @@ void fsck_put_object_name(struct fsck_options *options,
 		return;
 	va_start(ap, fmt);
 	strbuf_vaddf(&buf, fmt, ap);
-	kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
+	kh_ens_val(options->object_names, pos) = strbuf_detach(&buf, NULL);
 	va_end(ap);
 }
 
khashl.h +45 -15

126 lines not on this page · raw message

pack-bitmap-write.c +2 -2
index 990a9498d7..bbf2090c46 100644
@@ -465,7 +465,7 @@ static int fill_bitmap_commit(struct bb_commit *ent,
 static void store_selected(struct bb_commit *ent, struct commit *commit)
 {
 	struct bitmapped_commit *stored = &writer.selected[ent->idx];
-	khiter_t hash_pos;
+	kh_ensitr_t hash_pos;
 	int hash_ret;
 
 	stored->bitmap = bitmap_to_ewah(ent->bitmap);
@@ -474,7 +474,7 @@ static void store_selected(struct bb_commit *ent, struct commit *commit)
 	if (hash_ret == 0)
 		die("Duplicate entry when writing index: %s",
 		    oid_to_hex(&commit->object.oid));
-	kh_value(writer.bitmaps, hash_pos) = stored;
+	kh_ens_val(writer.bitmaps, hash_pos) = stored;
 }
 
 int bitmap_writer_build(struct packing_data *to_pack)
pack-bitmap.c +16 -16
index 2baeabacee..68cd893dee 100644
@@ -214,7 +214,7 @@ static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
 					  int flags)
 {
 	struct stored_bitmap *stored;
-	khiter_t hash_pos;
+	kh_ensitr_t hash_pos;
 	int ret;
 
 	stored = xmalloc(sizeof(struct stored_bitmap));
@@ -235,7 +235,7 @@ static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
 		return NULL;
 	}
 
-	kh_value(index->bitmaps, hash_pos) = stored;
+	kh_ens_val(index->bitmaps, hash_pos) = stored;
 	return stored;
 }
 
@@ -721,7 +721,7 @@ static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_
 	static size_t xor_items_nr = 0, xor_items_alloc = 0;
 	static int is_corrupt = 0;
 	int xor_flags;
-	khiter_t hash_pos;
+	kh_ensitr_t hash_pos;
 	struct bitmap_lookup_table_xor_item *xor_item;
 
 	if (is_corrupt)
@@ -766,8 +766,8 @@ static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_
 		 * has already been stored. So, assign this stored bitmap
 		 * to the xor_bitmap.
 		 */
-		if (hash_pos < kh_end(bitmap_git->bitmaps) &&
-			(xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
+		if (!kh_ens_is_end(hash_pos) &&
+			(xor_bitmap = kh_ens_val(bitmap_git->bitmaps, hash_pos)))
 			break;
 		xor_items_nr++;
 		xor_row = triplet.xor_row;
@@ -841,9 +841,9 @@ static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_
 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
 				      struct commit *commit)
 {
-	khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
+	kh_ensitr_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
 					   commit->object.oid);
-	if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
+	if (kh_ens_is_end(hash_pos)) {
 		struct stored_bitmap *bitmap = NULL;
 		if (!bitmap_git->table_lookup)
 			return NULL;
@@ -855,17 +855,17 @@ struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
 			return NULL;
 		return lookup_stored_bitmap(bitmap);
 	}
-	return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
+	return lookup_stored_bitmap(kh_ens_val(bitmap_git->bitmaps, hash_pos));
 }
 
 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
 					   const struct object_id *oid)
 {
 	kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
-	khiter_t pos = kh_get_oid_pos(positions, *oid);
+	kh_ensitr_t pos = kh_get_oid_pos(positions, *oid);
 
-	if (pos < kh_end(positions)) {
-		int bitmap_pos = kh_value(positions, pos);
+	if (!kh_ens_is_end(pos)) {
+		int bitmap_pos = kh_ens_val(positions, pos);
 		return bitmap_pos + bitmap_num_objects(bitmap_git);
 	}
 
@@ -913,7 +913,7 @@ static int ext_index_add_object(struct bitmap_index *bitmap_git,
 {
 	struct eindex *eindex = &bitmap_git->ext_index;
 
-	khiter_t hash_pos;
+	kh_ensitr_t hash_pos;
 	int hash_ret;
 	int bitmap_pos;
 
@@ -928,10 +928,10 @@ static int ext_index_add_object(struct bitmap_index *bitmap_git,
 		bitmap_pos = eindex->count;
 		eindex->objects[eindex->count] = object;
 		eindex->hashes[eindex->count] = pack_name_hash(name);
-		kh_value(eindex->positions, hash_pos) = bitmap_pos;
+		kh_ens_val(eindex->positions, hash_pos) = bitmap_pos;
 		eindex->count++;
 	} else {
-		bitmap_pos = kh_value(eindex->positions, hash_pos);
+		bitmap_pos = kh_ens_val(eindex->positions, hash_pos);
 	}
 
 	return bitmap_pos + bitmap_num_objects(bitmap_git);
@@ -2361,7 +2361,7 @@ int test_bitmap_commits(struct repository *r)
 			die(_("failed to load bitmap indexes"));
 	}
 
-	kh_foreach(bitmap_git->bitmaps, oid, value, {
+	kh_ens_foreach(bitmap_git->bitmaps, oid, value, {
 		printf_ln("%s", oid_to_hex(&oid));
 	});
 
@@ -2479,7 +2479,7 @@ void free_bitmap_index(struct bitmap_index *b)
 	ewah_pool_free(b->tags);
 	if (b->bitmaps) {
 		struct stored_bitmap *sb;
-		kh_foreach_value(b->bitmaps, sb, {
+		kh_ens_foreach_value(b->bitmaps, sb, {
 			ewah_pool_free(sb->root);
 			free(sb);
 		});
#8 lore raw
In reply to Eric Wong
Quoted 6 lines · Eric Wong

The memory improvement is minor, but any memory reduction at all is welcome at this point. Fortunately, this set of changes is unintrusive.

I have some other ideas that I'll hopefully get to implement before swapping kills all my SSDs (see bottom).

Please describe what this topic aims at to sell the topic better. Are we trying to reduce memory footprint? In other words, if this topic were to hit a released version of Git, what would the short paragraph description for the topic in the release notes look like?

* The khash.h hashtable implementation has been replaced with khashl.h that is mostly API compatible with reduced memory consumption, simpler insertion and a bit slower deletion.

or somesuch.

A performance oriented topic would be helped to have benchmark numbers to show how much improvement it makes and a memory reduction topic would be helped to have some numbers in the cover letter. It is OK to summarize/duplicate what appears in the proposed log message of some step; it does not need too much text to say 100MB total allocations reduced by 10MB or something like that, for example.

An API improvement topic would be helped to have an example rewrite of a caller (or just a reference to a representative one, i.e., "see how the caller in function X gets simplified in [PATCH 04/28]") in the cover letter.

A bugfix topic would be helped to have an end-user visible effect in the cover letter.

Thanks.

#9 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Eric Wong

The memory improvement is minor, but any memory reduction at all is welcome at this point. Fortunately, this set of changes is unintrusive.

Quoted 8 lines
Quoted 2 lines

I have some other ideas that I'll hopefully get to implement before swapping kills all my SSDs (see bottom).

Please describe what this topic aims at to sell the topic better. Are we trying to reduce memory footprint? In other words, if this topic were to hit a released version of Git, what would the short paragraph description for the topic in the release notes look like?
...

So, did anything happened since this exchange? I remember that we caught and fixed a few minor sparse errors, but other than that, I am not sure what to do with this topic.

Not that I want to merge loud tree-wide topics down during the prerelease period...

Thanks.

author
#10 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Eric Wong

The memory improvement is minor, but any memory reduction at all is welcome at this point. Fortunately, this set of changes is unintrusive.

Quoted 8 lines
Quoted 2 lines

I have some other ideas that I'll hopefully get to implement before swapping kills all my SSDs (see bottom).

Please describe what this topic aims at to sell the topic better. Are we trying to reduce memory footprint? In other words, if this topic were to hit a released version of Git, what would the short paragraph description for the topic in the release notes look like?
...

Quoted 3 lines

So, did anything happened since this exchange? I remember that we caught and fixed a few minor sparse errors, but other than that, I am not sure what to do with this topic.

Not really... I've been thinking we can beat khashl for our purposes usage by allowing explicit tombstones to be configured and getting rid of the ->used bitmap entirely.

One goal is to eventually reuse the code with the open-coded
hash table in object.c That said, I've been bogged down by
personal crap this year and don't know how/if I'll be able to hack on it this year.

Quoted 2 lines

Not that I want to merge loud tree-wide topics down during the prerelease period...

No worries, we can keep it out for now and work on it incrementally or drop it entirely in favor of something else down the line...

git's about to turn 20, and I really want to ensure it and the tools around it continue to be usable on computers from 20-25 years ago.