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

[PATCH 3/3] refs: use strings directly in find_containing_dir()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
May 22, 2012, 18:50 UTC
Message-ID
<4FBBE012.6090702@lsrfire.ath.cx>
In-Reply-To
<1337692566-3718-1-git-send-email-mhagger@alum.mit.edu>

Convert the parameter subdirname of search_for_subdir() to a length-limted string and then simply pass the interesting slice of the refname from find_containing_dir(), thereby avoiding to duplicate the string.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
Why allocate a NUL-terminated copy at all when we can teach the code to
stop after a given number of characters just as easily?  Alas, this
will still trigger an allocation in search_ref_dir() (see first patch).
 refs.c |   13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/refs.c b/refs.c
index c5e167b..96e943c 100644
--- a/refs.c
+++ b/refs.c
@@ -352,9 +352,9 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir,
  * directory cannot be found.  dir must already be complete.
  */
 static struct ref_dir *search_for_subdir(struct ref_dir *dir,
-					 const char *subdirname, int mkdir)
+					 const char *subdirname, size_t len,
+					 int mkdir)
 {
-	size_t len = strlen(subdirname);
 	struct ref_entry *entry = search_ref_dir(dir, subdirname, len);
 	if (!entry) {
 		if (!mkdir)
@@ -383,15 +383,11 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
 static struct ref_dir *find_containing_dir(struct ref_dir *dir,
 					   const char *refname, int mkdir)
 {
-	struct strbuf dirname;
 	const char *slash;
-	strbuf_init(&dirname, PATH_MAX);
 	for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
+		size_t dirnamelen = slash - refname + 1;
 		struct ref_dir *subdir;
-		strbuf_add(&dirname,
-			   refname + dirname.len,
-			   (slash + 1) - (refname + dirname.len));
-		subdir = search_for_subdir(dir, dirname.buf, mkdir);
+		subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
 		if (!subdir) {
 			dir = NULL;
 			break;
@@ -399,7 +395,6 @@ static struct ref_dir *find_containing_dir(struct ref_dir *dir,
 		dir = subdir;
 	}
 
-	strbuf_release(&dirname);
 	return dir;
 }
 
-- 
1.7.10.2
Previous: René ScharfeNext: Junio C Hamano
Message 5 of 12 in “find_containing_dir(): allocate strbuf less extravagantly”
  1. find_containing_dir(): allocate strbuf less extravagantlymhagger@alum.mit.edu, May 22, 2012
  2. Jeff KingMay 22, 2012
  3. 1/3 refs: convert parameter of search_ref_dir() to length-limited stringRené Scharfe, May 22, 2012
  4. 2/3 refs: convert parameter of create_dir_entry() to length-limited stringRené Scharfe, May 22, 2012
  5. 3/3 refs: use strings directly in find_containing_dir()René Scharfe, May 22, 2012
  6. Junio C HamanoMay 22, 2012
  7. René ScharfeMay 22, 2012
  8. Junio C HamanoMay 22, 2012
  9. René ScharfeMay 23, 2012
  10. Junio C HamanoMay 23, 2012
  11. René ScharfeMay 23, 2012
  12. Michael HaggertyMay 24, 2012

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.