--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>
1 file changed
tree: 34015a5b2a006babbf0eec2a09b28198da9d7d69
  1. arm/
  2. compat/
  3. contrib/
  4. Documentation/
  5. git-gui/
  6. gitweb/
  7. mozilla-sha1/
  8. perl/
  9. ppc/
  10. t/
  11. templates/
  12. xdiff/
  13. .gitignore
  14. .mailmap
  15. alloc.c
  16. archive-tar.c
  17. archive-zip.c
  18. archive.h
  19. attr.c
  20. attr.h
  21. base85.c
  22. blob.c
  23. blob.h
  24. builtin-add.c
  25. builtin-annotate.c
  26. builtin-apply.c
  27. builtin-archive.c
  28. builtin-blame.c
  29. builtin-branch.c
  30. builtin-bundle.c
  31. builtin-cat-file.c
  32. builtin-check-attr.c
  33. builtin-check-ref-format.c
  34. builtin-checkout-index.c
  35. builtin-commit-tree.c
  36. builtin-config.c
  37. builtin-count-objects.c
  38. builtin-describe.c
  39. builtin-diff-files.c
  40. builtin-diff-index.c
  41. builtin-diff-tree.c
  42. builtin-diff.c
  43. builtin-fetch--tool.c
  44. builtin-fetch-pack.c
  45. builtin-fetch.c
  46. builtin-fmt-merge-msg.c
  47. builtin-for-each-ref.c
  48. builtin-fsck.c
  49. builtin-gc.c
  50. builtin-grep.c
  51. builtin-http-fetch.c
  52. builtin-init-db.c
  53. builtin-log.c
  54. builtin-ls-files.c
  55. builtin-ls-tree.c
  56. builtin-mailinfo.c
  57. builtin-mailsplit.c
  58. builtin-merge-base.c
  59. builtin-merge-file.c
  60. builtin-mv.c
  61. builtin-name-rev.c
  62. builtin-pack-objects.c
  63. builtin-pack-refs.c
  64. builtin-prune-packed.c
  65. builtin-prune.c
  66. builtin-push.c
  67. builtin-read-tree.c
  68. builtin-reflog.c
  69. builtin-rerere.c
  70. builtin-reset.c
  71. builtin-rev-list.c
  72. builtin-rev-parse.c
  73. builtin-revert.c
  74. builtin-rm.c
  75. builtin-runstatus.c
  76. builtin-shortlog.c
  77. builtin-show-branch.c
  78. builtin-show-ref.c
  79. builtin-stripspace.c
  80. builtin-symbolic-ref.c
  81. builtin-tag.c
  82. builtin-tar-tree.c
  83. builtin-unpack-objects.c
  84. builtin-update-index.c
  85. builtin-update-ref.c
  86. builtin-upload-archive.c
  87. builtin-verify-pack.c
  88. builtin-verify-tag.c
  89. builtin-write-tree.c
  90. builtin.h
  91. bundle.c
  92. bundle.h
  93. cache-tree.c
  94. cache-tree.h
  95. cache.h
  96. check-builtins.sh
  97. check-racy.c
  98. color.c
  99. color.h
  100. combine-diff.c
  101. commit.c
  102. commit.h
  103. config.c
  104. config.mak.in
  105. configure.ac
  106. connect.c
  107. convert.c
  108. copy.c
  109. COPYING
  110. csum-file.c
  111. csum-file.h
  112. ctype.c
  113. daemon.c
  114. date.c
  115. decorate.c
  116. decorate.h
  117. delta.h
  118. diff-delta.c
  119. diff-lib.c
  120. diff.c
  121. diff.h
  122. diffcore-break.c
  123. diffcore-delta.c
  124. diffcore-order.c
  125. diffcore-pickaxe.c
  126. diffcore-rename.c
  127. diffcore.h
  128. dir.c
  129. dir.h
  130. dump-cache-tree.c
  131. entry.c
  132. environment.c
  133. exec_cmd.c
  134. exec_cmd.h
  135. fast-import.c
  136. fetch-pack.h
  137. fixup-builtins
  138. generate-cmdlist.sh
  139. git-add--interactive.perl
  140. git-am.sh
  141. git-archimport.perl
  142. git-bisect.sh
  143. git-checkout.sh
  144. git-clean.sh
  145. git-clone.sh
  146. git-commit.sh
  147. git-compat-util.h
  148. git-cvsexportcommit.perl
  149. git-cvsimport.perl
  150. git-cvsserver.perl
  151. git-filter-branch.sh
  152. git-instaweb.sh
  153. git-lost-found.sh
  154. git-ls-remote.sh
  155. git-merge-octopus.sh
  156. git-merge-one-file.sh
  157. git-merge-ours.sh
  158. git-merge-resolve.sh
  159. git-merge-stupid.sh
  160. git-merge.sh
  161. git-mergetool.sh
  162. git-parse-remote.sh
  163. git-pull.sh
  164. git-quiltimport.sh
  165. git-rebase--interactive.sh
  166. git-rebase.sh
  167. git-relink.perl
  168. git-remote.perl
  169. git-repack.sh
  170. git-request-pull.sh
  171. git-send-email.perl
  172. git-sh-setup.sh
  173. git-stash.sh
  174. git-submodule.sh
  175. git-svn.perl
  176. GIT-VERSION-GEN
  177. git.c
  178. git.spec.in
  179. gitk
  180. grep.c
  181. grep.h
  182. hash-object.c
  183. hash.c
  184. hash.h
  185. help.c
  186. http-push.c
  187. http-walker.c
  188. http.c
  189. http.h
  190. ident.c
  191. imap-send.c
  192. index-pack.c
  193. INSTALL
  194. interpolate.c
  195. interpolate.h
  196. list-objects.c
  197. list-objects.h
  198. lockfile.c
  199. log-tree.c
  200. log-tree.h
  201. mailmap.c
  202. mailmap.h
  203. Makefile
  204. match-trees.c
  205. merge-file.c
  206. merge-index.c
  207. merge-recursive.c
  208. merge-tree.c
  209. mktag.c
  210. mktree.c
  211. object-refs.c
  212. object.c
  213. object.h
  214. pack-check.c
  215. pack-redundant.c
  216. pack-write.c
  217. pack.h
  218. pager.c
  219. parse-options.c
  220. parse-options.h
  221. patch-delta.c
  222. patch-id.c
  223. patch-ids.c
  224. patch-ids.h
  225. path-list.c
  226. path-list.h
  227. path.c
  228. peek-remote.c
  229. pkt-line.c
  230. pkt-line.h
  231. pretty.c
  232. progress.c
  233. progress.h
  234. quote.c
  235. quote.h
  236. reachable.c
  237. reachable.h
  238. read-cache.c
  239. README
  240. receive-pack.c
  241. reflog-walk.c
  242. reflog-walk.h
  243. refs.c
  244. refs.h
  245. remote.c
  246. remote.h
  247. revision.c
  248. revision.h
  249. run-command.c
  250. run-command.h
  251. send-pack.c
  252. server-info.c
  253. setup.c
  254. sha1_file.c
  255. sha1_name.c
  256. shallow.c
  257. shell.c
  258. show-index.c
  259. sideband.c
  260. sideband.h
  261. strbuf.c
  262. strbuf.h
  263. symlinks.c
  264. tag.c
  265. tag.h
  266. tar.h
  267. test-absolute-path.c
  268. test-chmtime.c
  269. test-date.c
  270. test-delta.c
  271. test-genrandom.c
  272. test-match-trees.c
  273. test-parse-options.c
  274. test-sha1.c
  275. test-sha1.sh
  276. trace.c
  277. transport.c
  278. transport.h
  279. tree-diff.c
  280. tree-walk.c
  281. tree-walk.h
  282. tree.c
  283. tree.h
  284. unpack-file.c
  285. unpack-trees.c
  286. unpack-trees.h
  287. update-server-info.c
  288. upload-pack.c
  289. usage.c
  290. utf8.c
  291. utf8.h
  292. var.c
  293. walker.c
  294. walker.h
  295. write_or_die.c
  296. wt-status.c
  297. wt-status.h
  298. xdiff-interface.c
  299. xdiff-interface.h