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

--pretty=format: parse commit message only once

As Jeff King pointed out, some placeholder expansions are related to each other: the steps to calculate one go most of the way towards calculating the other, too. This patch makes format_commit_message() parse the commit message only once, remembering the position of each item. This speeds up handling of format strings containing multiple placeholders from the set %s, %a*, %c*, %e, %b. Here are the timings for the git version in next. The first one is to estimate the overhead of the caching, the second one is taken from http://svn.tue.mpg.de/tentakel/trunk/tentakel/Makefile as an example of a format string found in the wild. The times are the fastest of three consecutive runs in each case: $ time git log --pretty=format:%e >/dev/null real 0m0.381s user 0m0.340s sys 0m0.024s $ time git log --pretty=format:"* %cd %cn%n%n%s%n%b" >/dev/null real 0m0.623s user 0m0.556s sys 0m0.052s And here the times with this patch: $ time git log --pretty=format:%e >/dev/null real 0m0.385s user 0m0.332s sys 0m0.040s $ time git log --pretty=format:"* %cd %cn%n%n%s%n%b" >/dev/null real 0m0.563s user 0m0.504s sys 0m0.048s 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:14 UTC f29d59586c2a1666d18776cca5d96752dec0e8a2
1 file changed +82 -42
pretty.c
+82 -42
@@ -354,14 +354,67 @@ static void format_person_part(struct strbuf *sb, char part,
354 }
355 }
356
357 -static void format_commit_item(struct strbuf *sb, const char *placeholder,
358 - void *context)
357 +struct chunk {
358 + size_t off;
359 + size_t len;
360 +};
361 +
362 +struct format_commit_context {
363 + const struct commit *commit;
364 +
365 + /* These offsets are relative to the start of the commit message. */
366 + int commit_header_parsed;
367 + struct chunk subject;
368 + struct chunk author;
369 + struct chunk committer;
370 + struct chunk encoding;
371 + size_t body_off;
372 +};
373 +
374 +static void parse_commit_header(struct format_commit_context *context)
375 {
360 - const struct commit *commit = context;
361 - struct commit_list *p;
376 + const char *msg = context->commit->buffer;
377 int i;
378 enum { HEADER, SUBJECT, BODY } state;
379 +
380 + for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
381 + int eol;
382 + for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
383 + ; /* do nothing */
384 +
385 + if (state == SUBJECT) {
386 + context->subject.off = i;
387 + context->subject.len = eol - i;
388 + i = eol;
389 + }
390 + if (i == eol) {
391 + state++;
392 + /* strip empty lines */
393 + while (msg[eol + 1] == '\n')
394 + eol++;
395 + } else if (!prefixcmp(msg + i, "author ")) {
396 + context->author.off = i + 7;
397 + context->author.len = eol - i - 7;
398 + } else if (!prefixcmp(msg + i, "committer ")) {
399 + context->committer.off = i + 10;
400 + context->committer.len = eol - i - 10;
401 + } else if (!prefixcmp(msg + i, "encoding ")) {
402 + context->encoding.off = i + 9;
403 + context->encoding.len = eol - i - 9;
404 + }
405 + i = eol;
406 + }
407 + context->body_off = i;
408 + context->commit_header_parsed = 1;
409 +}
410 +
411 +static void format_commit_item(struct strbuf *sb, const char *placeholder,
412 + void *context)
413 +{
414 + struct format_commit_context *c = context;
415 + const struct commit *commit = c->commit;
416 const char *msg = commit->buffer;
417 + struct commit_list *p;
418
419 /* these are independent of the commit */
420 switch (placeholder[0]) {
@@ -429,45 +482,28 @@ static void format_commit_item(struct strbuf *sb, const char *placeholder,
482 }
483
484 /* 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 */
485 + if (!c->commit_header_parsed)
486 + parse_commit_header(c);
487
437 - if (state == SUBJECT) {
438 - if (placeholder[0] == 's') {
439 - strbuf_add(sb, msg + i, eol - i);
440 - return;
441 - }
442 - i = eol;
443 - }
444 - if (i == eol) {
445 - state++;
446 - /* strip empty lines */
447 - while (msg[eol + 1] == '\n')
448 - eol++;
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;
488 + switch (placeholder[0]) {
489 + case 's':
490 + strbuf_add(sb, msg + c->subject.off, c->subject.len);
491 + return;
492 + case 'a':
493 + format_person_part(sb, placeholder[1],
494 + msg + c->author.off, c->author.len);
495 + return;
496 + case 'c':
497 + format_person_part(sb, placeholder[1],
498 + msg + c->committer.off, c->committer.len);
499 + return;
500 + case 'e':
501 + strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
502 + return;
503 + case 'b':
504 + strbuf_addstr(sb, msg + c->body_off);
505 + return;
506 }
469 - if (msg[i] && placeholder[0] == 'b') /* body */
470 - strbuf_addstr(sb, msg + i);
507 }
508
509 void format_commit_message(const struct commit *commit,
@@ -505,7 +541,11 @@ void format_commit_message(const struct commit *commit,
541 "m", /* left/right/bottom */
542 NULL
543 };
508 - strbuf_expand(sb, format, placeholders, format_commit_item, (void *)commit);
544 + struct format_commit_context context;
545 +
546 + memset(&context, 0, sizeof(context));
547 + context.commit = commit;
548 + strbuf_expand(sb, format, placeholders, format_commit_item, &context);
549 }
550
551 static void pp_header(enum cmit_fmt fmt,