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

Use strbuf for struct xdiff_emit_state's remainder

Continually xreallocing and freeing the remainder member of struct xdiff_emit_state was a noticeable performance hit. Use a strbuf instead. This yields a decent performance improvement on "git blame" on certain repositories. For example, before this commit: $ time git blame -M -C -C -p --incremental server.c >/dev/null 101.52user 0.17system 1:41.73elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+39561minor)pagefaults 0swaps With this commit: $ time git blame -M -C -C -p --incremental server.c >/dev/null 80.38user 0.30system 1:20.81elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+50979minor)pagefaults 0swaps Signed-off-by: Brian Downing <bdowning@lavos.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brian Downing committed Aug 14, 2008 at 00:36 UTC b463776086a12c587f6d91c0347641fb6f7ddd72
2 files changed +12 -24
xdiff-interface.c
+10 -22
@@ -69,36 +69,22 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
69 for (i = 0; i < nbuf; i++) {
70 if (mb[i].ptr[mb[i].size-1] != '\n') {
71 /* Incomplete line */
72 - priv->remainder = xrealloc(priv->remainder,
73 - priv->remainder_size +
74 - mb[i].size);
75 - memcpy(priv->remainder + priv->remainder_size,
76 - mb[i].ptr, mb[i].size);
77 - priv->remainder_size += mb[i].size;
72 + strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
73 continue;
74 }
75
76 /* we have a complete line */
82 - if (!priv->remainder) {
77 + if (!priv->remainder.len) {
78 consume_one(priv, mb[i].ptr, mb[i].size);
79 continue;
80 }
86 - priv->remainder = xrealloc(priv->remainder,
87 - priv->remainder_size +
88 - mb[i].size);
89 - memcpy(priv->remainder + priv->remainder_size,
90 - mb[i].ptr, mb[i].size);
91 - consume_one(priv, priv->remainder,
92 - priv->remainder_size + mb[i].size);
93 - free(priv->remainder);
94 - priv->remainder = NULL;
95 - priv->remainder_size = 0;
81 + strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
82 + consume_one(priv, priv->remainder.buf, priv->remainder.len);
83 + strbuf_reset(&priv->remainder);
84 }
97 - if (priv->remainder) {
98 - consume_one(priv, priv->remainder, priv->remainder_size);
99 - free(priv->remainder);
100 - priv->remainder = NULL;
101 - priv->remainder_size = 0;
85 + if (priv->remainder.len) {
86 + consume_one(priv, priv->remainder.buf, priv->remainder.len);
87 + strbuf_reset(&priv->remainder);
88 }
89 return 0;
90 }
@@ -148,7 +134,9 @@ int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
134 int ret;
135 xecb->outf = xdiff_outf;
136 xecb->priv = state;
137 + strbuf_init(&state->remainder, 0);
138 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
139 + strbuf_release(&state->remainder);
140 return ret;
141 }
142
xdiff-interface.h
+2 -2
@@ -2,6 +2,7 @@
2 #define XDIFF_INTERFACE_H
3
4 #include "xdiff/xdiff.h"
5 +#include "strbuf.h"
6
7 struct xdiff_emit_state;
8
@@ -9,8 +10,7 @@ typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
10
11 struct xdiff_emit_state {
12 xdiff_emit_consume_fn consume;
12 - char *remainder;
13 - unsigned long remainder_size;
13 + struct strbuf remainder;
14 };
15
16 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);