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

From: René Scharfe <[email-masked]>
Date: Tue, 06 Nov 2007 23:31:42 +0100

Junio C Hamano schrieb:
> René Scharfe <[email-masked]> writes:
> 
>> Junio C Hamano schrieb: ...
>>> 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.
> 
> Sorry, where is this aversion to making the struct a bit larger 
> coming from?

Not from the rational part of my brain, for sure.  The following on
top of Dscho's second patch?  (A char would be smaller, but a bitfield
documents the intent better.)

diff --git a/interpolate.c b/interpolate.c
index 80eeb36..1e4ccaf 100644
--- a/interpolate.c
+++ b/interpolate.c
@@ -103,22 +103,21 @@ unsigned long interpolate(char *result, unsigned long reslen,
 	return newlen;
 }
 
-char *interp_find_active(const char *orig,
-		const struct interp *interps, int ninterps)
+void interp_find_active(const char *orig, struct interp *interps, int ninterps)
 {
-	char *result = xcalloc(1, ninterps);
 	char c;
 	int i;
 
+	for (i = 0; i < ninterps; i++)
+		interps[i].active = 0;
+
 	while ((c = *(orig++)))
 		if (c == '%')
 			/* Try to match an interpolation string. */
 			for (i = 0; i < ninterps; i++)
 				if (!prefixcmp(orig, interps[i].name + 1)) {
-					result[i] = 1;
+					interps[i].active = 1;
 					orig += strlen(interps[i].name + 1);
 					break;
 				}
-
-	return result;
 }
diff --git a/interpolate.h b/interpolate.h
index 2d197c5..a8ee6b9 100644
--- a/interpolate.h
+++ b/interpolate.h
@@ -14,6 +14,7 @@
 struct interp {
 	const char *name;
 	char *value;
+	unsigned active:1;
 };
 
 extern void interp_set_entry(struct interp *table, int slot, const char *value);
@@ -22,7 +23,6 @@ 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,
-				const struct interp *interps, int ninterps);
+extern void interp_find_active(const char *orig, struct interp *interps, int ninterps);
 
 #endif /* INTERPOLATE_H */