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

[PATCH] tree_entry_interesting: inline strncmp()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Apr 4, 2011, 14:46 UTC
Message-ID
<1301928386-25038-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<1301535481-1085-3-git-send-email-dpmcgee@gmail.com>

strncmp() is the function that takes most of the time inside tree_entry_interesting(). Inline it so we can shave some seconds out of function call time.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Turns out simplicity is the best. My straight copy of strncmp from
 glibc performed worse.
 With this I get a slightly better performance than Dan's 3/5:
 81.07-82.27 secs versus 82.02-82.92 (no other patches are applied).
 But I'm happy even if it gives the same or slightly worse
 performance because this applies to more cases than flat top tree
 case.
 Dan, match_dir_prefix() can also use some reordering to avoid
 strncmp(). But I suppose it won't give much gain on packages.git
 tree-walk.c |   28 ++++++++++++++++++++++++----
 1 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/tree-walk.c b/tree-walk.c
index 322becc..80bfc3a 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -457,6 +457,26 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
 	return retval;
 }
 
+/* Static version of strncmp to reduce function call cost */
+static inline int strncmp_1(const char *s1, const char *s2, size_t n)
+{
+	unsigned char c1 = '\0';
+	unsigned char c2 = '\0';
+
+	if (!n)
+		return 0;
+
+	while (n > 0) {
+		c1 = (unsigned char) *s1++;
+		c2 = (unsigned char) *s2++;
+		if (c1 == '\0' || c1 != c2)
+			return c1 - c2;
+		n--;
+	}
+
+	return c1 - c2;
+}
+
 static int match_entry(const struct name_entry *entry, int pathlen,
 		       const char *match, int matchlen,
 		       int *never_interesting)
@@ -473,7 +493,7 @@ static int match_entry(const struct name_entry *entry, int pathlen,
 		 * Does match sort strictly earlier than path
 		 * with their common parts?
 		 */
-		m = strncmp(match, entry->path,
+		m = strncmp_1(match, entry->path,
 			    (matchlen < pathlen) ? matchlen : pathlen);
 		if (m < 0)
 			return 0;
@@ -509,7 +529,7 @@ static int match_entry(const struct name_entry *entry, int pathlen,
 		 * we cheated and did not do strncmp(), so we do
 		 * that here.
 		 */
-		m = strncmp(match, entry->path, pathlen);
+		m = strncmp_1(match, entry->path, pathlen);
 
 	/*
 	 * If common part matched earlier then it is a hit,
@@ -525,7 +545,7 @@ static int match_entry(const struct name_entry *entry, int pathlen,
 static int match_dir_prefix(const char *base, int baselen,
 			    const char *match, int matchlen)
 {
-	if (strncmp(base, match, matchlen))
+	if (strncmp_1(base, match, matchlen))
 		return 0;
 
 	/*
@@ -592,7 +612,7 @@ int tree_entry_interesting(const struct name_entry *entry,
 		}
 
 		/* Does the base match? */
-		if (!strncmp(base_str, match, baselen)) {
+		if (!strncmp_1(base_str, match, baselen)) {
 			if (match_entry(entry, pathlen,
 					match + baselen, matchlen - baselen,
 					&never_interesting))
-- 
1.7.4.74.g639db
Previous: Dan McGeeNext: Dan McGee
Message 8 of 30 in “diff_tree_sha1: skip diff_tree if old == new”
  1. 1/5 diff_tree_sha1: skip diff_tree if old == newDan McGee, Mar 31, 2011
  2. 2/5 tree-walk: drop unused parameter from match_dir_prefixDan McGee, Mar 31, 2011
  3. Dan McGeeAug 30, 2011
  4. 3/5 tree-walk: micro-optimization in tree_entry_interestingDan McGee, Mar 31, 2011
  5. Nguyen Thai Ngoc DuyApr 3, 2011
  6. Junio C HamanoApr 3, 2011
  7. Dan McGeeApr 5, 2011
  8. tree_entry_interesting: inline strncmp()Nguyễn Thái Ngọc Duy, Apr 4, 2011
  9. 4/5 tree-walk: unroll get_mode since loop boundaries are well-knownDan McGee, Mar 31, 2011
  10. Nguyen Thai Ngoc DuyApr 2, 2011
  11. Dan McGeeApr 2, 2011
  12. Nguyen Thai Ngoc DuyApr 3, 2011
  13. Erik Faye-LundApr 4, 2011
  14. Andreas EricssonApr 4, 2011
  15. Junio C HamanoApr 4, 2011
  16. Dan McGeeApr 5, 2011
  17. Antriksh PanyApr 5, 2011
  18. Dan McGeeApr 6, 2011
  19. 5/5 tree-walk: match_entry microoptimizationDan McGee, Mar 31, 2011
  20. Nguyen Thai Ngoc DuyApr 2, 2011
  21. Dan McGeeApr 2, 2011
  22. Nguyen Thai Ngoc DuyMar 31, 2011
  23. Dan McGeeMar 31, 2011
  24. Junio C HamanoApr 1, 2011
  25. Nguyen Thai Ngoc DuyMay 3, 2011
  26. Fwd: [PATCH 1/5] diff_tree_sha1: skip diff_tree if old == newDan McGee, Apr 2, 2011
  27. Dan McGeeAug 30, 2011
  28. Junio C HamanoAug 30, 2011
  29. 1/2 tree-walk: drop unused parameter from match_dir_prefixDan McGee, Sep 9, 2011
  30. 2/2 tree-walk: micro-optimization in tree_entry_interestingDan McGee, Sep 9, 2011

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.