Store submodules in a hash, not a linked list
cover Store submodules in a hash, not a linked list
I have mentioned this patch series on the mailing list a couple of time [1,2] but haven't submitted it before. I just rebased it to current master. It is available from my Git fork [3] as branch "submodule-hash".
The first point of this patch series is to optimize submodule ref_store lookup by storing the ref_stores in a hashmap rather than a linked list. But a more interesting second point is to weaken the 1:1 relationship between submodules and ref_stores a little bit more.
A files_ref_store would be perfectly happy to represent, say, the references *physically* stored in a linked worktree (e.g., HEAD, refs/bisect/*, etc) even though that is not the complete collection of refs that are *logically* visible from that worktree (which includes references from the main repository, too). But the old code was confusing the two things by storing "submodule" in every ref_store instance.
So push the submodule attribute down to the files_ref_store class (but continue to let the ref_stores be looked up by submodule).
The last commit is relatively orthogonal to the others; it simplifies read_loose_refs() by calling resolve_ref_recursively() directly using the ref_store instance that it already has in hand, rather than indirectly via the public wrappers.
Michael
[1] http://public-inbox.org/git/[email protected]/
[2] http://public-inbox.org/git/[email protected]/
[3] https://github.com/mhagger/git
Michael Haggerty (5):
refs: store submodule ref stores in a hashmap
refs: push the submodule attribute down register_ref_store(): new function files_ref_store::submodule: use NULL for the main repository read_loose_refs(): read refs using resolve_ref_recursively()
refs.c | 93 ++++++++++++++++++++++++++++++++++------------------ refs/files-backend.c | 77 +++++++++++++++++++++++++------------------ refs/refs-internal.h | 37 ++++++++------------- 3 files changed, 122 insertions(+), 85 deletions(-)
Signature · 1 line
2.9.3
PATCH 1/5 refs: store submodule ref stores in a hashmap
Aside from scaling better, this means that the submodule name needn't be stored in the ref_store instance anymore (which will be changed in a moment). This, in turn, will help loosen the strict 1:1 relationship between ref_stores and submodules.
--- refs.c | 61 ++++++++++++++++++++++++++++++++++++++++------------ refs/refs-internal.h | 6 ------ 2 files changed, 47 insertions(+), 20 deletions(-)
refs.c +47 -14
@@ -3,6 +3,7 @@*/ #include "cache.h" +#include "hashmap.h" #include "lockfile.h" #include "refs.h" #include "refs/refs-internal.h"
@@ -1357,11 +1358,42 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,return 0 ; } +struct submodule_hash_entry +{ +struct hashmap_entry ent;/* must be the first member! */ + +struct ref_store *refs; + +/* NUL-terminated name of submodule: */ +char submodule[FLEX_ARRAY ]; +}; + +static int submodule_hash_cmp (const void *entry,const void *entry_or_key, +const void *keydata) +{ +const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key; +const char *submodule = keydata; + +return strcmp (e1->submodule, submodule ? submodule : e2->submodule); +} + +static struct submodule_hash_entry *alloc_submodule_hash_entry ( +const char *submodule,struct ref_store *refs) +{ +size_t len =strlen (submodule); +struct submodule_hash_entry *entry =malloc (sizeof (*entry) + len +1 ); + +hashmap_entry_init (entry,strhash (submodule)); + entry->refs = refs; +memcpy (entry->submodule, submodule, len +1 ); +return entry; +} +/* A pointer to the ref_store for the main repository: */ static struct ref_store *main_ref_store; -/* A linked list of ref_stores for submodules: */ -static struct ref_store *submodule_ref_stores; +/* A hashmap of ref_stores, stored by submodule name: */ +static struct hashmap submodule_ref_stores; void base_ref_store_init(struct ref_store *refs, const struct ref_storage_be *be,
@@ -1373,16 +1405,17 @@ void base_ref_store_init(struct ref_store *refs,die ("BUG: main_ref_store initialized twice" ); refs->submodule ="" ; - refs->next =NULL ; main_ref_store = refs; }else { -if (lookup_ref_store (submodule)) + refs->submodule =xstrdup (submodule); + +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 ); + +if (hashmap_put (&submodule_ref_stores, +alloc_submodule_hash_entry (submodule, refs)))die ("BUG: ref_store for submodule '%s' initialized twice" , submodule); - - refs->submodule =xstrdup (submodule); - refs->next = submodule_ref_stores; - submodule_ref_stores = refs; } }
@@ -1402,17 +1435,17 @@ struct ref_store *ref_store_init(const char *submodule)struct ref_store *lookup_ref_store (const char *submodule) { -struct ref_store *refs; +struct submodule_hash_entry *entry;if (!submodule || !*submodule)return main_ref_store; -for (refs = submodule_ref_stores; refs; refs = refs->next) { -if (!strcmp (submodule, refs->submodule)) -return refs; - } +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 ); -return NULL ; + entry =hashmap_get_from_hash (&submodule_ref_stores, +strhash (submodule), submodule); +return entry ? entry->refs :NULL ; }struct ref_store *get_ref_store (const char *submodule)
refs/refs-internal.h +0 -6
@@ -634,12 +634,6 @@ struct ref_store {* reference store: */ const char *submodule; - -/* -* Submodule reference store instances are stored in a linked -* list using this pointer. -*/ -struct ref_store *next; };/*
Signature · 1 line
2.9.3
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
refs.c struct ref_store *ref_store_init(const char *submodule) · line 1440
Quoted 7 lines
refs.c +2
-0@@ -1402,17 +1435,17 @@ struct ref_store *ref_store_init(const char *submodule)struct ref_store *lookup_ref_store (const char *submodule) { +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );
So we can lookup a submodule even before we initialized the subsystem? Does that actually happen? (It sounds like a bug to me.)
Instead of initializing, you could return NULL directly here.
Otherwise looks good.
Thanks,
Stefan
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
Quoted 5 lines · Stefan Beller
Quoted 4 lines
refs.c +0
-0@@ -1402,17 +1435,17 @@ struct ref_store *ref_store_init(const char *submodule)struct ref_store *lookup_ref_store (const char *submodule) {
Quoted 8 lines
Review struct ref_store *ref_store_init(const char *submodule) · line 1439
Quoted 2 lines
(quoted diff) +2
-0+ if (!submodule_ref_stores.tablesize) + hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 20);So we can lookup a submodule even before we initialized the subsystem? Does that actually happen? (It sounds like a bug to me.)
Instead of initializing, you could return NULL directly here.
The lines you quoted are only concerned with bringing the (empty) hashmap into existence if it hasn't been initialized already. (There's no HASHMAP_INIT.) I don't know what you mean by "initialize the subsystem". The only way to bring a ref_store *object* into existence is currently to call get_ref_store(submodule), which calls lookup_ref_store(submodule) to see if it already exists, and if not calls ref_store_init(submodule) to instantiate it and register it in the hashmap. There's nothing else that has to be initialize before that (except maybe the usual startup config reading etc.)
I suppose this code path could be changed to return NULL without initializing the hashmap, but the hashmap will be initialized a moment later by ref_store_init(), so I don't see much difference either way.
Thanks for your review!
Michael
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
Quoted 25 lines · Michael Haggerty
Quoted 5 lines · Stefan Beller
Quoted 4 lines
refs.c +0
-0@@ -1402,17 +1435,17 @@ struct ref_store *ref_store_init(const char *submodule)struct ref_store *lookup_ref_store (const char *submodule) {Quoted 8 lines
Review struct ref_store *ref_store_init(const char *submodule) · line 1439
Quoted 2 lines
(quoted diff) +2
-0+ if (!submodule_ref_stores.tablesize) + hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 20);So we can lookup a submodule even before we initialized the subsystem? Does that actually happen? (It sounds like a bug to me.)
Instead of initializing, you could return NULL directly here.
The lines you quoted are only concerned with bringing the (empty) hashmap into existence if it hasn't been initialized already. (There's no
HASHMAP_INIT.) I don't know what you mean by "initialize the subsystem". The only way to bring aref_store*object* into existence is currently to callget_ref_store(submodule), which callslookup_ref_store(submodule)to see if it already exists, and if not callsref_store_init(submodule)to instantiate it and register it in the hashmap. There's nothing else that has to be initialize before that (except maybe the usual startup config reading etc.)
Quoted 3 lines
I suppose this code path could be changed to return NULL without initializing the hashmap, but the hashmap will be initialized a moment later by
ref_store_init(), so I don't see much difference either way.
Oh, I did not see that.
Thanks,
Stefan
Quoted 4 lines
Thanks for your review!
Michael
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
In reply to Michael HaggertyQuoted 10 lines · Michael Haggerty
Quoted 9 lines · Stefan Beller
Quoted 7 lines
refs.c +2
-0@@ -1402,17 +1435,17 @@ struct ref_store *ref_store_init(const char *submodule)struct ref_store *lookup_ref_store (const char *submodule) { +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );
Quoted 8 lines
Quoted 4 lines
So we can lookup a submodule even before we initialized the subsystem? Does that actually happen? (It sounds like a bug to me.)
Instead of initializing, you could return NULL directly here.
[...]
I suppose this code path could be changed to return NULL without initializing the hashmap, but the hashmap will be initialized a moment later byref_store_init(), so I don't see much difference either way.
I faced a similar issue when adding the oidset API recently:
http://public-inbox.org/git/[email protected]/
I came to the same conclusion that it doesn't matter much in practice. A nice thing about "return NULL" is that you do not have to duplicate the initialization which happens on the "write" side (so if you ever changed submodule_hash_cmp, for example, it needs changed in both places).
I also used the "cmpfn" member to check whether the table had been initialized, which matches existing uses elsewhere. But I do notice that the documentation explicitly says "tablesize" is good for this purpose, so it's probably a better choice.
-Peff
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
In reply to Michael HaggertyQuoted 3 lines · Michael Haggerty
Aside from scaling better, this means that the submodule name needn't be stored in the
ref_storeinstance anymore (which will be changed in a moment).
Nice. I like the latter reason very much (this is not a suggestion to change the description).
Quoted 7 lines
refs.c +7
-0+struct submodule_hash_entry +{ +struct hashmap_entry ent;/* must be the first member! */ + +struct ref_store *refs; + +/* NUL-terminated name of submodule: */
refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1377
Quoted 10 lines
refs.c +10
-0+char submodule[FLEX_ARRAY ]; +}; + +static int submodule_hash_cmp (const void *entry,const void *entry_or_key, +const void *keydata) +{ +const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key; +const char *submodule = keydata; + +return strcmp (e1->submodule, submodule ? submodule : e2->submodule);
I would have found it more readable if it were like so:
const char *submodule = keydata ? keydata : e2->submodule; return strcmp(e1->submodule, submodule);
but I suspect the difference is not that huge.
refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1384
Quoted 7 lines
refs.c +7
-0+} + +static struct submodule_hash_entry *alloc_submodule_hash_entry ( +const char *submodule,struct ref_store *refs) +{ +size_t len =strlen (submodule); +struct submodule_hash_entry *entry =malloc (sizeof (*entry) + len +1 );
I think this (and the later memcpy) is what FLEX_ALLOC_MEM() was invented for.
Quoted 8 lines
refs.c +5
-0+hashmap_entry_init (entry,strhash (submodule)); + entry->refs = refs; +memcpy (entry->submodule, submodule, len +1 ); +return entry; +}@@ -1373,16 +1405,17 @@ void base_ref_store_init(struct ref_store *refs,die ("BUG: main_ref_store initialized twice" );
refs.c void base_ref_store_init(struct ref_store *refs, · line 1413
Quoted 10 lines
refs.c +4
-2refs->submodule ="" ; - refs->next =NULL ; main_ref_store = refs; }else { -if (lookup_ref_store (submodule)) + refs->submodule =xstrdup (submodule); + +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );
Makes me wonder what "20" stands for. Perhaps the caller should be allowed to say "I do not quite care what initial size is" by passing 0 or some equally but more clealy meaningless value (which of course would be outside the scope of this series).
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
Quoted 8 lines · Junio C Hamano
refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1384
Quoted 5 lines
refs.c +5
-0+static struct submodule_hash_entry *alloc_submodule_hash_entry ( +const char *submodule,struct ref_store *refs) +{ +size_t len =strlen (submodule); +struct submodule_hash_entry *entry =malloc (sizeof (*entry) + len +1 );I think this (and the later memcpy) is what
FLEX_ALLOC_MEM()was invented for.
Yes, it was. Though since the length comes from a strlen() call, it can actually use the _STR variant, like:
FLEX_ALLOC_STR(entry, submodule, submodule);
Besides being shorter, this does integer-overflow checks on the final length.
Quoted 17 lines
refs.c void base_ref_store_init(struct ref_store *refs, · line 1413
Quoted 12 lines
refs.c +4
-2@@ -1373,16 +1405,17 @@ void base_ref_store_init(struct ref_store *refs,die ("BUG: main_ref_store initialized twice" ); refs->submodule ="" ; - refs->next =NULL ; main_ref_store = refs; }else { -if (lookup_ref_store (submodule)) + refs->submodule =xstrdup (submodule); + +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );Makes me wonder what "20" stands for. Perhaps the caller should be allowed to say "I do not quite care what initial size is" by passing 0 or some equally but more clealy meaningless value (which of course would be outside the scope of this series).
I think this is what "0" already does (grep for HASHMAP_INITIAL_SIZE). In fact, that constant is 64. The 20 we pass in goes through some magic load-factor computation and ends up as 25. That being smaller than the INITIAL_SIZE constant, I believe that we end up allocating 64 entries either way (that's just from reading the code, though; I didn't run it to double check).
-Peff
Re: [PATCH 1/5] refs: store submodule ref stores in a hashmap
In reply to Junio C HamanoQuoted 12 lines · Junio C Hamano
refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1377
Quoted 8 lines · Michael Haggerty
[...]
refs.c +7
-0+static int submodule_hash_cmp (const void *entry,const void *entry_or_key, +const void *keydata) +{ +const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key; +const char *submodule = keydata; + +return strcmp (e1->submodule, submodule ? submodule : e2->submodule);I would have found it more readable if it were like so:
Quoted 5 lines
const char *submodule = keydata ? keydata : e2->submodule; return strcmp(e1->submodule, submodule);but I suspect the difference is not that huge.
Yes, that's better. I'll change it.
Quoted 11 lines · Jeff King
Quoted 8 lines · Junio C Hamano
refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1384
Quoted 5 lines
refs.c +5
-0+static struct submodule_hash_entry *alloc_submodule_hash_entry ( +const char *submodule,struct ref_store *refs) +{ +size_t len =strlen (submodule); +struct submodule_hash_entry *entry =malloc (sizeof (*entry) + len +1 );I think this (and the later memcpy) is what
FLEX_ALLOC_MEM()was invented for.
Quoted 7 lines
Yes, it was. Though since the length comes from a
strlen()call, it can actually use the_STRvariant, like:FLEX_ALLOC_STR(entry, submodule, submodule);Besides being shorter, this does integer-overflow checks on the final length.
Nice. TIL. Will fix.
Quoted 18 lines
Quoted 17 lines
refs.c void base_ref_store_init(struct ref_store *refs, · line 1413
Quoted 12 lines
refs.c +4
-2@@ -1373,16 +1405,17 @@ void base_ref_store_init(struct ref_store *refs,die ("BUG: main_ref_store initialized twice" ); refs->submodule ="" ; - refs->next =NULL ; main_ref_store = refs; }else { -if (lookup_ref_store (submodule)) + refs->submodule =xstrdup (submodule); + +if (!submodule_ref_stores.tablesize) +hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );Makes me wonder what "20" stands for. Perhaps the caller should be allowed to say "I do not quite care what initial size is" by passing 0 or some equally but more clealy meaningless value (which of course would be outside the scope of this series).
Quoted 6 lines
I think this is what "0" already does (grep for
HASHMAP_INITIAL_SIZE). In fact, that constant is 64. The 20 we pass in goes through some magic load-factor computation and ends up as 25. That being smaller than theINITIAL_SIZEconstant, I believe that we end up allocating 64 entries either way (that's just from reading the code, though; I didn't run it to double check).
I guess I might as well change it to zero, then.
Thanks for the feedback!
Michael
PATCH 2/5 refs: push the submodule attribute down
In reply to Michael HaggertyPush the submodule attribute down from ref_store to files_ref_store. This is another step towards loosening the 1:1 connection between ref_stores and submodules.
--- refs.c | 9 -------- refs/files-backend.c | 61 ++++++++++++++++++++++++++++++++++++---------------- refs/refs-internal.h | 13 ----------- 3 files changed, 43 insertions(+), 40 deletions(-)
refs.c +0 -9
@@ -1404,11 +1404,8 @@ void base_ref_store_init(struct ref_store *refs,if (main_ref_store)die ("BUG: main_ref_store initialized twice" ); - refs->submodule ="" ; main_ref_store = refs; }else { - refs->submodule =xstrdup (submodule); -if (!submodule_ref_stores.tablesize)hashmap_init (&submodule_ref_stores, submodule_hash_cmp,20 );
@@ -1473,12 +1470,6 @@ struct ref_store *get_ref_store(const char *submodule)return refs; } -void assert_main_repository (struct ref_store *refs,const char *caller) -{ -if (*refs->submodule) -die ("BUG: %s called for a submodule" , caller); -} -/* backend functions */ int pack_refs (unsigned int flags) {
refs/files-backend.c +43 -18
163 lines not on this page · raw message
refs/refs-internal.h +0 -13
@@ -627,13 +627,6 @@ extern struct ref_storage_be refs_be_files;struct ref_store {/* The backend describing this ref_store's storage scheme: */ const struct ref_storage_be *be; - -/* -* The name of the submodule represented by this object, or -* the empty string if it represents the main repository's -* reference store: -*/ -const char *submodule; };/*
@@ -675,10 +668,4 @@ struct ref_store *lookup_ref_store(const char *submodule);*/ struct ref_store *get_ref_store (const char *submodule); -/* -* Die if refs is for a submodule (i.e., not for the main repository). -* caller is used in any necessary error messages. -*/ -void assert_main_repository (struct ref_store *refs,const char *caller); -#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.9.3
Re: [PATCH 2/5] refs: push the submodule attribute down
Quoted 5 lines · Michael Haggerty
Push the submodule attribute down from
ref_storetofiles_ref_store. This is another step towards loosening the 1:1 connection betweenref_storesand submodules.Signed-off-by Michael Haggerty <[email protected]>
Looks good,
Stefan
Re: [PATCH 2/5] refs: push the submodule attribute down
In reply to Michael HaggertyQuoted 3 lines · Michael Haggerty
Push the submodule attribute down from
ref_storetofiles_ref_store. This is another step towards loosening the 1:1 connection betweenref_storesand submodules.
The update seems to retain the externally visible effects, but what does this change mean for future backend writers whose code will sit next to files_ref_store? They need to have "submodule" field in their implementation of the backend and keep track of it?
Granted that the primary thing that looks at ->submodule field in the code before this change all live in refs/files-backend.c, but I am not sure if that is an artifact of us having only one backend at this moment, or a sign that future backends would benefit from extra freedom to choose how they exactly implement their submodule support.
PATCH 3/5 register_ref_store(): new function
In reply to Michael HaggertyMove the responsibility for registering the ref_store for a submodule from base_ref_store_init() to a new function, register_ref_store(). Call the latter from ref_store_init().
This means that base_ref_store_init() can lose its submodule argument, further weakening the 1:1 relationship between ref_stores and submodules.
--- refs.c | 19 +++++++++++++------ refs/files-backend.c | 2 +- refs/refs-internal.h | 15 ++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-)
refs.c +13 -6
@@ -1395,11 +1395,8 @@ static struct ref_store *main_ref_store;/* A hashmap of ref_stores, stored by submodule name: */ static struct hashmap submodule_ref_stores; -void base_ref_store_init (struct ref_store *refs, -const struct ref_storage_be *be, -const char *submodule) +void register_ref_store (struct ref_store *refs,const char *submodule) { - refs->be = be;if (!submodule) {if (main_ref_store)die ("BUG: main_ref_store initialized twice" );
@@ -1416,18 +1413,28 @@ void base_ref_store_init(struct ref_store *refs, } } +void base_ref_store_init (struct ref_store *refs, +const struct ref_storage_be *be) +{ + refs->be = be; +} +struct ref_store *ref_store_init (const char *submodule) {const char *be_name ="files" ;struct ref_storage_be *be =find_ref_storage_backend (be_name); +struct ref_store *refs;if (!be)die ("BUG: reference backend %s is unknown" , be_name);if (!submodule || !*submodule) -return be->init (NULL ); + refs = be->init (NULL );else -return be->init (submodule); + refs = be->init (submodule); + +register_ref_store (refs, submodule); +return refs; }struct ref_store *lookup_ref_store (const char *submodule)
refs/files-backend.c +1 -1
@@ -980,7 +980,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)struct files_ref_store *refs =xcalloc (1 ,sizeof (*refs));struct ref_store *ref_store = (struct ref_store *)refs; -base_ref_store_init (ref_store, &refs_be_files, submodule); +base_ref_store_init (ref_store, &refs_be_files); refs->submodule = submodule ?xstrdup (submodule) :"" ;
refs/refs-internal.h +10 -5
@@ -481,7 +481,7 @@ struct ref_store;* Initialize the ref_store for the specified submodule, or for the * main repository if submodule == NULL. These functions should call * base_ref_store_init() to initialize the shared part of the -* ref_store and to record the ref_store for later lookup. +* ref_store. */ typedef struct ref_store *ref_store_init_fn (const char *submodule);
@@ -630,12 +630,17 @@ struct ref_store { };/* -* Fill in the generic part of refs for the specified submodule and -* add it to our collection of reference stores. +* Register the specified ref_store to be the one that should be used +* for submodule (or the main repository if submodule is NULL). It is +* a fatal error to call this function twice for the same submodule. +*/ +void register_ref_store (struct ref_store *refs,const char *submodule); + +/* +* Fill in the generic part of refs. */ void base_ref_store_init (struct ref_store *refs, -const struct ref_storage_be *be, -const char *submodule); +const struct ref_storage_be *be);/* * Create, record, and return a ref_store instance for the specified
Signature · 1 line
2.9.3
Re: [PATCH 3/5] register_ref_store(): new function
Quoted 24 lines · Michael Haggerty
Move the responsibility for registering the
ref_storefor a submodule frombase_ref_store_init()to a new function,register_ref_store(). Call the latter fromref_store_init().This means that
base_ref_store_init()can lose its submodule argument, further weakening the 1:1 relationship betweenref_storesand submodules.Signed-off-by Michael Haggerty <[email protected]>---
refs.c +2
-0+struct ref_store *ref_store_init (const char *submodule) {const char *be_name ="files" ;struct ref_storage_be *be =find_ref_storage_backend (be_name); +struct ref_store *refs;if (!be)die ("BUG: reference backend %s is unknown" , be_name);
refs.c void base_ref_store_init(struct ref_store *refs, · line 1438
Quoted 10 lines
refs.c +5
-2if (!submodule || !*submodule) -return be->init (NULL ); + refs = be->init (NULL );else -return be->init (submodule); + refs = be->init (submodule); + +register_ref_store (refs, submodule); +return refs; }
This function is already very readable, though maybe it would be more readable like so:
{
const char *be_name = "files";
struct ref_storage_be *be = find_ref_storage_backend(be_name);
if (!be)
die("BUG: reference backend %s is unknown", be_name);
/* replace empty string by NULL */
if (submodule && !*submodule)
submodule = NULL;
register_ref_store(be->init(submodule), submodule);
return refs;
}Well, I dunno; the function inside the arguments to register seems ugly, though.
Re: [PATCH 3/5] register_ref_store(): new function
Quoted 54 lines · Stefan Beller
Quoted 24 lines · Michael Haggerty
Move the responsibility for registering the
ref_storefor a submodule frombase_ref_store_init()to a new function,register_ref_store(). Call the latter fromref_store_init().This means that
base_ref_store_init()can lose its submodule argument, further weakening the 1:1 relationship betweenref_storesand submodules.Signed-off-by Michael Haggerty <[email protected]>---
refs.c +2
-0+struct ref_store *ref_store_init (const char *submodule) {const char *be_name ="files" ;struct ref_storage_be *be =find_ref_storage_backend (be_name); +struct ref_store *refs;if (!be)die ("BUG: reference backend %s is unknown" , be_name);refs.c void base_ref_store_init(struct ref_store *refs, · line 1438
Quoted 10 lines
refs.c +5
-2if (!submodule || !*submodule) -return be->init (NULL ); + refs = be->init (NULL );else -return be->init (submodule); + refs = be->init (submodule); + +register_ref_store (refs, submodule); +return refs; }This function is already very readable, though maybe it would be more readable like so:
{ const char *be_name = "files"; struct ref_storage_be *be = find_ref_storage_backend(be_name); if (!be) die("BUG: reference backend %s is unknown", be_name); /* replace empty string by NULL */ if (submodule && !*submodule) submodule = NULL; register_ref_store(be->init(submodule), submodule); return refs; }
Quoted 1 line
Well, I dunno; the function inside the arguments to register seems ugly, though.
Nit: you forgot to define and initialize refs (for returning to the caller).
Actually, there is an inconsistency between the docstring for this function and its behavior. The docstring claims that it can handle submodule == "", and it tries to do so, but incorrectly. The problem is that it passes an un-cleaned-up submodule to register_ref_store(), which is expecting a cleaned-up one.
But this function is only called by get_ref_store(), which has already arranged for the empty string to be passed along as NULL.
In fact, the only external entry point into these functions is get_ref_store(). I think what I should do is make the other functions private, and remove their attempts to handle submodule == "". I'll fix this up in a re-roll.
(I wonder whether anybody really passes the empty string into this method. It never happens in the test suite. I doubt I can muster the energy to audit all of the call paths.)
Michael
Re: [PATCH 3/5] register_ref_store(): new function
In reply to Michael HaggertyQuoted 7 lines · Michael Haggerty
Move the responsibility for registering the
ref_storefor a submodule frombase_ref_store_init()to a new function,register_ref_store(). Call the latter fromref_store_init().This means that
base_ref_store_init()can lose its submodule argument, further weakening the 1:1 relationship betweenref_storesand submodules.
OK. I think I am starting to get it. I've always found it disturbing that for_each_*ref*() has a "submodule" variant. Instead, the plan (outlined in the discussion from yesterday that triggered your posting this series) is to give an API to request a "ref-store", and then ask that object to iterate over refs, and these steps get us closer to that goal, because the "to enumerate these" won't have to know what set of refs a ref-store contains. If you want to iterate over refs in a submodule, you grab a ref-store for the submodule and ask it to iterate. Iterating over refs in another worktree, you grab a different ref-store and ask it to iterate using the same API.
Sounds like a good direction to go.
PATCH 4/5 files_ref_store::submodule: use NULL for the main repository
In reply to Michael HaggertyThe old practice of storing the empty string in this member for the main repository was a holdover from before 00eebe3 (refs: create a base class "ref_store" for files_ref_store, 2016-09-04), when the submodule was stored in a flex array at the end of struct files_ref_store. Storing NULL for this case is more idiomatic and a tiny bit less code.
--- refs/files-backend.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
refs/files-backend.c +8 -8
@@ -915,8 +915,8 @@ struct files_ref_store {/* * The name of the submodule represented by this object, or -* the empty string if it represents the main repository's -* reference store: +* NULL if it represents the main repository's reference +* store: */ const char *submodule;
@@ -982,7 +982,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)base_ref_store_init (ref_store, &refs_be_files); - refs->submodule = submodule ?xstrdup (submodule) :"" ; + refs->submodule =xstrdup_or_null (submodule);return ref_store; }
@@ -994,7 +994,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)static void files_assert_main_repository (struct files_ref_store *refs,const char *caller) { -if (*refs->submodule) +if (refs->submodule)die ("BUG: %s called for a submodule" , caller); }
@@ -1158,7 +1158,7 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref {char *packed_refs_file; -if (*refs->submodule) +if (refs->submodule) packed_refs_file =git_pathdup_submodule (refs->submodule,"packed-refs" );else
@@ -1228,7 +1228,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)size_t path_baselen;int err =0 ; -if (*refs->submodule) +if (refs->submodule) err =strbuf_git_path_submodule (&path, refs->submodule,"%s" , dirname);else strbuf_git_path (&path,"%s" , dirname);
@@ -1269,7 +1269,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir) }else {int read_ok; -if (*refs->submodule) { +if (refs->submodule) {hashclr (sha1); flag =0 ; read_ok = !resolve_gitlink_ref(refs->submodule,
@@ -1383,7 +1383,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, *type =0 ;strbuf_reset (&sb_path); -if (*refs->submodule) +if (refs->submodule)strbuf_git_path_submodule (&sb_path, refs->submodule,"%s" , refname);else strbuf_git_path (&sb_path,"%s" , refname);
Signature · 1 line
2.9.3
Re: [PATCH 4/5] files_ref_store::submodule: use NULL for the main repository
Quoted 8 lines · Michael Haggerty
The old practice of storing the empty string in this member for the main repository was a holdover from before 00eebe3 (refs: create a base class "
ref_store" forfiles_ref_store, 2016-09-04), when the submodule was stored in a flex array at the end ofstruct files_ref_store. Storing NULL for this case is more idiomatic and a tiny bit less code.Signed-off-by Michael Haggerty <[email protected]>---
Makes sense!
Stefan
Re: [PATCH 4/5] files_ref_store::submodule: use NULL for the main repository
In reply to Michael HaggertyQuoted 5 lines · Michael Haggerty
The old practice of storing the empty string in this member for the main repository was a holdover from before 00eebe3 (refs: create a base class "
ref_store" forfiles_ref_store, 2016-09-04), when the submodule was stored in a flex array at the end ofstruct files_ref_store. Storing NULL for this case is more idiomatic and a tiny bit less code.
Yes. I noticed this bit in 3/5 and wondered about it, knowing this step comes next:
Quoted 5 lines
struct
ref_store*ref_store_init(const char *submodule) {(quoted diff) +1
-0const char *be_name = "files"; struct ref_storage_be *be = find_ref_storage_backend(be_name); + struct ref_store *refs;
Review
Quoted 10 lines
(quoted diff) +2
-2if (!be) die("BUG: reference backend %s is unknown", be_name); if (!submodule || !*submodule) - return be->init(NULL); + refs = be->init(NULL); else - return be->init(submodule); + refs = be->init(submodule);
Can't we also lose this "if !*submodule, turn it to NULL"?
refs/files-backend.c static struct ref_store *files_ref_store_create(const char *submodule) · line 985
Quoted 10 lines
refs/files-backend.c +1
-1@@ -980,7 +980,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)struct files_ref_store *refs =xcalloc (1 ,sizeof (*refs));struct ref_store *ref_store = (struct ref_store *)refs; -base_ref_store_init (ref_store, &refs_be_files, submodule); +base_ref_store_init (ref_store, &refs_be_files); refs->submodule = submodule ?xstrdup (submodule) :"" ;
Also, can't we use xstrdup_or_null(submodule) with this step?
Re: [PATCH 4/5] files_ref_store::submodule: use NULL for the main repository
Quoted 8 lines · Junio C Hamano
Review
Quoted 6 lines
if (!submodule || !*submodule)(quoted diff) +2
-2- return be->init(NULL); + refs = be->init(NULL); else - return be->init(submodule); + refs = be->init(submodule);Can't we also lose this "if !*submodule, turn it to NULL"?
That was my suggestion as well, but that did not look nicer per se. That is because at this point of the series we still handle "" just fine.
Quoted 4 lines
Quoted 1 line
refs->submodule = submodule ? xstrdup(submodule) : "";Also, can't we use
xstrdup_or_null(submodule)with this step?
That is done in 4/5; here we must keep a "" around instead of NULL IIUC.
PATCH 5/5 read_loose_refs(): read refs using resolve_ref_recursively()
In reply to Michael HaggertyThere is no need to call read_ref_full() or resolve_gitlink_ref() from read_loose_refs(), because we already have a ref_store object in hand. So we can call resolve_ref_recursively() ourselves. Happily, this unifies the code for the submodule vs. non-submodule cases.
This requires resolve_ref_recursively() to be exposed to the refs subsystem, though not to non-refs code.
--- refs.c | 8 ++++---- refs/files-backend.c | 18 ++++-------------- refs/refs-internal.h | 5 +++++ 3 files changed, 13 insertions(+), 18 deletions(-)
refs.c +4 -4
@@ -1235,10 +1235,10 @@ int for_each_rawref(each_ref_fn fn, void *cb_data) }/* This function needs to return a meaningful errno on failure */ -static const char *resolve_ref_recursively (struct ref_store *refs, -const char *refname, -int resolve_flags, -unsigned char *sha1,int *flags) +const char *resolve_ref_recursively (struct ref_store *refs, +const char *refname, +int resolve_flags, +unsigned char *sha1,int *flags) {static struct strbuf sb_refname =STRBUF_INIT ;int unused_flags;
refs/files-backend.c +4 -14
@@ -1267,20 +1267,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)create_dir_entry (refs, refname.buf, refname.len, 1)); }else { -int read_ok; - -if (refs->submodule) { -hashclr (sha1); - flag =0 ; - read_ok = !resolve_gitlink_ref (refs->submodule, - refname.buf, sha1); - }else { - read_ok = !read_ref_full (refname.buf, -RESOLVE_REF_READING , - sha1, &flag); - } - -if (!read_ok) { +if (!resolve_ref_recursively (&refs->base, + refname.buf, +RESOLVE_REF_READING , + sha1, &flag)) {hashclr (sha1); flag |=REF_ISBROKEN ; }else if (is_null_sha1 (sha1)) {
refs/refs-internal.h +5 -0
@@ -673,4 +673,9 @@ struct ref_store *lookup_ref_store(const char *submodule);*/ struct ref_store *get_ref_store (const char *submodule); +const char *resolve_ref_recursively (struct ref_store *refs, +const char *refname, +int resolve_flags, +unsigned char *sha1,int *flags); +#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.9.3
Re: [PATCH 5/5] read_loose_refs(): read refs using resolve_ref_recursively()
Quoted 10 lines · Michael Haggerty
There is no need to call
read_ref_full()orresolve_gitlink_ref()fromread_loose_refs(), because we already have aref_storeobject in hand. So we can callresolve_ref_recursively()ourselves. Happily, this unifies the code for the submodule vs. non-submodule cases.This requires
resolve_ref_recursively()to be exposed to the refs subsystem, though not to non-refs code.Signed-off-by Michael Haggerty <[email protected]>---
Looks good,
Stefan
Quoted 33 lines · Michael Haggerty
I have mentioned this patch series on the mailing list a couple of time [1,2] but haven't submitted it before. I just rebased it to current master. It is available from my Git fork [3] as branch "submodule-hash".
The first point of this patch series is to optimize submodule
ref_storelookup by storing theref_stores in a hashmap rather than a linked list. But a more interesting second point is to weaken the 1:1 relationship between submodules andref_storesa little bit more.A
files_ref_storewould be perfectly happy to represent, say, the references *physically* stored in a linked worktree (e.g.,HEAD,refs/bisect/*, etc) even though that is not the complete collection of refs that are *logically* visible from that worktree (which includes references from the main repository, too). But the old code was confusing the two things by storing "submodule" in everyref_storeinstance.So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).The last commit is relatively orthogonal to the others; it simplifies
read_loose_refs()by callingresolve_ref_recursively()directly using theref_storeinstance that it already has in hand, rather than indirectly via the public wrappers.Michael
[1] http://public-inbox.org/git/[email protected]/
[2] http://public-inbox.org/git/[email protected]/
[3] https://github.com/mhagger/git
Quoted 6 lines
Michael Haggerty (5):
refs: store submodule ref stores in a hashmap
refs: push the submodule attribute downregister_ref_store(): new functionfiles_ref_store::submodule: use NULL for the main repositoryread_loose_refs(): read refs usingresolve_ref_recursively()
Thanks. Will queue on mh/submodule-hash forked from 'maint'.
Quoted 10 lines · Michael Haggerty
I have mentioned this patch series on the mailing list a couple of time [1,2] but haven't submitted it before. I just rebased it to current master. It is available from my Git fork [3] as branch "submodule-hash".
The first point of this patch series is to optimize submodule
ref_storelookup by storing theref_stores in a hashmap rather than a linked list. But a more interesting second point is to weaken the 1:1 relationship between submodules andref_storesa little bit more.
Sounds good. I remember this had been discussed before due to performance issues with resolve_gitlink_ref(), and we took a different route (not populating non-submodule entries). I think it's nice to have both optimizations, though, as they hit different use cases.
Quoted 10 lines
A
files_ref_storewould be perfectly happy to represent, say, the references *physically* stored in a linked worktree (e.g.,HEAD,refs/bisect/*, etc) even though that is not the complete collection of refs that are *logically* visible from that worktree (which includes references from the main repository, too). But the old code was confusing the two things by storing "submodule" in everyref_storeinstance.So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).
I'm not sure I understand all of the ramifications here. It _sounds_ like pushing this down into the files-backend code would make it harder to have mixed ref-backends for different submodules. Or is this just pushing down an implementation detail of the files backend, and future code is free to have as many different ref_stores as it likes?
-Peff
Quoted 10 lines · Jeff King
On Thu, Feb 09, 2017 at 02:26:57PM +0100, Michael Haggerty wrote:
[...]Quoted 8 lines
A
files_ref_storewould be perfectly happy to represent, say, the references *physically* stored in a linked worktree (e.g.,HEAD,refs/bisect/*, etc) even though that is not the complete collection of refs that are *logically* visible from that worktree (which includes references from the main repository, too). But the old code was confusing the two things by storing "submodule" in everyref_storeinstance.
Quoted 8 lines
Quoted 2 lines
So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).I'm not sure I understand all of the ramifications here. It _sounds_ like pushing this down into the files-backend code would make it harder to have mixed ref-backends for different submodules. Or is this just pushing down an implementation detail of the files backend, and future code is free to have as many different
ref_storesas it likes?
I don't understand how this would make it harder, aside from the fact that a new backend class might also need a path member and have to maintain its own copy rather than using one that the base class provides.
I consider it an implementation detail of the files backend that it needs to keep a permanent record of its submodule path in files_ref_store. Some hypothetical future backend might instead need, say, an IP number and port to connect to a MySQL server. A hypothetical pure packed-refs backend might just store the path of the packed-refs file.
A more likely imminent change is that backends need a path, but that the path needn't correspond to the git_dir of the repository that contains the corresponding objects, for example in the case of a linked worktree. You might ask for the ref_store for a worktree-submodule, and end up getting a compound object that delegates to one ref_store pointing at its git_dir and one at its common_dir.
Michael
Quoted 9 lines · Michael Haggerty
Quoted 8 lines
Quoted 2 lines
So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).I'm not sure I understand all of the ramifications here. It _sounds_ like pushing this down into the files-backend code would make it harder to have mixed ref-backends for different submodules. Or is this just pushing down an implementation detail of the files backend, and future code is free to have as many different
ref_storesas it likes?
Quoted 3 lines
I don't understand how this would make it harder, aside from the fact that a new backend class might also need a path member and have to maintain its own copy rather than using one that the base class provides.
Probably the answer is "I'm really confused".
But here's how my line of reasoning went:
Right now we have a main ref-store that points to the submodule ref-stores. I don't know the current state of it, but in theory those could all use different backends.
This seems like it's pushing that submodule linkage down into the backend.
But I think from your response that the answer is no, the thing that is being pushed down is not the right way for the main ref store and the submodules to be linked. In fact, there is no reason at all for the main ref store to know or care about submodules. Anybody who wants to know about a submodule's refs should ask the hashmap.
-Peff
Quoted 23 lines · Jeff King
Quoted 9 lines · Michael Haggerty
Quoted 8 lines
Quoted 2 lines
So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).I'm not sure I understand all of the ramifications here. It _sounds_ like pushing this down into the files-backend code would make it harder to have mixed ref-backends for different submodules. Or is this just pushing down an implementation detail of the files backend, and future code is free to have as many different
ref_storesas it likes?Quoted 3 lines
I don't understand how this would make it harder, aside from the fact that a new backend class might also need a path member and have to maintain its own copy rather than using one that the base class provides.
Probably the answer is "I'm really confused".
But here's how my line of reasoning went:
Right now we have a main ref-store that points to the submodule ref-stores. I don't know the current state of it, but in theory those could all use different backends.
Quoted 8 lines
This seems like it's pushing that submodule linkage down into the backend.
But I think from your response that the answer is no, the thing that is being pushed down is not the right way for the main ref store and the submodules to be linked. In fact, there is no reason at all for the main ref store to know or care about submodules. Anybody who wants to know about a submodule's refs should ask the hashmap.
That's correct; the main ref store and submodule ref stores know nothing of each other.
Michael
Quoted 23 lines · Jeff King
Quoted 9 lines · Michael Haggerty
Quoted 8 lines
Quoted 2 lines
So push the submodule attribute down to the
files_ref_storeclass (but continue to let theref_stores be looked up by submodule).I'm not sure I understand all of the ramifications here. It _sounds_ like pushing this down into the files-backend code would make it harder to have mixed ref-backends for different submodules. Or is this just pushing down an implementation detail of the files backend, and future code is free to have as many different
ref_storesas it likes?Quoted 3 lines
I don't understand how this would make it harder, aside from the fact that a new backend class might also need a path member and have to maintain its own copy rather than using one that the base class provides.
Probably the answer is "I'm really confused".
But here's how my line of reasoning went:
Right now we have a main ref-store that points to the submodule ref-stores. I don't know the current state of it, but in theory those could all use different backends.
Quoted 6 lines
This seems like it's pushing that submodule linkage down into the backend.
But I think from your response that the answer is no, the thing that is being pushed down is not the right way for the main ref store and the submodules to be linked.
I think it's more about "pushing out" than "pushing down". Once files backend takes a path to .git directory, we could have a submodule ref_store that resolves submodule path to that .git directory, files-backend will not need to know anything about submodules.
I imagine in future lookup_ref_store() will take a .git path instead of a submodule path, then iterate through all backends and call the backend-specific "probe" function to let the backend figure out if it's the right backend and whatever parameters it needs (e.g. IP address or path). There would be submodule_lookup_ref_store() wrapper that converts submodule path to .git path for lookup_ref_store() to consume.
--
Duy