git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/6] shortlog: optimize out useless "<none>" normalization

From
Jeff King <peff@peff.net>
Date
Jan 15, 2016, 17:10 UTC
Message-ID
<20160115171006.GE21102@sigill.intra.peff.net>
In-Reply-To
<20160115170627.GA20983@sigill.intra.peff.net>

If we are in --summary mode, we will always pass <none> to insert_one_record, which will then do some normalization (e.g., cutting out "[PATCH]"). There's no point in doing so if we aren't going to use the result anyway.

This drops my best-of-five for "git shortlog -ns HEAD" on linux.git from:

  real    0m5.257s
  user    0m5.104s
  sys     0m0.156s
to:
  real    0m5.194s
  user    0m5.028s
  sys     0m0.168s

That's only 1%, but arguably the result is clearer to read, as we're able to group our variable declarations inside the conditional block. It also opens up further optimization possibilities for future patches.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/shortlog.c | 63 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 29 deletions(-)
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 3e1e0d6..c2bf9b2 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -31,13 +31,9 @@ static void insert_one_record(struct shortlog *log,
 			      const char *author,
 			      const char *oneline)
 {
-	const char *dot3 = log->common_repo_prefix;
-	char *buffer, *p;
 	struct string_list_item *item;
 	const char *mailbuf, *namebuf;
 	size_t namelen, maillen;
-	const char *eol;
-	struct strbuf subject = STRBUF_INIT;
 	struct strbuf namemailbuf = STRBUF_INIT;
 	struct ident_split ident;
 
@@ -59,34 +55,43 @@ static void insert_one_record(struct shortlog *log,
 	if (item->util == NULL)
 		item->util = xcalloc(1, sizeof(struct string_list));
 
-	/* Skip any leading whitespace, including any blank lines. */
-	while (*oneline && isspace(*oneline))
-		oneline++;
-	eol = strchr(oneline, '\n');
-	if (!eol)
-		eol = oneline + strlen(oneline);
-	if (starts_with(oneline, "[PATCH")) {
-		char *eob = strchr(oneline, ']');
-		if (eob && (!eol || eob < eol))
-			oneline = eob + 1;
-	}
-	while (*oneline && isspace(*oneline) && *oneline != '\n')
-		oneline++;
-	format_subject(&subject, oneline, " ");
-	buffer = strbuf_detach(&subject, NULL);
-
-	if (dot3) {
-		int dot3len = strlen(dot3);
-		if (dot3len > 5) {
-			while ((p = strstr(buffer, dot3)) != NULL) {
-				int taillen = strlen(p) - dot3len;
-				memcpy(p, "/.../", 5);
-				memmove(p + 5, p + dot3len, taillen + 1);
+	if (log->summary)
+		string_list_append(item->util, xstrdup(""));
+	else {
+		const char *dot3 = log->common_repo_prefix;
+		char *buffer, *p;
+		struct strbuf subject = STRBUF_INIT;
+		const char *eol;
+
+		/* Skip any leading whitespace, including any blank lines. */
+		while (*oneline && isspace(*oneline))
+			oneline++;
+		eol = strchr(oneline, '\n');
+		if (!eol)
+			eol = oneline + strlen(oneline);
+		if (starts_with(oneline, "[PATCH")) {
+			char *eob = strchr(oneline, ']');
+			if (eob && (!eol || eob < eol))
+				oneline = eob + 1;
+		}
+		while (*oneline && isspace(*oneline) && *oneline != '\n')
+			oneline++;
+		format_subject(&subject, oneline, " ");
+		buffer = strbuf_detach(&subject, NULL);
+
+		if (dot3) {
+			int dot3len = strlen(dot3);
+			if (dot3len > 5) {
+				while ((p = strstr(buffer, dot3)) != NULL) {
+					int taillen = strlen(p) - dot3len;
+					memcpy(p, "/.../", 5);
+					memmove(p + 5, p + dot3len, taillen + 1);
+				}
 			}
 		}
-	}
 
-	string_list_append(item->util, buffer);
+		string_list_append(item->util, buffer);
+	}
 }
 
 /*
-- 
2.7.0.244.g0701a9d
Previous: Jeff KingNext: Jeff King
Message 20 of 22 in “shortlog fixes and optimizations”
  1. 0/6 shortlog fixes and optimizationsJeff King, Jan 15, 2016
  2. 1/6 shortlog: match both "Author:" and "author" on stdinJeff King, Jan 15, 2016
  3. Eric SunshineJan 15, 2016
  4. Jeff KingJan 18, 2016
  5. Jeff KingJan 18, 2016
  6. Junio C HamanoJan 18, 2016
  7. 0/6 shortlog fixes and optimizationsJeff King, Jan 18, 2016
  8. 1/6 shortlog: match both "Author:" and "author" on stdinJeff King, Jan 18, 2016
  9. 2/6 shortlog: use strbufs to read from stdinJeff King, Jan 18, 2016
  10. 3/6 shortlog: replace hand-parsing of author with pretty-printerJeff King, Jan 18, 2016
  11. Jeff KingJan 18, 2016
  12. Jeff KingJan 18, 2016
  13. Junio C HamanoJan 19, 2016
  14. 4/6 shortlog: optimize "--summary" modeJeff King, Jan 18, 2016
  15. 5/6 shortlog: optimize out useless "<none>" normalizationJeff King, Jan 18, 2016
  16. 6/6 shortlog: optimize out useless string listJeff King, Jan 18, 2016
  17. 2/6 shortlog: use strbufs to read from stdinJeff King, Jan 15, 2016
  18. 3/6 shortlog: replace hand-parsing of author with pretty-printerJeff King, Jan 15, 2016
  19. 4/6 shortlog: optimize "--summary" modeJeff King, Jan 15, 2016
  20. 5/6 shortlog: optimize out useless "<none>" normalizationJeff King, Jan 15, 2016
  21. 6/6 shortlog: optimize out useless string listJeff King, Jan 15, 2016
  22. Junio C HamanoJan 15, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.