git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Michael Haggerty <mhagger@alum.mit.edu>
Cc: Karthik Nayak <karthik.188@gmail.com>,
	git discussion list <git@vger.kernel.org>
Subject: Re: Performance regression in `git branch` due to ref-filter usage
Date: Wed, 17 May 2017 10:04:18 -0400	[thread overview]
Message-ID: <20170517140417.kwzznw4su36k6pxv@sigill.intra.peff.net> (raw)
In-Reply-To: <dfc3a334-8047-26b0-1142-81c703010507@alum.mit.edu>

On Wed, May 17, 2017 at 01:14:34PM +0200, Michael Haggerty wrote:

> While working on reference code, I was running `git branch` under
> `strace`, when I noticed that `$GIT_DIR/HEAD` was being `lstat()`ed and
> `read()` 121 times. This is in a repository with 114 branches, so
> probably it is being run once per branch. The extra work makes a
> measurable difference to the (admittedly, short) runtime.
> 
> As recently as 2.12.3 the file was only read 4 times when running the
> same command [1].
> 
> The regression bisects to
> 
>     949af0684c (branch: use ref-filter printing APIs, 2017-01-10)
> 
> It would be nice if these extra syscalls could be avoided.
> 
> I haven't checked whether other commands have similar regressions.

It looks like it's part of populate_value(). Each ref checks %(HEAD),
and resolve HEAD individually to see if we're it. So it probably doesn't
affect other commands by default (though you could specify %(HEAD)
manually via for-each-ref).

The solution is to cache the value we read and use it to compare against
each ref. I'm not sure if we can do something more elegant than the
patch below, which just caches it for the length of the program.

> [1] One wonders why the file has to be read more than once, but that's a
> different story and probably harder to fix.

The other ones seem to come from wt_status code, as part of
get_head_description().

---
diff --git a/ref-filter.c b/ref-filter.c
index 1fc5e9970..947919fc4 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1284,6 +1284,20 @@ static const char *get_refname(struct used_atom *atom, struct ref_array_item *re
 	return show_ref(&atom->u.refname, ref->refname);
 }
 
+static int head_matches(const char *refname)
+{
+	static int initialized;
+	static char *head;
+
+	if (!initialized) {
+		unsigned char sha1[20];
+		head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
+		initialized = 1;
+	}
+
+	return head && !strcmp(refname, head);
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -1369,12 +1383,7 @@ static void populate_value(struct ref_array_item *ref)
 		} else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
 			continue;
 		} else if (!strcmp(name, "HEAD")) {
-			const char *head;
-			unsigned char sha1[20];
-
-			head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
-						  sha1, NULL);
-			if (head && !strcmp(ref->refname, head))
+			if (head_matches(ref->refname))
 				v->s = "*";
 			else
 				v->s = " ";

  reply	other threads:[~2017-05-17 14:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-17 11:14 Performance regression in `git branch` due to ref-filter usage Michael Haggerty
2017-05-17 14:04 ` Jeff King [this message]
2017-05-19  6:12   ` [PATCH] ref-filter: resolve HEAD when parsing %(HEAD) atom Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20170517140417.kwzznw4su36k6pxv@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    --cc=mhagger@alum.mit.edu \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).