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

[PATCH 3/8] [GSOC] ref-filter: remove strlen from find_subpos

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 17, 2021, 08:41 UTC
Message-ID
<c3a4c858df0d246e06c3e770c5eb2956eeb96538.1629189701.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1021.git.1629189701.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

find_subpos() use strlen() to get the length of the object content, but in the caller of find_subpos(), grab_sub_body_contents(), we already get the length of the object content through data->size. So add a `size_t buf_size` in find_subpos(), and pass data->size to it. This can avoid unnecessary strlen() overhead.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Hariom Verma <hariom18599@gmail.com>
---
 ref-filter.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 592b8d9bd0a..875c2e4c39c 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1314,7 +1314,7 @@ static void grab_person(enum atom_type type, struct atom_value *val, int deref,
 	}
 }
 
-static void find_subpos(const char *buf,
+static void find_subpos(const char *buf, size_t buf_size,
 			const char **sub, size_t *sublen,
 			const char **body, size_t *bodylen,
 			size_t *nonsiglen,
@@ -1322,12 +1322,12 @@ static void find_subpos(const char *buf,
 {
 	struct strbuf payload = STRBUF_INIT;
 	struct strbuf signature = STRBUF_INIT;
+	const char *begin = buf;
 	const char *eol;
-	const char *end = buf + strlen(buf);
 	const char *sigstart;
 
 	/* parse signature first; we might not even have a subject line */
-	parse_signature(buf, end - buf, &payload, &signature);
+	parse_signature(buf, buf_size, &payload, &signature);
 
 	/* skip past header until we hit empty line */
 	while (*buf && *buf != '\n') {
@@ -1340,7 +1340,7 @@ static void find_subpos(const char *buf,
 	while (*buf == '\n')
 		buf++;
 	*sig = strbuf_detach(&signature, siglen);
-	sigstart = buf + parse_signed_buffer(buf, strlen(buf));
+	sigstart = buf + parse_signed_buffer(buf, buf_size - (buf - begin));
 
 	/* subject is first non-empty line */
 	*sub = buf;
@@ -1363,7 +1363,7 @@ static void find_subpos(const char *buf,
 	while (*buf == '\n' || *buf == '\r')
 		buf++;
 	*body = buf;
-	*bodylen = strlen(buf);
+	*bodylen = buf_size - (buf - begin);
 	*nonsiglen = sigstart - buf;
 }
 
@@ -1430,7 +1430,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp
 		     !starts_with(name, "contents")))
 			continue;
 		if (!subpos)
-			find_subpos(buf,
+			find_subpos(buf, data->size,
 				    &subpos, &sublen,
 				    &bodypos, &bodylen, &nonsiglen,
 				    &sigpos, &siglen);
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: ZheNing Hu via GitGitGadget
Message 4 of 9 in “[GSOC] [RFC] ref-filter: code logic optimization”
  1. 0/8 [GSOC] [RFC] ref-filter: code logic optimizationZheNing Hu via GitGitGadget, Aug 17, 2021
  2. 1/8 [GSOC] ref-filter: remove grab_oid() functionZheNing Hu via GitGitGadget, Aug 17, 2021
  3. 2/8 [GSOC] ref-filter: merge two for loop in grab_personZheNing Hu via GitGitGadget, Aug 17, 2021
  4. 3/8 [GSOC] ref-filter: remove strlen from find_subposZheNing Hu via GitGitGadget, Aug 17, 2021
  5. 4/8 [GSOC] ref-filter: introducing xstrvfmt_len() and xstrfmt_len()ZheNing Hu via GitGitGadget, Aug 17, 2021
  6. 5/8 [GSOC] ref-filter: introduction ref_filter_slopbuf[1]ZheNing Hu via GitGitGadget, Aug 17, 2021
  7. 6/8 [GSOC] ref-filter: add deref member to struct used_atomZheNing Hu via GitGitGadget, Aug 17, 2021
  8. 7/8 [GSOC] ref-filter: introduce symref_atom_parser()ZheNing Hu via GitGitGadget, Aug 17, 2021
  9. 8/8 [GSOC] ref-filter: use switch/case instead of if/elseZheNing Hu via GitGitGadget, Aug 17, 2021

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.