##// 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 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;
@@ -80,8 +80,17 b' static PyObject *bdiff(PyObject *self, P'
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
@@ -112,8 +121,8 b' static PyObject *bdiff(PyObject *self, P'
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;
@@ -34,10 +34,9 b' showdiff('
34 showdiff(
34 showdiff(
35 'a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n',
35 'a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n',
36 'a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n'):
36 'a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n'):
37 0 0 '' -> 'a\nb\nb\n'
37 'a\nb\nb\n'
38 'a\nb\nb\nb\nc\n.\n'
38 6 6 '' -> 'a\nb\nb\nb\nc\n.\n'
39 12 12 '' -> 'b\nc\n.\n'
39 'b\nc\n.\nd\ne\n'
40 'd\ne\n'
41 16 18 '.\n' -> ''
40 16 18 '.\n' -> ''
42 'f\n'
41 'f\n'
43 done
42 done
General Comments 0
You need to be logged in to leave comments. Login now