diff.c: get rid of duplicate implementation

The implementations in diff.c to detect moved lines needs to compare strings and hash strings, which is implemented in that file, as well as in the xdiff library. Remove the rather recent implementation in diff.c and rely on the well exercised code in the xdiff lib. With this change the hash used for bucketing the strings for the moved line detection changes from FNV32 (that is provided via the hashmaps memhash) to DJB2 (which is used internally in xdiff). Benchmarks found on the web[1] do not indicate that these hashes are different in performance for readable strings. [1] https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Oct 25, 2017 at 11:49 UTC 01be97c2b285e8ba377ba58385ef6ad2e7815c93
1 file changed +4 -78
diff.c
+4 -78
@@ -707,88 +707,14 @@ struct moved_entry {
707 struct moved_entry *next_line;
708 };
709
710 -static int next_byte(const char **cp, const char **endp,
711 - const struct diff_options *diffopt)
712 -{
713 - int retval;
714 -
715 - if (*cp >= *endp)
716 - return -1;
717 -
718 - if (isspace(**cp)) {
719 - if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
720 - while (*cp < *endp && isspace(**cp))
721 - (*cp)++;
722 - /*
723 - * After skipping a couple of whitespaces,
724 - * we still have to account for one space.
725 - */
726 - return (int)' ';
727 - }
728 -
729 - if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
730 - while (*cp < *endp && isspace(**cp))
731 - (*cp)++;
732 - /*
733 - * return the first non-ws character via the usual
734 - * below, unless we ate all of the bytes
735 - */
736 - if (*cp >= *endp)
737 - return -1;
738 - }
739 - }
740 -
741 - retval = (unsigned char)(**cp);
742 - (*cp)++;
743 - return retval;
744 -}
745 -
710 static int moved_entry_cmp(const struct diff_options *diffopt,
711 const struct moved_entry *a,
712 const struct moved_entry *b,
713 const void *keydata)
714 {
751 - const char *ap = a->es->line, *ae = a->es->line + a->es->len;
752 - const char *bp = b->es->line, *be = b->es->line + b->es->len;
753 -
754 - if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
755 - return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
756 -
757 - if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
758 - while (ae > ap && isspace(ae[-1]))
759 - ae--;
760 - while (be > bp && isspace(be[-1]))
761 - be--;
762 - }
763 -
764 - while (1) {
765 - int ca, cb;
766 - ca = next_byte(&ap, &ae, diffopt);
767 - cb = next_byte(&bp, &be, diffopt);
768 - if (ca != cb)
769 - return 1;
770 - if (ca < 0)
771 - return 0;
772 - }
773 -}
774 -
775 -static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
776 -{
777 - if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
778 - static struct strbuf sb = STRBUF_INIT;
779 - const char *ap = es->line, *ae = es->line + es->len;
780 - int c;
781 -
782 - strbuf_reset(&sb);
783 - while (ae > ap && isspace(ae[-1]))
784 - ae--;
785 - while ((c = next_byte(&ap, &ae, o)) >= 0)
786 - strbuf_addch(&sb, c);
787 -
788 - return memhash(sb.buf, sb.len);
789 - } else {
790 - return memhash(es->line, es->len);
791 - }
715 + return !xdiff_compare_lines(a->es->line, a->es->len,
716 + b->es->line, b->es->len,
717 + diffopt->xdl_opts);
718 }
719
720 static struct moved_entry *prepare_entry(struct diff_options *o,
@@ -797,7 +723,7 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
723 struct moved_entry *ret = xmalloc(sizeof(*ret));
724 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
725
800 - ret->ent.hash = get_string_hash(l, o);
726 + ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
727 ret->es = l;
728 ret->next_line = NULL;
729