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

--pretty=format: on-demand format expansion

Some of the --pretty=format placeholders expansions are expensive to calculate. This is made worse by the current code's use of interpolate(), which requires _all_ placeholders are to be prepared up front. One way to speed this up is to check which placeholders are present in the format string and to prepare only the expansions that are needed. That still leaves the allocation overhead of interpolate(). Another way is to use a callback based approach together with the strbuf library to keep allocations to a minimum and avoid string copies. That's what this patch does. It introduces a new strbuf function, strbuf_expand(). The function takes a format string, list of placeholder strings, a user supplied function 'fn', and an opaque pointer 'context' to tell 'fn' what thingy to operate on. The function 'fn' is expected to accept a strbuf, a parsed placeholder string and the 'context' pointer, and append the interpolated value for the 'context' thingy, according to the format specified by the placeholder. Thanks to Pierre Habouzit for his suggestion to use strchrnul() and the code surrounding its callsite. And thanks to Junio for most of this commit message. :) Here my measurements of most of Paul Mackerras' test cases that highlighted the performance problem (best of three runs): (master) $ time git log --pretty=oneline >/dev/null real 0m0.390s user 0m0.340s sys 0m0.040s (master) $ time git log --pretty=raw >/dev/null real 0m0.434s user 0m0.408s sys 0m0.016s (master) $ time git log --pretty="format:%H {%P} %ct" >/dev/null real 0m1.347s user 0m0.080s sys 0m1.256s (interp_find_active -- Dscho) $ time ./git log --pretty="format:%H {%P} %ct" >/dev/null real 0m0.694s user 0m0.020s sys 0m0.672s (strbuf_expand -- this patch) $ time ./git log --pretty="format:%H {%P} %ct" >/dev/null real 0m0.395s user 0m0.352s sys 0m0.028s Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 9, 2007 at 01:49 UTC cde75e59e1b2d8dd3ba49bc9034692dd06ee3907
3 files changed +180 -123
pretty.c
+153 -123
@@ -1,6 +1,5 @@
1 #include "cache.h"
2 #include "commit.h"
3 -#include "interpolate.h"
3 #include "utf8.h"
4 #include "diff.h"
5 #include "revision.h"
@@ -283,7 +282,8 @@ static char *logmsg_reencode(const struct commit *commit,
282 return out;
283 }
284
286 -static void fill_person(struct interp *table, const char *msg, int len)
285 +static void format_person_part(struct strbuf *sb, char part,
286 + const char *msg, int len)
287 {
288 int start, end, tz = 0;
289 unsigned long date;
@@ -295,7 +295,10 @@ static void fill_person(struct interp *table, const char *msg, int len)
295 start = end + 1;
296 while (end > 0 && isspace(msg[end - 1]))
297 end--;
298 - table[0].value = xmemdupz(msg, end);
298 + if (part == 'n') { /* name */
299 + strbuf_add(sb, msg, end);
300 + return;
301 + }
302
303 if (start >= len)
304 return;
@@ -307,7 +310,10 @@ static void fill_person(struct interp *table, const char *msg, int len)
310 if (end >= len)
311 return;
312
310 - table[1].value = xmemdupz(msg + start, end - start);
313 + if (part == 'e') { /* email */
314 + strbuf_add(sb, msg + start, end - start);
315 + return;
316 + }
317
318 /* parse date */
319 for (start = end + 1; start < len && isspace(msg[start]); start++)
@@ -318,7 +324,10 @@ static void fill_person(struct interp *table, const char *msg, int len)
324 if (msg + start == ep)
325 return;
326
321 - table[5].value = xmemdupz(msg + start, ep - (msg + start));
327 + if (part == 't') { /* date, UNIX timestamp */
328 + strbuf_add(sb, msg + start, ep - (msg + start));
329 + return;
330 + }
331
332 /* parse tz */
333 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
@@ -329,123 +338,107 @@ static void fill_person(struct interp *table, const char *msg, int len)
338 tz = -tz;
339 }
340
332 - interp_set_entry(table, 2, show_date(date, tz, DATE_NORMAL));
333 - interp_set_entry(table, 3, show_date(date, tz, DATE_RFC2822));
334 - interp_set_entry(table, 4, show_date(date, tz, DATE_RELATIVE));
335 - interp_set_entry(table, 6, show_date(date, tz, DATE_ISO8601));
341 + switch (part) {
342 + case 'd': /* date */
343 + strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
344 + return;
345 + case 'D': /* date, RFC2822 style */
346 + strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
347 + return;
348 + case 'r': /* date, relative */
349 + strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
350 + return;
351 + case 'i': /* date, ISO 8601 */
352 + strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
353 + return;
354 + }
355 }
356
338 -void format_commit_message(const struct commit *commit,
339 - const void *format, struct strbuf *sb)
357 +static void format_commit_item(struct strbuf *sb, const char *placeholder,
358 + void *context)
359 {
341 - struct interp table[] = {
342 - { "%H" }, /* commit hash */
343 - { "%h" }, /* abbreviated commit hash */
344 - { "%T" }, /* tree hash */
345 - { "%t" }, /* abbreviated tree hash */
346 - { "%P" }, /* parent hashes */
347 - { "%p" }, /* abbreviated parent hashes */
348 - { "%an" }, /* author name */
349 - { "%ae" }, /* author email */
350 - { "%ad" }, /* author date */
351 - { "%aD" }, /* author date, RFC2822 style */
352 - { "%ar" }, /* author date, relative */
353 - { "%at" }, /* author date, UNIX timestamp */
354 - { "%ai" }, /* author date, ISO 8601 */
355 - { "%cn" }, /* committer name */
356 - { "%ce" }, /* committer email */
357 - { "%cd" }, /* committer date */
358 - { "%cD" }, /* committer date, RFC2822 style */
359 - { "%cr" }, /* committer date, relative */
360 - { "%ct" }, /* committer date, UNIX timestamp */
361 - { "%ci" }, /* committer date, ISO 8601 */
362 - { "%e" }, /* encoding */
363 - { "%s" }, /* subject */
364 - { "%b" }, /* body */
365 - { "%Cred" }, /* red */
366 - { "%Cgreen" }, /* green */
367 - { "%Cblue" }, /* blue */
368 - { "%Creset" }, /* reset color */
369 - { "%n" }, /* newline */
370 - { "%m" }, /* left/right/bottom */
371 - };
372 - enum interp_index {
373 - IHASH = 0, IHASH_ABBREV,
374 - ITREE, ITREE_ABBREV,
375 - IPARENTS, IPARENTS_ABBREV,
376 - IAUTHOR_NAME, IAUTHOR_EMAIL,
377 - IAUTHOR_DATE, IAUTHOR_DATE_RFC2822, IAUTHOR_DATE_RELATIVE,
378 - IAUTHOR_TIMESTAMP, IAUTHOR_ISO8601,
379 - ICOMMITTER_NAME, ICOMMITTER_EMAIL,
380 - ICOMMITTER_DATE, ICOMMITTER_DATE_RFC2822,
381 - ICOMMITTER_DATE_RELATIVE, ICOMMITTER_TIMESTAMP,
382 - ICOMMITTER_ISO8601,
383 - IENCODING,
384 - ISUBJECT,
385 - IBODY,
386 - IRED, IGREEN, IBLUE, IRESET_COLOR,
387 - INEWLINE,
388 - ILEFT_RIGHT,
389 - };
360 + const struct commit *commit = context;
361 struct commit_list *p;
391 - char parents[1024];
392 - unsigned long len;
362 int i;
363 enum { HEADER, SUBJECT, BODY } state;
364 const char *msg = commit->buffer;
365
397 - if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table))
398 - die("invalid interp table!");
399 -
366 /* these are independent of the commit */
401 - interp_set_entry(table, IRED, "\033[31m");
402 - interp_set_entry(table, IGREEN, "\033[32m");
403 - interp_set_entry(table, IBLUE, "\033[34m");
404 - interp_set_entry(table, IRESET_COLOR, "\033[m");
405 - interp_set_entry(table, INEWLINE, "\n");
367 + switch (placeholder[0]) {
368 + case 'C':
369 + switch (placeholder[3]) {
370 + case 'd': /* red */
371 + strbuf_addstr(sb, "\033[31m");
372 + return;
373 + case 'e': /* green */
374 + strbuf_addstr(sb, "\033[32m");
375 + return;
376 + case 'u': /* blue */
377 + strbuf_addstr(sb, "\033[34m");
378 + return;
379 + case 's': /* reset color */
380 + strbuf_addstr(sb, "\033[m");
381 + return;
382 + }
383 + case 'n': /* newline */
384 + strbuf_addch(sb, '\n');
385 + return;
386 + }
387
388 /* these depend on the commit */
389 if (!commit->object.parsed)
390 parse_object(commit->object.sha1);
410 - interp_set_entry(table, IHASH, sha1_to_hex(commit->object.sha1));
411 - interp_set_entry(table, IHASH_ABBREV,
412 - find_unique_abbrev(commit->object.sha1,
413 - DEFAULT_ABBREV));
414 - interp_set_entry(table, ITREE, sha1_to_hex(commit->tree->object.sha1));
415 - interp_set_entry(table, ITREE_ABBREV,
416 - find_unique_abbrev(commit->tree->object.sha1,
417 - DEFAULT_ABBREV));
418 - interp_set_entry(table, ILEFT_RIGHT,
419 - (commit->object.flags & BOUNDARY)
420 - ? "-"
421 - : (commit->object.flags & SYMMETRIC_LEFT)
422 - ? "<"
423 - : ">");
424 -
425 - parents[1] = 0;
426 - for (i = 0, p = commit->parents;
427 - p && i < sizeof(parents) - 1;
428 - p = p->next)
429 - i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
430 - sha1_to_hex(p->item->object.sha1));
431 - interp_set_entry(table, IPARENTS, parents + 1);
432 -
433 - parents[1] = 0;
434 - for (i = 0, p = commit->parents;
435 - p && i < sizeof(parents) - 1;
436 - p = p->next)
437 - i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
438 - find_unique_abbrev(p->item->object.sha1,
439 - DEFAULT_ABBREV));
440 - interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
391
392 + switch (placeholder[0]) {
393 + case 'H': /* commit hash */
394 + strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
395 + return;
396 + case 'h': /* abbreviated commit hash */
397 + strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
398 + DEFAULT_ABBREV));
399 + return;
400 + case 'T': /* tree hash */
401 + strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
402 + return;
403 + case 't': /* abbreviated tree hash */
404 + strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
405 + DEFAULT_ABBREV));
406 + return;
407 + case 'P': /* parent hashes */
408 + for (p = commit->parents; p; p = p->next) {
409 + if (p != commit->parents)
410 + strbuf_addch(sb, ' ');
411 + strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
412 + }
413 + return;
414 + case 'p': /* abbreviated parent hashes */
415 + for (p = commit->parents; p; p = p->next) {
416 + if (p != commit->parents)
417 + strbuf_addch(sb, ' ');
418 + strbuf_addstr(sb, find_unique_abbrev(
419 + p->item->object.sha1, DEFAULT_ABBREV));
420 + }
421 + return;
422 + case 'm': /* left/right/bottom */
423 + strbuf_addch(sb, (commit->object.flags & BOUNDARY)
424 + ? '-'
425 + : (commit->object.flags & SYMMETRIC_LEFT)
426 + ? '<'
427 + : '>');
428 + return;
429 + }
430 +
431 + /* For the rest we have to parse the commit header. */
432 for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
433 int eol;
434 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
435 ; /* do nothing */
436
437 if (state == SUBJECT) {
448 - table[ISUBJECT].value = xmemdupz(msg + i, eol - i);
438 + if (placeholder[0] == 's') {
439 + strbuf_add(sb, msg + i, eol - i);
440 + return;
441 + }
442 i = eol;
443 }
444 if (i == eol) {
@@ -453,29 +446,66 @@ void format_commit_message(const struct commit *commit,
446 /* strip empty lines */
447 while (msg[eol + 1] == '\n')
448 eol++;
456 - } else if (!prefixcmp(msg + i, "author "))
457 - fill_person(table + IAUTHOR_NAME,
458 - msg + i + 7, eol - i - 7);
459 - else if (!prefixcmp(msg + i, "committer "))
460 - fill_person(table + ICOMMITTER_NAME,
461 - msg + i + 10, eol - i - 10);
462 - else if (!prefixcmp(msg + i, "encoding "))
463 - table[IENCODING].value =
464 - xmemdupz(msg + i + 9, eol - i - 9);
449 + } else if (!prefixcmp(msg + i, "author ")) {
450 + if (placeholder[0] == 'a') {
451 + format_person_part(sb, placeholder[1],
452 + msg + i + 7, eol - i - 7);
453 + return;
454 + }
455 + } else if (!prefixcmp(msg + i, "committer ")) {
456 + if (placeholder[0] == 'c') {
457 + format_person_part(sb, placeholder[1],
458 + msg + i + 10, eol - i - 10);
459 + return;
460 + }
461 + } else if (!prefixcmp(msg + i, "encoding ")) {
462 + if (placeholder[0] == 'e') {
463 + strbuf_add(sb, msg + i + 9, eol - i - 9);
464 + return;
465 + }
466 + }
467 i = eol;
468 }
467 - if (msg[i])
468 - table[IBODY].value = xstrdup(msg + i);
469 -
470 - len = interpolate(sb->buf + sb->len, strbuf_avail(sb),
471 - format, table, ARRAY_SIZE(table));
472 - if (len > strbuf_avail(sb)) {
473 - strbuf_grow(sb, len);
474 - interpolate(sb->buf + sb->len, strbuf_avail(sb) + 1,
475 - format, table, ARRAY_SIZE(table));
476 - }
477 - strbuf_setlen(sb, sb->len + len);
478 - interp_clear_table(table, ARRAY_SIZE(table));
469 + if (msg[i] && placeholder[0] == 'b') /* body */
470 + strbuf_addstr(sb, msg + i);
471 +}
472 +
473 +void format_commit_message(const struct commit *commit,
474 + const void *format, struct strbuf *sb)
475 +{
476 + const char *placeholders[] = {
477 + "H", /* commit hash */
478 + "h", /* abbreviated commit hash */
479 + "T", /* tree hash */
480 + "t", /* abbreviated tree hash */
481 + "P", /* parent hashes */
482 + "p", /* abbreviated parent hashes */
483 + "an", /* author name */
484 + "ae", /* author email */
485 + "ad", /* author date */
486 + "aD", /* author date, RFC2822 style */
487 + "ar", /* author date, relative */
488 + "at", /* author date, UNIX timestamp */
489 + "ai", /* author date, ISO 8601 */
490 + "cn", /* committer name */
491 + "ce", /* committer email */
492 + "cd", /* committer date */
493 + "cD", /* committer date, RFC2822 style */
494 + "cr", /* committer date, relative */
495 + "ct", /* committer date, UNIX timestamp */
496 + "ci", /* committer date, ISO 8601 */
497 + "e", /* encoding */
498 + "s", /* subject */
499 + "b", /* body */
500 + "Cred", /* red */
501 + "Cgreen", /* green */
502 + "Cblue", /* blue */
503 + "Creset", /* reset color */
504 + "n", /* newline */
505 + "m", /* left/right/bottom */
506 + NULL
507 + };
508 + strbuf_expand(sb, format, placeholders, format_commit_item, (void *)commit);
509 }
510
511 static void pp_header(enum cmit_fmt fmt,
strbuf.c
+24
@@ -129,6 +129,30 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
129 strbuf_setlen(sb, sb->len + len);
130 }
131
132 +void strbuf_expand(struct strbuf *sb, const char *format,
133 + const char **placeholders, expand_fn_t fn, void *context)
134 +{
135 + for (;;) {
136 + const char *percent, **p;
137 +
138 + percent = strchrnul(format, '%');
139 + strbuf_add(sb, format, percent - format);
140 + if (!*percent)
141 + break;
142 + format = percent + 1;
143 +
144 + for (p = placeholders; *p; p++) {
145 + if (!prefixcmp(format, *p))
146 + break;
147 + }
148 + if (*p) {
149 + fn(sb, *p, context);
150 + format += strlen(*p);
151 + } else
152 + strbuf_addch(sb, '%');
153 + }
154 +}
155 +
156 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
157 {
158 size_t res;
strbuf.h
+3
@@ -102,6 +102,9 @@ static inline void strbuf_addbuf(struct strbuf *sb, struct strbuf *sb2) {
102 strbuf_add(sb, sb2->buf, sb2->len);
103 }
104
105 +typedef void (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
106 +extern void strbuf_expand(struct strbuf *sb, const char *format, const char **placeholders, expand_fn_t fn, void *context);
107 +
108 __attribute__((format(printf,2,3)))
109 extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
110