--format=pretty: avoid calculating expensive expansions twice
commitb9c62321380374d09cc99570cdd1390674532832
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>
Sat, 10 Nov 2007 11:18:26 +0000 (10 12:18 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sun, 11 Nov 2007 10:04:46 +0000 (11 02:04 -0800)
tree34015a5b2a006babbf0eec2a09b28198da9d7d69
parent91db267ec849279053cf3ac3066c2f2c11db4321
--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>
pretty.c