Commit f2b16adaedf4891924ea61e7f357b02c6cf42bdf

Stefan Sperling 2019-08-02T15:50:16

cache path length in struct got_pathlist_entry

diff --git a/got/got.c b/got/got.c
index 5356c71..f0d4319 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1193,7 +1193,7 @@ cmd_update(int argc, char *argv[])
 	if (branch_name) {
 		struct got_object_id *head_commit_id;
 		TAILQ_FOREACH(pe, &paths, entry) {
-			if (strlen(pe->path) == 0)
+			if (pe->path_len == 0)
 				continue;
 			error = got_error_msg(GOT_ERR_BAD_PATH,
 			    "switching between branches requires that "
diff --git a/include/got_path.h b/include/got_path.h
index 929be76..756faf8 100644
--- a/include/got_path.h
+++ b/include/got_path.h
@@ -63,6 +63,7 @@ int got_path_cmp(const char *, const char *, size_t, size_t);
 struct got_pathlist_entry {
 	TAILQ_ENTRY(got_pathlist_entry) entry;
 	const char *path;
+	size_t path_len;
 	void *data; /* data pointer provided to got_pathlist_insert() */
 };
 TAILQ_HEAD(got_pathlist_head, got_pathlist_entry);
diff --git a/lib/path.c b/lib/path.c
index 3605ef1..ad33152 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -221,6 +221,7 @@ got_pathlist_insert(struct got_pathlist_entry **inserted,
 	if (new == NULL)
 		return got_error_from_errno("malloc");
 	new->path = path;
+	new->path_len = strlen(path);
 	new->data = data;
 
 	/*
@@ -232,8 +233,8 @@ got_pathlist_insert(struct got_pathlist_entry **inserted,
 	 */
 	pe = TAILQ_LAST(pathlist, got_pathlist_head);
 	while (pe) {
-		int cmp = got_path_cmp(pe->path, path,
-		    strlen(pe->path), strlen(path));
+		int cmp = got_path_cmp(pe->path, new->path,
+		    pe->path_len, new->path_len);
 		if (cmp == 0) {
 			free(new); /* duplicate */
 			return NULL;
@@ -262,6 +263,7 @@ got_pathlist_append(struct got_pathlist_head *pathlist,
 	if (new == NULL)
 		return got_error_from_errno("malloc");
 	new->path = path;
+	new->path_len = strlen(path);
 	new->data = data;
 	TAILQ_INSERT_TAIL(pathlist, new, entry);
 	return NULL;
diff --git a/lib/worktree.c b/lib/worktree.c
index b23c7b5..fe6944a 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1849,7 +1849,7 @@ got_worktree_checkout_files(struct got_worktree *worktree,
 		bbc_arg.base_commit_id = worktree->base_commit_id;
 		bbc_arg.entry_name = tpd->entry_name;
 		bbc_arg.path = pe->path;
-		bbc_arg.path_len = strlen(pe->path);
+		bbc_arg.path_len = pe->path_len;
 		bbc_arg.progress_cb = progress_cb;
 		bbc_arg.progress_arg = progress_arg;
 		err = got_fileindex_for_each_entry_safe(fileindex,