@matheusbsbs / matheus-bagliano / commits / b9c6232138
Fork 0

--format=pretty: avoid calculating expensive expansions twice

As Jeff King remarked, format strings with duplicate placeholders can be slow to expand, because each instance is calculated anew. This patch makes use of the fact that format_commit_message() and its helper functions only ever add stuff to the end of the strbuf. For certain expensive placeholders, store the offset and length of their expansion with the strbuf at the first occurrence. Later they expansion result can simply be copied from there -- no malloc() or strdup() required. These certain placeholders are the abbreviated commit, tree and parent hashes, as the search for a unique abbreviated hash is quite costly. Here are the times for next (best of three runs): $ time git log --pretty=format:%h >/dev/null real 0m0.611s user 0m0.404s sys 0m0.204s $ time git log --pretty=format:%h%h%h%h >/dev/null real 0m1.206s user 0m0.744s sys 0m0.452s And here those with this patch (and the previous two); the speedup of the single placeholder case is just noise: $ time git log --pretty=format:%h >/dev/null real 0m0.608s user 0m0.416s sys 0m0.192s $ time git log --pretty=format:%h%h%h%h >/dev/null real 0m0.639s user 0m0.488s sys 0m0.140s Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 10, 2007 at 12:18 UTC b9c62321380374d09cc99570cdd1390674532832
1 file changed +32
pretty.c
+32
@@ -369,8 +369,30 @@ struct format_commit_context {
369 struct chunk committer;
370 struct chunk encoding;
371 size_t body_off;
372 +
373 + /* The following ones are relative to the result struct strbuf. */
374 + struct chunk abbrev_commit_hash;
375 + struct chunk abbrev_tree_hash;
376 + struct chunk abbrev_parent_hashes;
377 };
378
379 +static int add_again(struct strbuf *sb, struct chunk *chunk)
380 +{
381 + if (chunk->len) {
382 + strbuf_adddup(sb, chunk->off, chunk->len);
383 + return 1;
384 + }
385 +
386 + /*
387 + * We haven't seen this chunk before. Our caller is surely
388 + * going to add it the hard way now. Remember the most likely
389 + * start of the to-be-added chunk: the current end of the
390 + * struct strbuf.
391 + */
392 + chunk->off = sb->len;
393 + return 0;
394 +}
395 +
396 static void parse_commit_header(struct format_commit_context *context)
397 {
398 const char *msg = context->commit->buffer;
@@ -447,15 +469,21 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
469 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
470 return;
471 case 'h': /* abbreviated commit hash */
472 + if (add_again(sb, &c->abbrev_commit_hash))
473 + return;
474 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
475 DEFAULT_ABBREV));
476 + c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
477 return;
478 case 'T': /* tree hash */
479 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
480 return;
481 case 't': /* abbreviated tree hash */
482 + if (add_again(sb, &c->abbrev_tree_hash))
483 + return;
484 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
485 DEFAULT_ABBREV));
486 + c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
487 return;
488 case 'P': /* parent hashes */
489 for (p = commit->parents; p; p = p->next) {
@@ -465,12 +493,16 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
493 }
494 return;
495 case 'p': /* abbreviated parent hashes */
496 + if (add_again(sb, &c->abbrev_parent_hashes))
497 + return;
498 for (p = commit->parents; p; p = p->next) {
499 if (p != commit->parents)
500 strbuf_addch(sb, ' ');
501 strbuf_addstr(sb, find_unique_abbrev(
502 p->item->object.sha1, DEFAULT_ABBREV));
503 }
504 + c->abbrev_parent_hashes.len = sb->len -
505 + c->abbrev_parent_hashes.off;
506 return;
507 case 'm': /* left/right/bottom */
508 strbuf_addch(sb, (commit->object.flags & BOUNDARY)