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

[PATCH] dir: avoid allocation in fill_directory()

From
René Scharfe <l.s.r@web.de>
Date
Feb 7, 2017, 22:04 UTC
Message-ID
<73ec9cc7-8a86-8ba9-90fd-a954fa031820@web.de>

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>
---
This updates 966de3028 (dir: convert fill_directory to use the pathspec
struct interface).  The result is closer to the original, yet still
using the modern interface.
This patch eerily resembles 2b189435 (dir: simplify fill_directory()).
 dir.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/dir.c b/dir.c
index 65c3e681b8..4541f9e146 100644
--- a/dir.c
+++ b/dir.c
@@ -174,20 +174,19 @@ char *common_prefix(const struct pathspec *pathspec)
 
 int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
 {
-	char *prefix;
+	const char *prefix;
 	size_t prefix_len;
 
 	/*
 	 * Calculate common prefix for the pathspec, and
 	 * use that to optimize the directory walk
 	 */
-	prefix = common_prefix(pathspec);
-	prefix_len = prefix ? strlen(prefix) : 0;
+	prefix_len = common_prefix_len(pathspec);
+	prefix = prefix_len ? pathspec->items[0].match : "";
 
 	/* Read the directory and prune it */
 	read_directory(dir, prefix, prefix_len, pathspec);
 
-	free(prefix);
 	return prefix_len;
 }
 
-- 
2.11.1
Next: Duy Nguyen
Message 1 of 4 in “dir: avoid allocation in fill_directory()”
  1. dir: avoid allocation in fill_directory()René Scharfe, Feb 7, 2017
  2. Duy NguyenFeb 8, 2017
  3. Brandon WilliamsFeb 8, 2017
  4. René ScharfeFeb 10, 2017

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.