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>

René Scharfe committed Feb 7, 2017 at 23:04 UTC bec5ab8997c2391fa2241520f52f301397ebd538
1 file changed +3 -4
dir.c
+3 -4
@@ -174,20 +174,19 @@ char *common_prefix(const struct pathspec *pathspec)
174
175 int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
176 {
177 - char *prefix;
177 + const char *prefix;
178 size_t prefix_len;
179
180 /*
181 * Calculate common prefix for the pathspec, and
182 * use that to optimize the directory walk
183 */
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 : "";
186
187 /* Read the directory and prune it */
188 read_directory(dir, prefix, prefix_len, pathspec);
189
190 - free(prefix);
190 return prefix_len;
191 }
192