git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Clemens Buchacher <drizzd@aon.at>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH] optimize path_relative()
Date: Fri, 4 Jun 2010 09:44:43 +0200	[thread overview]
Message-ID: <20100604074442.GA5117@localhost> (raw)
In-Reply-To: <7vocfr7oe6.fsf@alter.siamese.dyndns.org>

Avoid copying to strbuf in case a subset of the original string can
be returned.

Since the strbuf is no longer guaranteed to be updated, this
function is different from quote_path_relative(). To avoid
confusion, do not export it.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
On Thu, Jun 03, 2010 at 03:16:33PM -0700, Junio C Hamano wrote:
> Clemens Buchacher <drizzd@aon.at> writes:
>
> > +	strbuf_add(out, in, len);
> > +
> > +	return out->buf;
> > +}
> 
> Hmm...  I wonder if we really want to always make a copy of the string in
> the majority of the case where there is no need to add ../ and the path
> does not have any funny characters that needs quoting.

Right. Implemented below. I noticed a few more redundancies along
the way, so the following could be squashed into 1/2 "separate
quoting and relative path generation" or applied on top of the
existing two patches.

Please let me know if you'd rather have a resend.

Clemens

 quote.c |   72 ++++++++++++++++++++++++++++++++++++--------------------------
 quote.h |    3 --
 2 files changed, 42 insertions(+), 33 deletions(-)

diff --git a/quote.c b/quote.c
index 2ae2c1f..481541a 100644
--- a/quote.c
+++ b/quote.c
@@ -295,62 +295,74 @@ void write_name_quotedpfx(const char *pfx, size_t pfxlen,
 	fputc(terminator, fp);
 }
 
+static const char *path_relative(const char *in, int len,
+				 struct strbuf *sb, const char *prefix,
+				 int prefix_len);
+
 void write_name_quoted_relative(const char *name, size_t len,
 				const char *prefix, size_t prefix_len,
 				FILE *fp, int terminator)
 {
 	struct strbuf sb = STRBUF_INIT;
 
-	char *path = path_relative(name, len, &sb, prefix, prefix_len);
-	write_name_quoted(path, fp, terminator);
+	name = path_relative(name, len, &sb, prefix, prefix_len);
+	write_name_quoted(name, fp, terminator);
 
 	strbuf_release(&sb);
 }
 
-/* give path as relative to prefix */
-char *path_relative(const char *in, int len,
-		    struct strbuf *out, const char *prefix, int prefix_len)
+/*
+ * Give path as relative to prefix.
+ *
+ * The strbuf may or may not be used, so do not assume it contains the
+ * returned path.
+ */
+static const char *path_relative(const char *in, int len,
+				 struct strbuf *sb, const char *prefix,
+				 int prefix_len)
 {
+	int off, i;
+
 	if (len < 0)
 		len = strlen(in);
 	if (prefix && prefix_len < 0)
 		prefix_len = strlen(prefix);
 
-	strbuf_setlen(out, 0);
-	strbuf_grow(out, len);
+	off = 0;
+	i = 0;
+	while (i < prefix_len && i < len && prefix[i] == in[i]) {
+		if (prefix[i] == '/')
+			off = i + 1;
+		i++;
+	}
+	in += off;
+	len -= off;
 
-	if (prefix_len > 0) {
-		int off = 0, i = 0;
-		while (i < prefix_len && i < len && prefix[i] == in[i]) {
-			if (prefix[i] == '/')
-				off = i + 1;
-			i++;
-		}
-		in += off;
-		len -= off;
+	if (i == prefix_len)
+		return in;
 
-		while (i < prefix_len) {
-			if (prefix[i] == '/')
-				strbuf_addstr(out, "../");
-			i++;
-		}
+	strbuf_reset(sb);
+	strbuf_grow(sb, len);
+
+	while (i < prefix_len) {
+		if (prefix[i] == '/')
+			strbuf_addstr(sb, "../");
+		i++;
 	}
-	strbuf_add(out, in, len);
+	strbuf_add(sb, in, len);
 
-	return out->buf;
+	return sb->buf;
 }
 
 /* quote path as relative to the given prefix */
 char *quote_path_relative(const char *in, int len,
 			  struct strbuf *out, const char *prefix)
 {
-	char *rel;
-	size_t rel_len;
-
-	path_relative(in, len, out, prefix, -1);
-	rel = strbuf_detach(out, &rel_len);
-	quote_c_style_counted(rel, rel_len, out, NULL, 0);
-	free(rel);
+	struct strbuf sb = STRBUF_INIT;
+	const char *rel = path_relative(in, len, &sb, prefix, -1);
+	strbuf_reset(out);
+	quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
+	strbuf_release(&sb);
 
 	if (!out->len)
 		strbuf_addstr(out, "./");
diff --git a/quote.h b/quote.h
index e9e0221..38003bf 100644
--- a/quote.h
+++ b/quote.h
@@ -58,9 +58,6 @@ extern void write_name_quoted_relative(const char *name, size_t len,
 		const char *prefix, size_t prefix_len,
 		FILE *fp, int terminator);
 
-/* give path as relative to prefix */
-extern char *path_relative(const char *in, int len,
-		struct strbuf *out, const char *prefix, int prefix_len);
 /* quote path as relative to the given prefix */
 extern char *quote_path_relative(const char *in, int len,
 			  struct strbuf *out, const char *prefix);
-- 
1.7.1.2.ga1f6e

  reply	other threads:[~2010-06-04  7:45 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-02 23:36 What's cooking in git.git (Jun 2010, #01; Wed, 2) Junio C Hamano
2010-06-03  3:13 ` Tay Ray Chuan
2010-06-03  8:40 ` Michael J Gruber
2010-06-03 18:25   ` mg/rev-parse-option-sifter-deprecation Jonathan Nieder
2010-06-03 13:36 ` [PATCH 1/2] separate quoting and relative path generation Clemens Buchacher
2010-06-03 13:39   ` [PATCH 2/2] ls-files: allow relative pathspec Clemens Buchacher
2010-06-03 22:16   ` [PATCH 1/2] separate quoting and relative path generation Junio C Hamano
2010-06-04  7:44     ` Clemens Buchacher [this message]
2010-06-04  7:50       ` [PATCH] optimize path_relative() Clemens Buchacher
2010-06-05  8:04         ` [PATCH] setup: document prefix Clemens Buchacher
2010-06-05  7:37       ` [PATCH v2] optimize path_relative() Clemens Buchacher
2010-06-05 18:07         ` Junio C Hamano
2010-06-03 14:36 ` What's cooking in git.git (Jun 2010, #01; Wed, 2) Thomas Rast
2010-06-03 19:53 ` Eyvind Bernhardsen
2010-06-04 21:18 ` Jonathan Nieder
2010-06-05 18:07   ` Junio C Hamano
2010-06-05 19:32     ` Jonathan Nieder
2010-06-05 23:57     ` Sverre Rabbelier
2010-06-06  4:00       ` Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2010-05-24  9:47 [PATCH 0/3] commit: fix abbrev-sha regression Tay Ray Chuan
2010-05-24  9:47 ` [PATCH 1/3] t7502-commit: add tests for summary output Tay Ray Chuan
2010-05-24  9:47   ` [PATCH 2/3] t7502-commit: add summary output tests for empty and merge commits Tay Ray Chuan
2010-05-24  9:47     ` [PATCH 3/3] commit: show abbreviated sha for commits with empty diffs Tay Ray Chuan
2010-05-26  5:07       ` Junio C Hamano
2010-05-26  5:37         ` Tay Ray Chuan
2010-05-26  5:39           ` Tay Ray Chuan
2010-05-26  5:07     ` [PATCH 2/3] t7502-commit: add summary output tests for empty and merge commits Junio C Hamano
2010-05-26  5:19       ` Tay Ray Chuan
2010-05-27 15:34 ` [PATCH v2 0/3] commit: fix abbrev-sha regression Tay Ray Chuan
2010-05-27 15:34   ` [PATCH v2 1/3] t7502-commit: add tests for summary output Tay Ray Chuan
2010-05-27 15:34     ` [PATCH v2 2/3] t7502-commit: add summary output tests for empty and merge commits Tay Ray Chuan
2010-05-27 15:34       ` [PATCH v2 3/3] commit::print_summary(): set rev_info.always_show_header to 1 Tay Ray Chuan
2010-05-29  1:10         ` Junio C Hamano
2010-05-29  1:41           ` Tay Ray Chuan
2010-06-04  8:21         ` [PATCH v3 " Tay Ray Chuan
2010-06-04  8:34           ` Tay Ray Chuan
2010-06-05  6:44           ` Junio C Hamano
2010-06-07  5:04         ` [PATCH v4] " Tay Ray Chuan
2010-06-12 14:15         ` [PATCH v6 3/3] commit::print_summary(): don't use format_commit_message() Tay Ray Chuan
2010-05-27 16:58   ` [PATCH v2 0/3] commit: fix abbrev-sha regression Will Palmer

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=20100604074442.GA5117@localhost \
    --to=drizzd@aon.at \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).