Skip to content

Commit 81a9aa6

Browse files
Kjetil Barvikgitster
authored andcommitted
create_directories(): remove some memcpy() and strchr() calls
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7847892 commit 81a9aa6

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

entry.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
#include "blob.h"
33
#include "dir.h"
44

5-
static void create_directories(const char *path, const struct checkout *state)
5+
static void create_directories(const char *path, int path_len,
6+
const struct checkout *state)
67
{
7-
int len = strlen(path);
8-
char *buf = xmalloc(len + 1);
9-
const char *slash = path;
10-
11-
while ((slash = strchr(slash+1, '/')) != NULL) {
12-
len = slash - path;
13-
memcpy(buf, path, len);
8+
char *buf = xmalloc(path_len + 1);
9+
int len = 0;
10+
11+
while (len < path_len) {
12+
do {
13+
buf[len] = path[len];
14+
len++;
15+
} while (len < path_len && path[len] != '/');
16+
if (len >= path_len)
17+
break;
1418
buf[len] = 0;
1519

1620
/*
@@ -190,6 +194,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
190194

191195
memcpy(path, state->base_dir, len);
192196
strcpy(path + len, ce->name);
197+
len += ce_namelen(ce);
193198

194199
if (!lstat(path, &st)) {
195200
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
218223
return error("unable to unlink old '%s' (%s)", path, strerror(errno));
219224
} else if (state->not_new)
220225
return 0;
221-
create_directories(path, state);
226+
create_directories(path, len, state);
222227
return write_entry(ce, path, state, 0);
223228
}

0 commit comments

Comments
 (0)