##// END OF EJS Templates
bdiff: early pruning of common prefix before doing expensive computations...
Mads Kiilerich -
r30561:7c0c722d default
parent child Browse files
Show More
@@ -61,12 +61,12 b' nomem:'
61 61
62 62 static PyObject *bdiff(PyObject *self, PyObject *args)
63 63 {
64 char *sa, *sb, *rb;
64 char *sa, *sb, *rb, *ia, *ib;
65 65 PyObject *result = NULL;
66 66 struct bdiff_line *al, *bl;
67 67 struct bdiff_hunk l, *h;
68 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 70 PyThreadState *_save;
71 71
72 72 l.next = NULL;
@@ -80,8 +80,17 b' static PyObject *bdiff(PyObject *self, P'
80 80 }
81 81
82 82 _save = PyEval_SaveThread();
83 an = bdiff_splitlines(sa, la, &al);
84 bn = bdiff_splitlines(sb, lb, &bl);
83
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 94 if (!al || !bl)
86 95 goto nomem;
87 96
@@ -112,8 +121,8 b' static PyObject *bdiff(PyObject *self, P'
112 121 for (h = l.next; h; h = h->next) {
113 122 if (h->a1 != la || h->b1 != lb) {
114 123 len = bl[h->b1].l - bl[lb].l;
115 putbe32((uint32_t)(al[la].l - al->l), rb);
116 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
124 putbe32((uint32_t)(al[la].l + lcommon - al->l), rb);
125 putbe32((uint32_t)(al[h->a1].l + lcommon - al->l), rb + 4);
117 126 putbe32((uint32_t)len, rb + 8);
118 127 memcpy(rb + 12, bl[lb].l, len);
119 128 rb += 12 + len;
@@ -34,10 +34,9 b' showdiff('
34 34 showdiff(
35 35 'a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n',
36 36 'a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n'):
37 0 0 '' -> 'a\nb\nb\n'
38 'a\nb\nb\nb\nc\n.\n'
39 12 12 '' -> 'b\nc\n.\n'
40 'd\ne\n'
37 'a\nb\nb\n'
38 6 6 '' -> 'a\nb\nb\nb\nc\n.\n'
39 'b\nc\n.\nd\ne\n'
41 40 16 18 '.\n' -> ''
42 41 'f\n'
43 42 done
General Comments 0
You need to be logged in to leave comments. Login now