Mercurial > hg
comparison mercurial/bdiff_module.c @ 30561:7c0c722d568d
bdiff: early pruning of common prefix before doing expensive computations
It seems quite common that files don't change completely. New lines are often
pretty much appended, and modifications will often only change a small section
of the file which on average will be in the middle.
There can thus be a big win by pruning a common prefix before starting the more
expensive search for longest common substrings.
Worst case, it will scan through a long sequence of similar bytes without
encountering a newline. Splitlines will then have to do the same again ...
twice for each side. If similar lines are found, splitlines will save the
double iteration and hashing of the lines ... plus there will be less lines to
find common substrings in.
This change might in some cases make the algorith pick shorter or less optimal
common substrings. We can't have the cake and eat it.
This make hg --time bundle --base null -r 4.0 go from 14.5 to 15 s - a 3%
increase.
On mozilla-unified:
perfbdiff -m 3041e4d59df2
! wall 0.053088 comb 0.060000 user 0.060000 sys 0.000000 (best of 100) to
! wall 0.024618 comb 0.020000 user 0.020000 sys 0.000000 (best of 116)
perfbdiff 0e9928989e9c --alldata --count 10
! wall 0.702075 comb 0.700000 user 0.700000 sys 0.000000 (best of 15) to
! wall 0.579235 comb 0.580000 user 0.580000 sys 0.000000 (best of 18)
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Wed, 16 Nov 2016 19:45:35 +0100 |
parents | 15635d8b17e0 |
children | 08ecec297521 |
comparison
equal
deleted
inserted
replaced
30560:783016005122 | 30561:7c0c722d568d |
---|---|
59 return rl ? rl : PyErr_NoMemory(); | 59 return rl ? rl : PyErr_NoMemory(); |
60 } | 60 } |
61 | 61 |
62 static PyObject *bdiff(PyObject *self, PyObject *args) | 62 static PyObject *bdiff(PyObject *self, PyObject *args) |
63 { | 63 { |
64 char *sa, *sb, *rb; | 64 char *sa, *sb, *rb, *ia, *ib; |
65 PyObject *result = NULL; | 65 PyObject *result = NULL; |
66 struct bdiff_line *al, *bl; | 66 struct bdiff_line *al, *bl; |
67 struct bdiff_hunk l, *h; | 67 struct bdiff_hunk l, *h; |
68 int an, bn, count; | 68 int an, bn, count; |
69 Py_ssize_t len = 0, la, lb; | 69 Py_ssize_t len = 0, la, lb, li = 0, lcommon = 0, lmax; |
70 PyThreadState *_save; | 70 PyThreadState *_save; |
71 | 71 |
72 l.next = NULL; | 72 l.next = NULL; |
73 | 73 |
74 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) | 74 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
78 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); | 78 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); |
79 return NULL; | 79 return NULL; |
80 } | 80 } |
81 | 81 |
82 _save = PyEval_SaveThread(); | 82 _save = PyEval_SaveThread(); |
83 an = bdiff_splitlines(sa, la, &al); | 83 |
84 bn = bdiff_splitlines(sb, lb, &bl); | 84 lmax = la > lb ? lb : la; |
85 for (ia = sa, ib = sb; | |
86 li < lmax && *ia == *ib; | |
87 ++li, ++ia, ++ib) | |
88 if (*ia == '\n') | |
89 lcommon = li + 1; | |
90 /* we can almost add: if (li == lmax) lcommon = li; */ | |
91 | |
92 an = bdiff_splitlines(sa + lcommon, la - lcommon, &al); | |
93 bn = bdiff_splitlines(sb + lcommon, lb - lcommon, &bl); | |
85 if (!al || !bl) | 94 if (!al || !bl) |
86 goto nomem; | 95 goto nomem; |
87 | 96 |
88 count = bdiff_diff(al, an, bl, bn, &l); | 97 count = bdiff_diff(al, an, bl, bn, &l); |
89 if (count < 0) | 98 if (count < 0) |
110 la = lb = 0; | 119 la = lb = 0; |
111 | 120 |
112 for (h = l.next; h; h = h->next) { | 121 for (h = l.next; h; h = h->next) { |
113 if (h->a1 != la || h->b1 != lb) { | 122 if (h->a1 != la || h->b1 != lb) { |
114 len = bl[h->b1].l - bl[lb].l; | 123 len = bl[h->b1].l - bl[lb].l; |
115 putbe32((uint32_t)(al[la].l - al->l), rb); | 124 putbe32((uint32_t)(al[la].l + lcommon - al->l), rb); |
116 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); | 125 putbe32((uint32_t)(al[h->a1].l + lcommon - al->l), rb + 4); |
117 putbe32((uint32_t)len, rb + 8); | 126 putbe32((uint32_t)len, rb + 8); |
118 memcpy(rb + 12, bl[lb].l, len); | 127 memcpy(rb + 12, bl[lb].l, len); |
119 rb += 12 + len; | 128 rb += 12 + len; |
120 } | 129 } |
121 la = h->a2; | 130 la = h->a2; |