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

[PATCH 2/3] grep: simplify grep_oid and grep_file

From
Rasmus Villemoes <rv@rasmusvillemoes.dk>
Date
Feb 15, 2018, 21:56 UTC
Message-ID
<20180215215615.21208-3-rv@rasmusvillemoes.dk>
In-Reply-To
<20180215215615.21208-1-rv@rasmusvillemoes.dk>

In the NO_PTHREADS or !num_threads case, this doesn't change anything. In the threaded case, note that grep_source_init duplicates its third argument, so there is no need to keep [path]buf.buf alive across the call of add_work().

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 builtin/grep.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 4a4f15172..593f48d59 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -326,18 +326,17 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 	}
 
 	grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
+	strbuf_release(&pathbuf);
 
 #ifndef NO_PTHREADS
 	if (num_threads) {
 		add_work(opt, &gs);
-		strbuf_release(&pathbuf);
 		return 0;
 	} else
 #endif
 	{
 		int hit;
 
-		strbuf_release(&pathbuf);
 		hit = grep_source(opt, &gs);
 
 		grep_source_clear(&gs);
@@ -356,18 +355,17 @@ static int grep_file(struct grep_opt *opt, const char *filename)
 		strbuf_addstr(&buf, filename);
 
 	grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
+	strbuf_release(&buf);
 
 #ifndef NO_PTHREADS
 	if (num_threads) {
 		add_work(opt, &gs);
-		strbuf_release(&buf);
 		return 0;
 	} else
 #endif
 	{
 		int hit;
 
-		strbuf_release(&buf);
 		hit = grep_source(opt, &gs);
 
 		grep_source_clear(&gs);
-- 
2.15.1
Previous: Jeff KingNext: Brandon Williams
Message 7 of 12 in “a few grep patches”
  1. 0/3 a few grep patchesRasmus Villemoes, Feb 15, 2018
  2. 1/3 grep: move grep_source_init outside critical sectionRasmus Villemoes, Feb 15, 2018
  3. Jeff KingFeb 15, 2018
  4. Junio C HamanoFeb 16, 2018
  5. 3/3 grep: avoid one strdup() per fileRasmus Villemoes, Feb 15, 2018
  6. Jeff KingFeb 15, 2018
  7. 2/3 grep: simplify grep_oid and grep_fileRasmus Villemoes, Feb 15, 2018
  8. Brandon WilliamsFeb 15, 2018
  9. 0/2 two small grep patchesRasmus Villemoes, Feb 23, 2018
  10. 2/2 grep: simplify grep_oid and grep_fileRasmus Villemoes, Feb 23, 2018
  11. 1/2 grep: move grep_source_init outside critical sectionRasmus Villemoes, Feb 23, 2018
  12. Jeff KingFeb 23, 2018

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.