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
Pierre Habouzit <madcoder@debian.org>
Date
Nov 7, 2007, 00:11 UTC
Message-ID
<20071107001112.GD4382@artemis.corp>
In-Reply-To
<4730F5FA.3030705@lsrfire.ath.cx>
On Tue, Nov 06, 2007 at 11:17:14PM +0000, René Scharfe wrote:
> I haven't seen any comments on strbuf_expand.  Is it too far out?
> Here it is again, adjusted for current master and with the changes
> to strbuf.[ch] coming first:
  I have one.
Show 24 quoted lines
>  strbuf.c |   22 +++++
>  strbuf.h |    3 
>  pretty.c |  276 ++++++++++++++++++++++++++++++++++-----------------------------
>  3 files changed, 178 insertions(+), 123 deletions(-)
> 
> diff --git a/strbuf.c b/strbuf.c
> index f4201e1..b71da99 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -129,6 +129,28 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
>  	strbuf_setlen(sb, sb->len + len);
>  }
>  
> +void strbuf_expand(struct strbuf *sb, const char *fmt,
> +                   const char **placeholders, expand_fn_t fn, void *context)
> +{
> +	char c;
> +	const char **p;
> +
> +	while ((c = *fmt++)) {
> +		if (c != '%') {
> +			strbuf_addch(sb, c);
> +			continue;
> +		}

strbuf_addch is pretty inneficient as it puts NULs each time. rather do that (sketchy) :

{
    for (;;) {
        const char *percent = strchr(fmt, '%');
        if (!percent)
            break;
        strbuf_add(sb, fmt, percent - fmt);
        fmt = percent + 1;
        /* do your stuff */
    }
    strbuf_addstr(sb, fmt);
}

Of course it's a detail as formats will probably be short. But it's a good example to show to people wanting to write new strbuf functions.

This nitpicking apart, the timings are impressive.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Previous: Johannes SchindelinNext: Pierre Habouzit
Message 16 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.