Skip to content

Commit bec5ab8

Browse files
rscharfegitster
authored andcommitted
dir: avoid allocation in fill_directory()
Pass the match member of the first pathspec item directly to read_directory() instead of using common_prefix() to duplicate it first, thus avoiding memory duplication, strlen(3) and free(3). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6e3a7b3 commit bec5ab8

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

dir.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,19 @@ char *common_prefix(const struct pathspec *pathspec)
174174

175175
int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
176176
{
177-
char *prefix;
177+
const char *prefix;
178178
size_t prefix_len;
179179

180180
/*
181181
* Calculate common prefix for the pathspec, and
182182
* use that to optimize the directory walk
183183
*/
184-
prefix = common_prefix(pathspec);
185-
prefix_len = prefix ? strlen(prefix) : 0;
184+
prefix_len = common_prefix_len(pathspec);
185+
prefix = prefix_len ? pathspec->items[0].match : "";
186186

187187
/* Read the directory and prune it */
188188
read_directory(dir, prefix, prefix_len, pathspec);
189189

190-
free(prefix);
191190
return prefix_len;
192191
}
193192

0 commit comments

Comments
 (0)