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

[PATCH v4 5/9] create_directories(): remove some memcpy() and strchr() calls

From
KBKjetil Barvik <barvik@broadpark.no>
Date
Feb 9, 2009, 20:54 UTC
Message-ID
<03f0dcd2030c4f48a59ae98beda039bf1930aaf5.1234211595.git.barvik@broadpark.no>
In-Reply-To
<cover.1234211594.git.barvik@broadpark.no>

Remove the call to memcpy() and strchr() for each path component tested, and instead add each path component as we go forward inside the while-loop.

Impact: small optimisation
Signed-off-by: Kjetil Barvik <barvik@broadpark.no>
---
 entry.c |   23 ++++++++++++++---------
 1 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/entry.c b/entry.c
index bb6bdb9..cc8f0c6 100644
--- a/entry.c
+++ b/entry.c
@@ -2,15 +2,19 @@
 #include "blob.h"
 #include "dir.h"
 
-static void create_directories(const char *path, const struct checkout *state)
+static void create_directories(const char *path, int path_len,
+			       const struct checkout *state)
 {
-	int len = strlen(path);
-	char *buf = xmalloc(len + 1);
-	const char *slash = path;
-
-	while ((slash = strchr(slash+1, '/')) != NULL) {
-		len = slash - path;
-		memcpy(buf, path, len);
+	char *buf = xmalloc(path_len + 1);
+	int len = 0;
+
+	while (len < path_len) {
+		do {
+			buf[len] = path[len];
+			len++;
+		} while (len < path_len && path[len] != '/');
+		if (len >= path_len)
+			break;
 		buf[len] = 0;
 
 		/*
@@ -190,6 +194,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
 
 	memcpy(path, state->base_dir, len);
 	strcpy(path + len, ce->name);
+	len += ce_namelen(ce);
 
 	if (!lstat(path, &st)) {
 		unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
@@ -218,6 +223,6 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
 			return error("unable to unlink old '%s' (%s)", path, strerror(errno));
 	} else if (state->not_new)
 		return 0;
-	create_directories(path, state);
+	create_directories(path, len, state);
 	return write_entry(ce, path, state, 0);
 }
-- 
1.6.1.349.g99fa5
Previous: Kjetil BarvikNext: Kjetil Barvik
Message 6 of 10 in “git checkout: more cleanups, optimisation, less lstat() calls”
  1. 0/9 git checkout: more cleanups, optimisation, less lstat() callsKjetil Barvik, Feb 9, 2009
  2. 1/9 lstat_cache(): small cleanup and optimisationKjetil Barvik, Feb 9, 2009
  3. 2/9 lstat_cache(): generalise longest_match_lstat_cache()Kjetil Barvik, Feb 9, 2009
  4. 3/9 lstat_cache(): swap func(length, string) into func(string, length)Kjetil Barvik, Feb 9, 2009
  5. 4/9 unlink_entry(): introduce schedule_dir_for_removal()Kjetil Barvik, Feb 9, 2009
  6. 5/9 create_directories(): remove some memcpy() and strchr() callsKjetil Barvik, Feb 9, 2009
  7. 6/9 write_entry(): cleanup of some duplicated codeKjetil Barvik, Feb 9, 2009
  8. 7/9 write_entry(): use fstat() instead of lstat() when file is openKjetil Barvik, Feb 9, 2009
  9. 8/9 show_patch_diff(): remove a call to fstat()Kjetil Barvik, Feb 9, 2009
  10. 9/9 lstat_cache(): print a warning if doing ping-pong between cache typesKjetil Barvik, Feb 9, 2009

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.