From f130b1168e95f51dcd6c7f3667b14bd244a9c61e Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:24 +0200 Subject: [PATCH 1/7] Extract a function clear_cached_refs() Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/refs.c b/refs.c index 3a8789d385..9dc25c3984 100644 --- a/refs.c +++ b/refs.c @@ -171,10 +171,8 @@ static void free_ref_list(struct ref_list *list) } } -static void invalidate_cached_refs(void) +static void clear_cached_refs(struct cached_refs *ca) { - struct cached_refs *ca = &cached_refs; - if (ca->did_loose && ca->loose) free_ref_list(ca->loose); if (ca->did_packed && ca->packed) @@ -183,6 +181,11 @@ static void invalidate_cached_refs(void) ca->did_loose = ca->did_packed = 0; } +static void invalidate_cached_refs(void) +{ + clear_cached_refs(&cached_refs); +} + static void read_packed_refs(FILE *f, struct cached_refs *cached_refs) { struct ref_list *list = NULL; From 4349a66805129ba11121b8a5eaf3f81cfd30bdd5 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:25 +0200 Subject: [PATCH 2/7] Access reference caches only through new function get_cached_refs() Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 54 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/refs.c b/refs.c index 9dc25c3984..df4ce42281 100644 --- a/refs.c +++ b/refs.c @@ -181,9 +181,26 @@ static void clear_cached_refs(struct cached_refs *ca) ca->did_loose = ca->did_packed = 0; } +/* + * Return a pointer to a cached_refs for the specified submodule. For + * the main repository, use submodule==NULL. The returned structure + * will be allocated and initialized but not necessarily populated; it + * should not be freed. + */ +static struct cached_refs *get_cached_refs(const char *submodule) +{ + if (!submodule) + return &cached_refs; + else { + /* For now, don't reuse the refs cache for submodules. */ + clear_cached_refs(&submodule_refs); + return &submodule_refs; + } +} + static void invalidate_cached_refs(void) { - clear_cached_refs(&cached_refs); + clear_cached_refs(get_cached_refs(NULL)); } static void read_packed_refs(FILE *f, struct cached_refs *cached_refs) @@ -234,20 +251,17 @@ void clear_extra_refs(void) static struct ref_list *get_packed_refs(const char *submodule) { - const char *packed_refs_file; - struct cached_refs *refs; + struct cached_refs *refs = get_cached_refs(submodule); - if (submodule) { - packed_refs_file = git_path_submodule(submodule, "packed-refs"); - refs = &submodule_refs; - free_ref_list(refs->packed); - } else { - packed_refs_file = git_path("packed-refs"); - refs = &cached_refs; - } + if (!refs->did_packed) { + const char *packed_refs_file; + FILE *f; - if (!refs->did_packed || submodule) { - FILE *f = fopen(packed_refs_file, "r"); + if (submodule) + packed_refs_file = git_path_submodule(submodule, "packed-refs"); + else + packed_refs_file = git_path("packed-refs"); + f = fopen(packed_refs_file, "r"); refs->packed = NULL; if (f) { read_packed_refs(f, refs); @@ -361,17 +375,13 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) static struct ref_list *get_loose_refs(const char *submodule) { - if (submodule) { - free_ref_list(submodule_refs.loose); - submodule_refs.loose = get_ref_dir(submodule, "refs", NULL); - return submodule_refs.loose; - } + struct cached_refs *refs = get_cached_refs(submodule); - if (!cached_refs.did_loose) { - cached_refs.loose = get_ref_dir(NULL, "refs", NULL); - cached_refs.did_loose = 1; + if (!refs->did_loose) { + refs->loose = get_ref_dir(submodule, "refs", NULL); + refs->did_loose = 1; } - return cached_refs.loose; + return refs->loose; } /* We allow "recursive" symbolic refs. Only within reason, though */ From db4dd93a4a3553c61b416db924520b55bfee5f7e Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:26 +0200 Subject: [PATCH 3/7] Change the signature of read_packed_refs() Change it to return a (struct ref_list *) instead of writing into a cached_refs structure. (This removes the need to create a cached_refs structure in resolve_gitlink_packed_ref(), where it is otherwise unneeded.) Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/refs.c b/refs.c index df4ce42281..10aebcc689 100644 --- a/refs.c +++ b/refs.c @@ -203,7 +203,7 @@ static void invalidate_cached_refs(void) clear_cached_refs(get_cached_refs(NULL)); } -static void read_packed_refs(FILE *f, struct cached_refs *cached_refs) +static struct ref_list *read_packed_refs(FILE *f) { struct ref_list *list = NULL; struct ref_list *last = NULL; @@ -235,7 +235,7 @@ static void read_packed_refs(FILE *f, struct cached_refs *cached_refs) !get_sha1_hex(refline + 1, sha1)) hashcpy(last->peeled, sha1); } - cached_refs->packed = sort_ref_list(list); + return sort_ref_list(list); } void add_extra_ref(const char *name, const unsigned char *sha1, int flag) @@ -264,7 +264,7 @@ static struct ref_list *get_packed_refs(const char *submodule) f = fopen(packed_refs_file, "r"); refs->packed = NULL; if (f) { - read_packed_refs(f, refs); + refs->packed = read_packed_refs(f); fclose(f); } refs->did_packed = 1; @@ -391,7 +391,7 @@ static struct ref_list *get_loose_refs(const char *submodule) static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refname, unsigned char *result) { FILE *f; - struct cached_refs refs; + struct ref_list *packed_refs; struct ref_list *ref; int retval; @@ -399,9 +399,9 @@ static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refna f = fopen(name, "r"); if (!f) return -1; - read_packed_refs(f, &refs); + packed_refs = read_packed_refs(f); fclose(f); - ref = refs.packed; + ref = packed_refs; retval = -1; while (ref) { if (!strcmp(ref->name, refname)) { @@ -411,7 +411,7 @@ static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refna } ref = ref->next; } - free_ref_list(refs.packed); + free_ref_list(packed_refs); return retval; } From e5dbf6056faecbd9a3c5510f5c5cd73de10374b8 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:27 +0200 Subject: [PATCH 4/7] Allocate cached_refs objects dynamically Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/refs.c b/refs.c index 10aebcc689..b325fc71fe 100644 --- a/refs.c +++ b/refs.c @@ -157,7 +157,7 @@ static struct cached_refs { char did_packed; struct ref_list *loose; struct ref_list *packed; -} cached_refs, submodule_refs; +} *cached_refs, *submodule_refs; static struct ref_list *current_ref; static struct ref_list *extra_refs; @@ -181,6 +181,15 @@ static void clear_cached_refs(struct cached_refs *ca) ca->did_loose = ca->did_packed = 0; } +struct cached_refs *create_cached_refs(void) +{ + struct cached_refs *refs; + refs = xmalloc(sizeof(struct cached_refs)); + refs->did_loose = refs->did_packed = 0; + refs->loose = refs->packed = NULL; + return refs; +} + /* * Return a pointer to a cached_refs for the specified submodule. For * the main repository, use submodule==NULL. The returned structure @@ -189,12 +198,17 @@ static void clear_cached_refs(struct cached_refs *ca) */ static struct cached_refs *get_cached_refs(const char *submodule) { - if (!submodule) - return &cached_refs; - else { - /* For now, don't reuse the refs cache for submodules. */ - clear_cached_refs(&submodule_refs); - return &submodule_refs; + if (!submodule) { + if (!cached_refs) + cached_refs = create_cached_refs(); + return cached_refs; + } else { + if (!submodule_refs) + submodule_refs = create_cached_refs(); + else + /* For now, don't reuse the refs cache for submodules. */ + clear_cached_refs(submodule_refs); + return submodule_refs; } } From ce40979cf83c4c92421f9dd56cc4eabb67c85f29 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:28 +0200 Subject: [PATCH 5/7] Store the submodule name in struct cached_refs Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/refs.c b/refs.c index b325fc71fe..6fd351174a 100644 --- a/refs.c +++ b/refs.c @@ -157,6 +157,8 @@ static struct cached_refs { char did_packed; struct ref_list *loose; struct ref_list *packed; + /* The submodule name, or "" for the main repo. */ + char name[FLEX_ARRAY]; } *cached_refs, *submodule_refs; static struct ref_list *current_ref; @@ -181,12 +183,17 @@ static void clear_cached_refs(struct cached_refs *ca) ca->did_loose = ca->did_packed = 0; } -struct cached_refs *create_cached_refs(void) +struct cached_refs *create_cached_refs(const char *submodule) { + int len; struct cached_refs *refs; - refs = xmalloc(sizeof(struct cached_refs)); + if (!submodule) + submodule = ""; + len = strlen(submodule) + 1; + refs = xmalloc(sizeof(struct cached_refs) + len); refs->did_loose = refs->did_packed = 0; refs->loose = refs->packed = NULL; + memcpy(refs->name, submodule, len); return refs; } @@ -200,11 +207,11 @@ static struct cached_refs *get_cached_refs(const char *submodule) { if (!submodule) { if (!cached_refs) - cached_refs = create_cached_refs(); + cached_refs = create_cached_refs(submodule); return cached_refs; } else { if (!submodule_refs) - submodule_refs = create_cached_refs(); + submodule_refs = create_cached_refs(submodule); else /* For now, don't reuse the refs cache for submodules. */ clear_cached_refs(submodule_refs); From 0e88c130f2f6309932464d4919725e54837978c7 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 13 Aug 2011 00:36:29 +0200 Subject: [PATCH 6/7] Retain caches of submodule refs Instead of keeping track of one cache for refs in the main repo and another single cache shared among submodules, keep a linked list of cached_refs objects, one for each module/submodule. Change invalidate_cached_refs() to invalidate all caches. (Previously, it only invalidated the cache of the main repo because the submodule caches were not reused anyway.) Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/refs.c b/refs.c index 6fd351174a..2811d8e053 100644 --- a/refs.c +++ b/refs.c @@ -153,13 +153,15 @@ static struct ref_list *sort_ref_list(struct ref_list *list) * when doing a full libification. */ static struct cached_refs { + struct cached_refs *next; char did_loose; char did_packed; struct ref_list *loose; struct ref_list *packed; /* The submodule name, or "" for the main repo. */ char name[FLEX_ARRAY]; -} *cached_refs, *submodule_refs; +} *cached_refs; + static struct ref_list *current_ref; static struct ref_list *extra_refs; @@ -191,6 +193,7 @@ struct cached_refs *create_cached_refs(const char *submodule) submodule = ""; len = strlen(submodule) + 1; refs = xmalloc(sizeof(struct cached_refs) + len); + refs->next = NULL; refs->did_loose = refs->did_packed = 0; refs->loose = refs->packed = NULL; memcpy(refs->name, submodule, len); @@ -205,23 +208,28 @@ struct cached_refs *create_cached_refs(const char *submodule) */ static struct cached_refs *get_cached_refs(const char *submodule) { - if (!submodule) { - if (!cached_refs) - cached_refs = create_cached_refs(submodule); - return cached_refs; - } else { - if (!submodule_refs) - submodule_refs = create_cached_refs(submodule); - else - /* For now, don't reuse the refs cache for submodules. */ - clear_cached_refs(submodule_refs); - return submodule_refs; + struct cached_refs *refs = cached_refs; + if (!submodule) + submodule = ""; + while (refs) { + if (!strcmp(submodule, refs->name)) + return refs; + refs = refs->next; } + + refs = create_cached_refs(submodule); + refs->next = cached_refs; + cached_refs = refs; + return refs; } static void invalidate_cached_refs(void) { - clear_cached_refs(get_cached_refs(NULL)); + struct cached_refs *refs = cached_refs; + while (refs) { + clear_cached_refs(refs); + refs = refs->next; + } } static struct ref_list *read_packed_refs(FILE *f) From 3ab24efeef37f9a148662f8de4412a72e754b69f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 11 Sep 2011 15:59:26 -0700 Subject: [PATCH 7/7] refs.c: make create_cached_refs() static There is nobody outside that calls into this helper function. Signed-off-by: Junio C Hamano --- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/refs.c b/refs.c index 2811d8e053..2cb93e2f7b 100644 --- a/refs.c +++ b/refs.c @@ -185,7 +185,7 @@ static void clear_cached_refs(struct cached_refs *ca) ca->did_loose = ca->did_packed = 0; } -struct cached_refs *create_cached_refs(const char *submodule) +static struct cached_refs *create_cached_refs(const char *submodule) { int len; struct cached_refs *refs;