git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	Heiko Voigt <hvoigt@hvoigt.net>
Cc: git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH 33/33] refs: handle the main ref_cache specially
Date: Sun, 14 Apr 2013 14:54:48 +0200	[thread overview]
Message-ID: <1365944088-10588-34-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1365944088-10588-1-git-send-email-mhagger@alum.mit.edu>

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>
---
 refs.c | 60 +++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/refs.c b/refs.c
index cfcb42f..13e5bc8 100644
--- a/refs.c
+++ b/refs.c
@@ -812,9 +812,13 @@ static struct ref_cache {
 	struct ref_cache *next;
 	struct ref_entry *loose;
 	struct ref_entry *packed;
-	/* The submodule name, or "" for the main repo. */
-	char name[FLEX_ARRAY];
-} *ref_cache;
+	/*
+	 * The submodule name, or "" for the main repo.  We allocate
+	 * length 1 rather than FLEX_ARRAY so that the main ref_cache
+	 * is initialized correctly.
+	 */
+	char name[1];
+} ref_cache, *submodule_ref_caches;
 
 static void clear_packed_ref_cache(struct ref_cache *refs)
 {
@@ -852,18 +856,18 @@ static struct ref_cache *create_ref_cache(const char *submodule)
  */
 static struct ref_cache *get_ref_cache(const char *submodule)
 {
-	struct ref_cache *refs = ref_cache;
-	if (!submodule)
-		submodule = "";
-	while (refs) {
+	struct ref_cache *refs;
+
+	if (!submodule || !*submodule)
+		return &ref_cache;
+
+	for (refs = submodule_ref_caches; refs; refs = refs->next)
 		if (!strcmp(submodule, refs->name))
 			return refs;
-		refs = refs->next;
-	}
 
 	refs = create_ref_cache(submodule);
-	refs->next = ref_cache;
-	ref_cache = refs;
+	refs->next = submodule_ref_caches;
+	submodule_ref_caches = refs;
 	return refs;
 }
 
@@ -1012,8 +1016,8 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
 
 void add_packed_ref(const char *refname, const unsigned char *sha1)
 {
-	add_ref(get_packed_refs(get_ref_cache(NULL)),
-			create_ref_entry(refname, sha1, REF_ISPACKED, 1));
+	add_ref(get_packed_refs(&ref_cache),
+		create_ref_entry(refname, sha1, REF_ISPACKED, 1));
 }
 
 /*
@@ -1188,7 +1192,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
  */
 static struct ref_entry *get_packed_ref(const char *refname)
 {
-	return find_ref(get_packed_refs(get_ref_cache(NULL)), refname);
+	return find_ref(get_packed_refs(&ref_cache), refname);
 }
 
 const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
@@ -1589,7 +1593,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 
 int for_each_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(get_ref_cache(NULL), "", fn, 0, 0, cb_data);
+	return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
 }
 
 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
@@ -1599,7 +1603,7 @@ int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 
 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(get_ref_cache(NULL), prefix, fn, strlen(prefix), 0, cb_data);
+	return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
@@ -1640,7 +1644,7 @@ int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *c
 
 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(get_ref_cache(NULL), "refs/replace/", fn, 13, 0, cb_data);
+	return do_for_each_ref(&ref_cache, "refs/replace/", fn, 13, 0, cb_data);
 }
 
 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
@@ -1663,7 +1667,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
 	struct strbuf buf = STRBUF_INIT;
 	int ret;
 	strbuf_addf(&buf, "%srefs/", get_git_namespace());
-	ret = do_for_each_ref(get_ref_cache(NULL), buf.buf, fn, 0, 0, cb_data);
+	ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data);
 	strbuf_release(&buf);
 	return ret;
 }
@@ -1705,7 +1709,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
 
 int for_each_rawref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(get_ref_cache(NULL), "", fn, 0,
+	return do_for_each_ref(&ref_cache, "", fn, 0,
 			       DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
 }
 
@@ -1911,7 +1915,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	 * name is a proper prefix of our refname.
 	 */
 	if (missing &&
-	     !is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) {
+	     !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {
 		last_errno = ENOTDIR;
 		goto error_return;
 	}
@@ -2100,7 +2104,7 @@ int pack_refs(unsigned int flags)
 
 	write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
 
-	do_for_each_entry(get_ref_cache(NULL), "", pack_one_ref, &cbdata);
+	do_for_each_entry(&ref_cache, "", pack_one_ref, &cbdata);
 	if (commit_lock_file(&packlock) < 0)
 		die_errno("unable to overwrite old ref-pack file");
 	prune_refs(cbdata.ref_to_prune);
@@ -2126,7 +2130,6 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
 static int repack_without_ref(const char *refname)
 {
 	int fd;
-	struct ref_cache *refs = get_ref_cache(NULL);
 	struct ref_dir *packed;
 
 	if (!get_packed_ref(refname))
@@ -2137,8 +2140,8 @@ static int repack_without_ref(const char *refname)
 		unable_to_lock_error(git_path("packed-refs"), errno);
 		return error("cannot delete '%s' from packed refs", refname);
 	}
-	clear_packed_ref_cache(refs);
-	packed = get_packed_refs(refs);
+	clear_packed_ref_cache(&ref_cache);
+	packed = get_packed_refs(&ref_cache);
 	/* Remove refname from the cache. */
 	if (remove_entry(packed, refname) == -1) {
 		/*
@@ -2178,7 +2181,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
 	ret |= repack_without_ref(lock->ref_name);
 
 	unlink_or_warn(git_path("logs/%s", lock->ref_name));
-	clear_loose_ref_cache(get_ref_cache(NULL));
+	clear_loose_ref_cache(&ref_cache);
 	unlock_ref(lock);
 	return ret;
 }
@@ -2200,7 +2203,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	struct stat loginfo;
 	int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
 	const char *symref = NULL;
-	struct ref_cache *refs = get_ref_cache(NULL);
 
 	if (log && S_ISLNK(loginfo.st_mode))
 		return error("reflog for %s is a symlink", oldrefname);
@@ -2212,10 +2214,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	if (!symref)
 		return error("refname %s not found", oldrefname);
 
-	if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs)))
+	if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))
 		return 1;
 
-	if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs)))
+	if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))
 		return 1;
 
 	if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
@@ -2471,7 +2473,7 @@ int write_ref_sha1(struct ref_lock *lock,
 		unlock_ref(lock);
 		return -1;
 	}
-	clear_loose_ref_cache(get_ref_cache(NULL));
+	clear_loose_ref_cache(&ref_cache);
 	if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
 	    (strcmp(lock->ref_name, lock->orig_ref_name) &&
 	     log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
-- 
1.8.2.1

      parent reply	other threads:[~2013-04-14 12:56 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-14 12:54 [PATCH 00/33] Various cleanups around reference packing and peeling Michael Haggerty
2013-04-14 12:54 ` [PATCH 01/33] refs: document flags constants REF_* Michael Haggerty
2013-04-14 12:54 ` [PATCH 02/33] refs: document the fields of struct ref_value Michael Haggerty
2013-04-14 12:54 ` [PATCH 03/33] refs: document do_for_each_ref() and do_one_ref() Michael Haggerty
2013-04-15 17:38   ` Junio C Hamano
2013-04-16  9:11     ` Michael Haggerty
2013-04-14 12:54 ` [PATCH 04/33] refs: document how current_ref is used Michael Haggerty
2013-04-14 12:54 ` [PATCH 05/33] refs: define constant PEELED_LINE_LENGTH Michael Haggerty
2013-04-14 12:54 ` [PATCH 06/33] do_for_each_ref_in_dirs(): remove dead code Michael Haggerty
2013-04-14 12:54 ` [PATCH 07/33] get_packed_ref(): return a ref_entry Michael Haggerty
2013-04-14 12:54 ` [PATCH 08/33] peel_ref(): use function get_packed_ref() Michael Haggerty
2013-04-14 12:54 ` [PATCH 09/33] repack_without_ref(): " Michael Haggerty
2013-04-14 12:54 ` [PATCH 10/33] refs: extract a function ref_resolves_to_object() Michael Haggerty
2013-04-15 16:51   ` Junio C Hamano
2013-04-16  9:27     ` Michael Haggerty
2013-04-16 18:08       ` Junio C Hamano
2013-04-14 12:54 ` [PATCH 11/33] refs: extract function peel_object() Michael Haggerty
2013-04-14 12:54 ` [PATCH 12/33] peel_object(): give more specific information in return value Michael Haggerty
2013-04-15 17:38   ` Junio C Hamano
2013-04-14 12:54 ` [PATCH 13/33] peel_ref(): fix return value for non-peelable, not-current reference Michael Haggerty
2013-04-15 17:38   ` Junio C Hamano
2013-04-16  9:38     ` Michael Haggerty
2013-04-14 12:54 ` [PATCH 14/33] refs: extract a function peel_entry() Michael Haggerty
2013-04-15 17:38   ` Junio C Hamano
2013-04-16 13:07     ` Michael Haggerty
2013-04-16 17:55       ` Junio C Hamano
2013-04-16 22:01         ` Michael Haggerty
2013-04-14 12:54 ` [PATCH 15/33] refs: change the internal reference-iteration API Michael Haggerty
2013-04-15 17:38   ` Junio C Hamano
2013-04-16 13:27     ` Michael Haggerty
2013-04-16 17:47       ` Junio C Hamano
2013-04-14 12:54 ` [PATCH 16/33] t3210: test for spurious error messages for dangling packed refs Michael Haggerty
2013-04-15 17:39   ` Junio C Hamano
2013-04-16 14:14     ` Michael Haggerty
2013-04-16 23:22       ` Junio C Hamano
2013-04-16 23:57         ` Jeff King
2013-04-17  4:42           ` Junio C Hamano
2013-04-17 22:38             ` Junio C Hamano
2013-04-18  7:46               ` [PATCH 0/2] Add documentation for new expiry option values Michael Haggerty
2013-04-18  7:46                 ` [PATCH 1/2] git-gc.txt, git-reflog.txt: document new expiry options Michael Haggerty
2013-04-18  7:46                 ` [PATCH 2/2] api-parse-options.txt: document "no-" for non-boolean options Michael Haggerty
2013-04-25 18:13                 ` [PATCH] prune: introduce OPT_EXPIRY_DATE() and use it Junio C Hamano
2013-04-17  8:11         ` [PATCH 16/33] t3210: test for spurious error messages for dangling packed refs Michael Haggerty
2013-04-14 12:54 ` [PATCH 17/33] repack_without_ref(): silence errors " Michael Haggerty
2013-04-15 17:39   ` Junio C Hamano
2013-04-17  8:41     ` Michael Haggerty
2013-04-14 12:54 ` [PATCH 18/33] search_ref_dir(): return an index rather than a pointer Michael Haggerty
2013-04-14 12:54 ` [PATCH 19/33] refs: change how packed refs are deleted Michael Haggerty
2013-04-14 12:54 ` [PATCH 20/33] t3211: demonstrate loss of peeled refs if a packed ref is deleted Michael Haggerty
2013-04-14 12:54 ` [PATCH 21/33] repack_without_ref(): write peeled refs in the rewritten file Michael Haggerty
2013-04-15 17:39   ` Junio C Hamano
2013-04-14 12:54 ` [PATCH 22/33] refs: extract a function write_packed_entry() Michael Haggerty
2013-04-14 12:54 ` [PATCH 23/33] pack-refs: rename handle_one_ref() to pack_one_ref() Michael Haggerty
2013-04-14 12:54 ` [PATCH 24/33] pack-refs: merge code from pack-refs.{c,h} into refs.{c,h} Michael Haggerty
2013-04-14 12:54 ` [PATCH 25/33] pack_one_ref(): rename "path" parameter to "refname" Michael Haggerty
2013-04-14 12:54 ` [PATCH 26/33] refs: use same lock_file object for both ref-packing functions Michael Haggerty
2013-04-14 12:54 ` [PATCH 27/33] pack_refs(): change to use do_for_each_entry() Michael Haggerty
2013-04-14 12:54 ` [PATCH 28/33] refs: inline function do_not_prune() Michael Haggerty
2013-04-14 12:54 ` [PATCH 29/33] pack_one_ref(): use function peel_entry() Michael Haggerty
2013-04-14 12:54 ` [PATCH 30/33] pack_one_ref(): use write_packed_entry() to do the writing Michael Haggerty
2013-04-14 12:54 ` [PATCH 31/33] pack_one_ref(): do some cheap tests before a more expensive one Michael Haggerty
2013-04-14 12:54 ` [PATCH 32/33] refs: change do_for_each_*() functions to take ref_cache arguments Michael Haggerty
2013-04-15 17:40   ` Junio C Hamano
2013-04-14 12:54 ` Michael Haggerty [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1365944088-10588-34-git-send-email-mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=hvoigt@hvoigt.net \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).