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

Re: [PATCH 3/3] --format=pretty: avoid calculating expensive expansions twice

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 11, 2007, 10:29 UTC
Message-ID
<7vejexf53b.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<47359382.1010600@lsrfire.ath.cx>
René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:
Show 30 quoted lines
> +static int add_again(struct strbuf *sb, struct chunk *chunk)
> +{
> +	if (chunk->len) {
> +		strbuf_adddup(sb, chunk->off, chunk->len);
> +		return 1;
> +	}
> +
> +	/*
> +	 * We haven't seen this chunk before.  Our caller is surely
> +	 * going to add it the hard way now.  Remember the most likely
> +	 * start of the to-be-added chunk: the current end of the
> +	 * struct strbuf.
> +	 */
> +	chunk->off = sb->len;
> +	return 0;
> +}
> +
>  static void parse_commit_header(struct format_commit_context *context)
>  {
>  	const char *msg = context->commit->buffer;
> @@ -447,15 +469,21 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
>  		strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
>  		return;
>  	case 'h':		/* abbreviated commit hash */
> +		if (add_again(sb, &c->abbrev_commit_hash))
> +			return;
>  		strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
>  		                                     DEFAULT_ABBREV));
> +		c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
>  		return;

Brilliant. Doubly brilliant is the adddup abstraction that does not suffer from underlying strbuf being reallocated.

Me likee..
Previous: René ScharfeNext: Jeff King
Message 20 of 24 in “--pretty=format: on-demand format expansion”
  1. 2/2 --pretty=format: on-demand format expansionRené Scharfe, Nov 9, 2007
  2. Johannes SchindelinNov 9, 2007
  3. René ScharfeNov 9, 2007
  4. Johannes SchindelinNov 9, 2007
  5. Junio C HamanoNov 9, 2007
  6. Jeff KingNov 9, 2007
  7. René ScharfeNov 9, 2007
  8. Johannes SchindelinNov 10, 2007
  9. Jeff KingNov 10, 2007
  10. Jeff KingNov 10, 2007
  11. René ScharfeNov 10, 2007
  12. Johannes SchindelinNov 10, 2007
  13. René ScharfeNov 10, 2007
  14. Jeff KingNov 10, 2007
  15. Jeff KingNov 10, 2007
  16. Jeff KingNov 11, 2007
  17. 1/3 --pretty=format: parse commit message only onceRené Scharfe, Nov 10, 2007
  18. 2/3 add strbuf_adddup()René Scharfe, Nov 10, 2007
  19. 3/3 --format=pretty: avoid calculating expensive expansions twiceRené Scharfe, Nov 10, 2007
  20. Junio C HamanoNov 11, 2007
  21. Jeff KingNov 9, 2007
  22. René ScharfeNov 9, 2007
  23. Jeff KingNov 10, 2007
  24. Paul MackerrasNov 9, 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.