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

[PATCH 22/27] [GSOC] ref-filter: introduction ref_filter_slopbuf

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 13, 2021, 08:23 UTC
Message-ID
<b68c00dfa5de5fae77024f4c2949a36ae2c14abd.1628842990.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1016.git.1628842990.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

xstrdup("") calls strdup(""), which in glibc will call malloc(1), this means that there will have unwanted memory allocations.

Introduce the global one-byte array ref_filter_slopbuf, which learn from strbuf_slopbuf. let those places which need fill strdup("") now fill ref_filter_slopbuf. This can reduce some unnecessary memory allocation.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Hariom Verma <hariom18599@gmail.com>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 ref-filter.c | 53 ++++++++++++++++++++++++++--------------------------
 1 file changed, 27 insertions(+), 26 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 771804c06f0..5e00dbd993a 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -25,6 +25,8 @@
 #include "hashmap.h"
 #include "strvec.h"
 
+char ref_filter_slopbuf[1];
+
 static struct ref_msg {
 	const char *gone;
 	const char *ahead;
@@ -1225,7 +1227,7 @@ static const char *copy_name(const char *buf)
 		if (!strncmp(cp, " <", 2))
 			return xmemdupz(buf, cp - buf);
 	}
-	return xstrdup("");
+	return ref_filter_slopbuf;
 }
 
 static const char *copy_email(const char *buf, struct used_atom *atom)
@@ -1233,7 +1235,7 @@ static const char *copy_email(const char *buf, struct used_atom *atom)
 	const char *email = strchr(buf, '<');
 	const char *eoemail;
 	if (!email)
-		return xstrdup("");
+		return ref_filter_slopbuf;
 	switch (atom->u.email_option.option) {
 	case EO_RAW:
 		eoemail = strchr(email, '>');
@@ -1255,7 +1257,7 @@ static const char *copy_email(const char *buf, struct used_atom *atom)
 	}
 
 	if (!eoemail)
-		return xstrdup("");
+		return ref_filter_slopbuf;
 	return xmemdupz(email, eoemail - email);
 }
 
@@ -1309,7 +1311,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
 	v->value = timestamp;
 	return;
  bad:
-	v->s = xstrdup("");
+	v->s = ref_filter_slopbuf;
 	v->value = 0;
 }
 
@@ -1519,7 +1521,7 @@ static void fill_missing_values(struct atom_value *val)
 	for (i = 0; i < used_atom_cnt; i++) {
 		struct atom_value *v = &val[i];
 		if (v->s == NULL)
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 	}
 }
 
@@ -1594,7 +1596,7 @@ static const char *lstrip_ref_components(const char *refname, int len)
 		switch (*start++) {
 		case '\0':
 			free((char *)to_free);
-			return xstrdup("");
+			return ref_filter_slopbuf;
 		case '/':
 			remaining--;
 			break;
@@ -1632,7 +1634,7 @@ static const char *rstrip_ref_components(const char *refname, int len)
 		char *p = strrchr(start, '/');
 		if (p == NULL) {
 			free((char *)to_free);
-			return xstrdup("");
+			return ref_filter_slopbuf;
 		} else
 			p[0] = '\0';
 	}
@@ -1663,7 +1665,7 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
 				       AHEAD_BEHIND_FULL) < 0) {
 			v->s = xstrdup(msgs.gone);
 		} else if (!num_ours && !num_theirs)
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 		else if (!num_ours)
 			v->s = xstrfmt_len(&v->s_size, msgs.behind, num_theirs);
 		else if (!num_theirs)
@@ -1680,7 +1682,7 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
 		if (stat_tracking_info(branch, &num_ours, &num_theirs,
 				       NULL, atom->u.remote_ref.push,
 				       AHEAD_BEHIND_FULL) < 0) {
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			return;
 		}
 		if (!num_ours && !num_theirs)
@@ -1739,7 +1741,7 @@ char *get_head_description(void)
 static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
 {
 	if (!ref->symref)
-		return xstrdup("");
+		return ref_filter_slopbuf;
 	else
 		return show_ref(&atom->u.refname, ref->symref);
 }
@@ -1861,7 +1863,7 @@ static char *get_worktree_path(const struct used_atom *atom, const struct ref_ar
 	e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
 
 	if (!e)
-		return xstrdup("");
+		return ref_filter_slopbuf;
 
 	lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
 
@@ -1884,7 +1886,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 		ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
 					     NULL, NULL);
 		if (!ref->symref)
-			ref->symref = xstrdup("");
+			ref->symref = ref_filter_slopbuf;
 	}
 
 	/* Fill in specials first */
@@ -1912,7 +1914,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			if (ref->kind == FILTER_REFS_BRANCHES)
 				v->s = get_worktree_path(atom, ref);
 			else
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 			continue;
 		}
 		else if (atom_type == ATOM_SYMREF)
@@ -1922,7 +1924,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			/* only local branches may have an upstream */
 			if (!skip_prefix(ref->refname, "refs/heads/",
 					 &branch_name)) {
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 				continue;
 			}
 			branch = branch_get(branch_name);
@@ -1931,11 +1933,11 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			if (refname)
 				fill_remote_ref_details(atom, refname, branch, v);
 			else
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 			continue;
 		} else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) {
 			const char *branch_name;
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			if (!skip_prefix(ref->refname, "refs/heads/",
 					 &branch_name))
 				continue;
@@ -1948,8 +1950,6 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 				if (!refname)
 					continue;
 			}
-			/* We will definitely re-init v->s on the next line. */
-			free((char *)v->s);
 			fill_remote_ref_details(atom, refname, branch, v);
 			continue;
 		} else if (atom_type == ATOM_COLOR) {
@@ -1962,7 +1962,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			if (ref->flag & REF_ISPACKED)
 				cp = copy_advance(cp, ",packed");
 			if (cp == buf)
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 			else {
 				*cp = '\0';
 				v->s = xstrdup(buf + 1);
@@ -1979,33 +1979,33 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			continue;
 		} else if (atom_type == ATOM_ALIGN) {
 			v->handler = align_atom_handler;
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			continue;
 		} else if (atom_type == ATOM_END) {
 			v->handler = end_atom_handler;
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			continue;
 		} else if (atom_type == ATOM_IF) {
 			const char *s;
 			if (skip_prefix(name, "if:", &s))
 				v->s = xstrdup(s);
 			else
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 			v->handler = if_atom_handler;
 			continue;
 		} else if (atom_type == ATOM_THEN) {
 			v->handler = then_atom_handler;
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			continue;
 		} else if (atom_type == ATOM_ELSE) {
 			v->handler = else_atom_handler;
-			v->s = xstrdup("");
+			v->s = ref_filter_slopbuf;
 			continue;
 		} else if (atom_type == ATOM_REST) {
 			if (ref->rest)
 				v->s = xstrdup(ref->rest);
 			else
-				v->s = xstrdup("");
+				v->s = ref_filter_slopbuf;
 			continue;
 		} else
 			continue;
@@ -2356,7 +2356,8 @@ void free_ref_array_item_value(struct ref_array_item *item)
 	if (item->value) {
 		int i;
 		for (i = 0; i < used_atom_cnt; i++)
-			free((char *)item->value[i].s);
+			if (item->value[i].s != ref_filter_slopbuf)
+				free((char *)item->value[i].s);
 		free(item->value);
 	}
 }
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: ZheNing Hu via GitGitGadget
Message 23 of 28 in “[GSOC] [RFC] cat-file: reuse ref-filter logic”
  1. 00/27 [GSOC] [RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Aug 13, 2021
  2. 02/27 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Aug 13, 2021
  3. 01/27 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Aug 13, 2021
  4. 03/27 [GSOC] ref-filter: --format=%(raw) support --perlZheNing Hu via GitGitGadget, Aug 13, 2021
  5. 04/27 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Aug 13, 2021
  6. 07/27 [GSOC] ref-filter: introduce free_ref_array_item_value() functionZheNing Hu via GitGitGadget, Aug 13, 2021
  7. 05/27 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Aug 13, 2021
  8. 06/27 [GSOC] ref-filter: pass get_object() return value to their callersZheNing Hu via GitGitGadget, Aug 13, 2021
  9. 08/27 [GSOC] ref-filter: add cat_file_mode to ref_formatZheNing Hu via GitGitGadget, Aug 13, 2021
  10. 10/27 [GSOC] cat-file: add has_object_file() checkZheNing Hu via GitGitGadget, Aug 13, 2021
  11. 09/27 [GSOC] ref-filter: modify the error message and value in get_objectZheNing Hu via GitGitGadget, Aug 13, 2021
  12. 11/27 [GSOC] cat-file: change batch_objects parameter nameZheNing Hu via GitGitGadget, Aug 13, 2021
  13. 12/27 [GSOC] cat-file: create p1006-cat-file.shZheNing Hu via GitGitGadget, Aug 13, 2021
  14. 13/27 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Aug 13, 2021
  15. 14/27 [GSOC] cat-file: reuse err buf in batch_object_write()ZheNing Hu via GitGitGadget, Aug 13, 2021
  16. 15/27 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Aug 13, 2021
  17. 16/27 [GSOC] ref-filter: remove grab_oid() functionZheNing Hu via GitGitGadget, Aug 13, 2021
  18. 17/27 [GSOC] ref-filter: performance optimization by skip parse_object_bufferZheNing Hu via GitGitGadget, Aug 13, 2021
  19. 18/27 [GSOC] ref-filter: use atom_type and merge two for loop in grab_personZheNing Hu via GitGitGadget, Aug 13, 2021
  20. 19/27 [GSOC] ref-filter: remove strlen from find_subposZheNing Hu via GitGitGadget, Aug 13, 2021
  21. 20/27 [GSOC] ref-filter: introducing xstrvfmt_len() and xstrfmt_len()ZheNing Hu via GitGitGadget, Aug 13, 2021
  22. 21/27 [GSOC] ref-filter: remove second parsing in format_ref_array_itemZheNing Hu via GitGitGadget, Aug 13, 2021
  23. 22/27 [GSOC] ref-filter: introduction ref_filter_slopbufZheNing Hu via GitGitGadget, Aug 13, 2021
  24. 23/27 [GSOC] ref-filter: add deref member to struct used_atomZheNing Hu via GitGitGadget, Aug 13, 2021
  25. 24/27 [GSOC] ref-filter: introduce symref_atom_parser()ZheNing Hu via GitGitGadget, Aug 13, 2021
  26. 25/27 [GSOC] ref-filter: use switch case instread of if elseZheNing Hu via GitGitGadget, Aug 13, 2021
  27. 27/27 [GSOC] ref-filter: add need_get_object_info flag to struct expand_dataZheNing Hu via GitGitGadget, Aug 13, 2021
  28. 26/27 [GSOC] ref-filter: reuse finnal buffer if no stack needZheNing Hu via GitGitGadget, Aug 13, 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.