refs: handle the main ref_cache specially

Hold the ref_cache instance for the main repository in a dedicated, statically-allocated instance to avoid the need for a function call and a linked-list traversal when it is needed. Suggested by: Heiko Voigt <hvoigt@hvoigt.net> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 22, 2013 at 21:52 UTC 9da31cb027aa5dac3d4914a88faa8830f0578c88
1 file changed +31 -29
refs.c
+31 -29
@@ -811,9 +811,13 @@ static struct ref_cache {
811 struct ref_cache *next;
812 struct ref_entry *loose;
813 struct ref_entry *packed;
814 - /* The submodule name, or "" for the main repo. */
815 - char name[FLEX_ARRAY];
816 -} *ref_cache;
814 + /*
815 + * The submodule name, or "" for the main repo. We allocate
816 + * length 1 rather than FLEX_ARRAY so that the main ref_cache
817 + * is initialized correctly.
818 + */
819 + char name[1];
820 +} ref_cache, *submodule_ref_caches;
821
822 static void clear_packed_ref_cache(struct ref_cache *refs)
823 {
@@ -851,18 +855,18 @@ static struct ref_cache *create_ref_cache(const char *submodule)
855 */
856 static struct ref_cache *get_ref_cache(const char *submodule)
857 {
854 - struct ref_cache *refs = ref_cache;
855 - if (!submodule)
856 - submodule = "";
857 - while (refs) {
858 + struct ref_cache *refs;
859 +
860 + if (!submodule || !*submodule)
861 + return &ref_cache;
862 +
863 + for (refs = submodule_ref_caches; refs; refs = refs->next)
864 if (!strcmp(submodule, refs->name))
865 return refs;
860 - refs = refs->next;
861 - }
866
867 refs = create_ref_cache(submodule);
864 - refs->next = ref_cache;
865 - ref_cache = refs;
868 + refs->next = submodule_ref_caches;
869 + submodule_ref_caches = refs;
870 return refs;
871 }
872
@@ -1011,8 +1015,8 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1015
1016 void add_packed_ref(const char *refname, const unsigned char *sha1)
1017 {
1014 - add_ref(get_packed_refs(get_ref_cache(NULL)),
1015 - create_ref_entry(refname, sha1, REF_ISPACKED, 1));
1018 + add_ref(get_packed_refs(&ref_cache),
1019 + create_ref_entry(refname, sha1, REF_ISPACKED, 1));
1020 }
1021
1022 /*
@@ -1187,7 +1191,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
1191 */
1192 static struct ref_entry *get_packed_ref(const char *refname)
1193 {
1190 - return find_ref(get_packed_refs(get_ref_cache(NULL)), refname);
1194 + return find_ref(get_packed_refs(&ref_cache), refname);
1195 }
1196
1197 const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
@@ -1592,7 +1596,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1596
1597 int for_each_ref(each_ref_fn fn, void *cb_data)
1598 {
1595 - return do_for_each_ref(get_ref_cache(NULL), "", fn, 0, 0, cb_data);
1599 + return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
1600 }
1601
1602 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
@@ -1602,7 +1606,7 @@ int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1606
1607 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1608 {
1605 - return do_for_each_ref(get_ref_cache(NULL), prefix, fn, strlen(prefix), 0, cb_data);
1609 + return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
1610 }
1611
1612 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
@@ -1643,7 +1647,7 @@ int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *c
1647
1648 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1649 {
1646 - return do_for_each_ref(get_ref_cache(NULL), "refs/replace/", fn, 13, 0, cb_data);
1650 + return do_for_each_ref(&ref_cache, "refs/replace/", fn, 13, 0, cb_data);
1651 }
1652
1653 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
@@ -1666,7 +1670,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1670 struct strbuf buf = STRBUF_INIT;
1671 int ret;
1672 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1669 - ret = do_for_each_ref(get_ref_cache(NULL), buf.buf, fn, 0, 0, cb_data);
1673 + ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data);
1674 strbuf_release(&buf);
1675 return ret;
1676 }
@@ -1708,7 +1712,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
1712
1713 int for_each_rawref(each_ref_fn fn, void *cb_data)
1714 {
1711 - return do_for_each_ref(get_ref_cache(NULL), "", fn, 0,
1715 + return do_for_each_ref(&ref_cache, "", fn, 0,
1716 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1717 }
1718
@@ -1914,7 +1918,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1918 * name is a proper prefix of our refname.
1919 */
1920 if (missing &&
1917 - !is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) {
1921 + !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {
1922 last_errno = ENOTDIR;
1923 goto error_return;
1924 }
@@ -2103,7 +2107,7 @@ int pack_refs(unsigned int flags)
2107
2108 write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
2109
2106 - do_for_each_entry(get_ref_cache(NULL), "", pack_one_ref, &cbdata);
2110 + do_for_each_entry(&ref_cache, "", pack_one_ref, &cbdata);
2111 if (commit_lock_file(&packlock) < 0)
2112 die_errno("unable to overwrite old ref-pack file");
2113 prune_refs(cbdata.ref_to_prune);
@@ -2161,7 +2165,6 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
2165 static int repack_without_ref(const char *refname)
2166 {
2167 int fd;
2164 - struct ref_cache *refs = get_ref_cache(NULL);
2168 struct ref_dir *packed;
2169
2170 if (!get_packed_ref(refname))
@@ -2172,8 +2175,8 @@ static int repack_without_ref(const char *refname)
2175 unable_to_lock_error(git_path("packed-refs"), errno);
2176 return error("cannot delete '%s' from packed refs", refname);
2177 }
2175 - clear_packed_ref_cache(refs);
2176 - packed = get_packed_refs(refs);
2178 + clear_packed_ref_cache(&ref_cache);
2179 + packed = get_packed_refs(&ref_cache);
2180 /* Remove refname from the cache. */
2181 if (remove_entry(packed, refname) == -1) {
2182 /*
@@ -2213,7 +2216,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
2216 ret |= repack_without_ref(lock->ref_name);
2217
2218 unlink_or_warn(git_path("logs/%s", lock->ref_name));
2216 - clear_loose_ref_cache(get_ref_cache(NULL));
2219 + clear_loose_ref_cache(&ref_cache);
2220 unlock_ref(lock);
2221 return ret;
2222 }
@@ -2235,7 +2238,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2238 struct stat loginfo;
2239 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
2240 const char *symref = NULL;
2238 - struct ref_cache *refs = get_ref_cache(NULL);
2241
2242 if (log && S_ISLNK(loginfo.st_mode))
2243 return error("reflog for %s is a symlink", oldrefname);
@@ -2247,10 +2249,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2249 if (!symref)
2250 return error("refname %s not found", oldrefname);
2251
2250 - if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs)))
2252 + if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))
2253 return 1;
2254
2253 - if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs)))
2255 + if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))
2256 return 1;
2257
2258 if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
@@ -2506,7 +2508,7 @@ int write_ref_sha1(struct ref_lock *lock,
2508 unlock_ref(lock);
2509 return -1;
2510 }
2509 - clear_loose_ref_cache(get_ref_cache(NULL));
2511 + clear_loose_ref_cache(&ref_cache);
2512 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
2513 (strcmp(lock->ref_name, lock->orig_ref_name) &&
2514 log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {