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

Re: [PATCH 3/3] pretty=format: Avoid some expensive calculations when not needed

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Nov 5, 2007, 23:53 UTC
Message-ID
<Pine.LNX.4.64.0711052348390.4362@racer.site>
In-Reply-To
<472F7B2F.4050608@lsrfire.ath.cx>
Hi,
On Mon, 5 Nov 2007, Ren? Scharfe wrote:
Show 37 quoted lines
> Junio C Hamano schrieb:
> > Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> > 
> >> Unfortunately, we cannot reuse the result of that function, which
> >> would be cleaner: there are more users than just git log.  Most
> >> notably, git-archive with "$Format:...$" substitution.
> > 
> > That makes sense.
> > 
> > 
> >> diff --git a/pretty.c b/pretty.c
> >> index 490cede..241e91c 100644
> >> --- a/pretty.c
> >> +++ b/pretty.c
> >> @@ -393,6 +393,7 @@ void format_commit_message(const struct commit *commit,
> >>  	int i;
> >>  	enum { HEADER, SUBJECT, BODY } state;
> >>  	const char *msg = commit->buffer;
> >> +	char *active = interp_find_active(format, table, ARRAY_SIZE(table));
> >> ...
> >> +	if (active[IHASH])
> >> +		interp_set_entry(table, IHASH,
> >> +				sha1_to_hex(commit->object.sha1));
> >> +	if (active[IHASH_ABBREV])
> >> +		interp_set_entry(table, IHASH_ABBREV,
> >>  			find_unique_abbrev(commit->object.sha1,
> >>  				DEFAULT_ABBREV));
> > 
> > Instead of allocating a separate array and freeing at the end,
> > wouldn't it make more sense to have a bitfield that records what
> > is used by the format string inside the array elements?
> 
> How about (ab)using the value field?  Let interp_find_active() mark
> unneeded entries with NULL, and the rest with some cookie.  All table
> entries with non-NULL values need to be initialized.  interp_set_entry()
> needs to be aware of this cookie, as it mustn't free() it.  The cookie
> could be the address of a static char* in interpolate.c.

Yeah, something like this on top of my earlier patch (and obviously the corresponding change from "if (active[IHASH])" to "if (table[IHASH].value)"):

---
 interpolate.c |   10 ++++------
 interpolate.h |    2 +-
 2 files changed, 5 insertions(+), 7 deletions(-)
diff --git a/interpolate.c b/interpolate.c
index 80eeb36..05a22e1 100644
--- a/interpolate.c
+++ b/interpolate.c
@@ -5,13 +5,14 @@
 #include "git-compat-util.h"
 #include "interpolate.h"
 
+static const char *empty_value = "";
 
 void interp_set_entry(struct interp *table, int slot, const char *value)
 {
 	char *oldval = table[slot].value;
 	char *newval = NULL;
 
-	if (oldval)
+	if (oldval && oldval != empty_value)
 		free(oldval);
 
 	if (value)
@@ -103,10 +104,9 @@ unsigned long interpolate(char *result, unsigned long reslen,
 	return newlen;
 }
 
-char *interp_find_active(const char *orig,
+void interp_find_active(const char *orig,
 		const struct interp *interps, int ninterps)
 {
-	char *result = xcalloc(1, ninterps);
 	char c;
 	int i;
 
@@ -115,10 +115,8 @@ char *interp_find_active(const char *orig,
 			/* Try to match an interpolation string. */
 			for (i = 0; i < ninterps; i++)
 				if (!prefixcmp(orig, interps[i].name + 1)) {
-					result[i] = 1;
+					interps[i].value = empty_value;
 					orig += strlen(interps[i].name + 1);
 					break;
 				}
-
-	return result;
 }
diff --git a/interpolate.h b/interpolate.h
index 2d197c5..19b7ebe 100644
--- a/interpolate.h
+++ b/interpolate.h
@@ -22,7 +22,7 @@ extern void interp_clear_table(struct interp *table, int ninterps);
 extern unsigned long interpolate(char *result, unsigned long reslen,
 				 const char *orig,
 				 const struct interp *interps, int ninterps);
-extern char *interp_find_active(const char *orig,
+extern void interp_find_active(const char *orig,
 				const struct interp *interps, int ninterps);
 
 #endif /* INTERPOLATE_H */


Hmm?

Ciao,
Dscho
Previous: Jon LoeligerNext: Junio C Hamano
Message 9 of 24 in “Make user formatted commit listing less expensive”
  1. 0/3 Make user formatted commit listing less expensiveJohannes Schindelin, Nov 4, 2007
  2. 1/3 Split off the pretty print stuff into its own fileJohannes Schindelin, Nov 4, 2007
  3. Junio C HamanoNov 5, 2007
  4. 2/3 interpolate.[ch]: Add a function to find which interpolations are active.Johannes Schindelin, Nov 4, 2007
  5. 3/3 pretty=format: Avoid some expensive calculations when not neededJohannes Schindelin, Nov 4, 2007
  6. Junio C HamanoNov 5, 2007
  7. René ScharfeNov 5, 2007
  8. Jon LoeligerNov 5, 2007
  9. Johannes SchindelinNov 5, 2007
  10. Junio C HamanoNov 6, 2007
  11. René ScharfeNov 6, 2007
  12. René ScharfeNov 6, 2007
  13. Johannes SchindelinNov 6, 2007
  14. René ScharfeNov 7, 2007
  15. Johannes SchindelinNov 8, 2007
  16. Pierre HabouzitNov 7, 2007
  17. Pierre HabouzitNov 7, 2007
  18. René ScharfeNov 7, 2007
  19. Pierre HabouzitNov 7, 2007
  20. Junio C HamanoNov 7, 2007
  21. René ScharfeNov 9, 2007
  22. Johannes SchindelinNov 6, 2007
  23. 1/2 interpolate.[ch]: Add a function to find which interpolations are active.Johannes Schindelin, Nov 6, 2007
  24. 2/2 pretty=format: Avoid some expensive calculations when not neededJohannes Schindelin, Nov 6, 2007

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.