Patchwork [3,of,3] bdiff: split bdiff into cpy-aware and cpy-agnostic part

login
register
mail settings
Submitter Maciej Fijalkowski
Date July 14, 2016, 11:08 a.m.
Message ID <b85d11bb4cd07b65f64d.1468494482@brick.arcode.com>
Download mbox | patch
Permalink /patch/15853/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Maciej Fijalkowski - July 14, 2016, 11:08 a.m.
# HG changeset patch
# User Maciej Fijalkowski <fijall@gmail.com>
# Date 1468399586 -7200
#      Wed Jul 13 10:46:26 2016 +0200
# Node ID b85d11bb4cd07b65f64d843686cb6b526753a455
# Parent  9d57b790d7bc89dce7fab396c237fe7d6e1a4ade
bdiff: split bdiff into cpy-aware and cpy-agnostic part
Augie Fackler - July 15, 2016, 2:31 p.m.
On Thu, Jul 14, 2016 at 01:08:02PM +0200, Maciej Fijalkowski wrote:
> # HG changeset patch
> # User Maciej Fijalkowski <fijall@gmail.com>
> # Date 1468399586 -7200
> #      Wed Jul 13 10:46:26 2016 +0200
> # Node ID b85d11bb4cd07b65f64d843686cb6b526753a455
> # Parent  9d57b790d7bc89dce7fab396c237fe7d6e1a4ade
> bdiff: split bdiff into cpy-aware and cpy-agnostic part

Queued these, thanks!

>
> diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff.c
> --- a/mercurial/bdiff.c	Wed Jul 13 10:07:17 2016 +0200
> +++ b/mercurial/bdiff.c	Wed Jul 13 10:46:26 2016 +0200
> @@ -9,33 +9,19 @@
>   Based roughly on Python difflib
>  */
>
> -#define PY_SSIZE_T_CLEAN
> -#include <Python.h>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <limits.h>
>
>  #include "compat.h"
> -#include "util.h"
>  #include "bitmanipulation.h"
> -
> -struct bdiff_line {
> -	int hash, n, e;
> -	ssize_t len;
> -	const char *l;
> -};
> +#include "bdiff.h"
>
>  struct pos {
>       int pos, len;
>  };
>
> -struct bdiff_hunk;
> -struct bdiff_hunk {
> -	int a1, a2, b1, b2;
> -	struct bdiff_hunk *next;
> -};
> -
> -static int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
> +int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
>  {
>       unsigned hash;
>       int i;
> @@ -244,7 +230,7 @@
>       }
>  }
>
> -static int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
> +int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
>               int bn, struct bdiff_hunk *base)
>  {
>       struct bdiff_hunk *curr;
> @@ -298,7 +284,7 @@
>       return count;
>  }
>
> -static void bdiff_freehunks(struct bdiff_hunk *l)
> +void bdiff_freehunks(struct bdiff_hunk *l)
>  {
>       struct bdiff_hunk *n;
>       for (; l; l = n) {
> @@ -307,186 +293,4 @@
>       }
>  }
>
> -static PyObject *blocks(PyObject *self, PyObject *args)
> -{
> -	PyObject *sa, *sb, *rl = NULL, *m;
> -	struct bdiff_line *a, *b;
> -	struct bdiff_hunk l, *h;
> -	int an, bn, count, pos = 0;
>
> -	l.next = NULL;
> -
> -	if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
> -		return NULL;
> -
> -	an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
> -	bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
> -
> -	if (!a || !b)
> -		goto nomem;
> -
> -	count = bdiff_diff(a, an, b, bn, &l);
> -	if (count < 0)
> -		goto nomem;
> -
> -	rl = PyList_New(count);
> -	if (!rl)
> -		goto nomem;
> -
> -	for (h = l.next; h; h = h->next) {
> -		m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
> -		PyList_SetItem(rl, pos, m);
> -		pos++;
> -	}
> -
> -nomem:
> -	free(a);
> -	free(b);
> -	bdiff_freehunks(l.next);
> -	return rl ? rl : PyErr_NoMemory();
> -}
> -
> -static PyObject *bdiff(PyObject *self, PyObject *args)
> -{
> -	char *sa, *sb, *rb;
> -	PyObject *result = NULL;
> -	struct bdiff_line *al, *bl;
> -	struct bdiff_hunk l, *h;
> -	int an, bn, count;
> -	Py_ssize_t len = 0, la, lb;
> -	PyThreadState *_save;
> -
> -	l.next = NULL;
> -
> -	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
> -		return NULL;
> -
> -	if (la > UINT_MAX || lb > UINT_MAX) {
> -		PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
> -		return NULL;
> -	}
> -
> -	_save = PyEval_SaveThread();
> -	an = bdiff_splitlines(sa, la, &al);
> -	bn = bdiff_splitlines(sb, lb, &bl);
> -	if (!al || !bl)
> -		goto nomem;
> -
> -	count = bdiff_diff(al, an, bl, bn, &l);
> -	if (count < 0)
> -		goto nomem;
> -
> -	/* calculate length of output */
> -	la = lb = 0;
> -	for (h = l.next; h; h = h->next) {
> -		if (h->a1 != la || h->b1 != lb)
> -			len += 12 + bl[h->b1].l - bl[lb].l;
> -		la = h->a2;
> -		lb = h->b2;
> -	}
> -	PyEval_RestoreThread(_save);
> -	_save = NULL;
> -
> -	result = PyBytes_FromStringAndSize(NULL, len);
> -
> -	if (!result)
> -		goto nomem;
> -
> -	/* build binary patch */
> -	rb = PyBytes_AsString(result);
> -	la = lb = 0;
> -
> -	for (h = l.next; h; h = h->next) {
> -		if (h->a1 != la || h->b1 != lb) {
> -			len = bl[h->b1].l - bl[lb].l;
> -			putbe32((uint32_t)(al[la].l - al->l), rb);
> -			putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
> -			putbe32((uint32_t)len, rb + 8);
> -			memcpy(rb + 12, bl[lb].l, len);
> -			rb += 12 + len;
> -		}
> -		la = h->a2;
> -		lb = h->b2;
> -	}
> -
> -nomem:
> -	if (_save)
> -		PyEval_RestoreThread(_save);
> -	free(al);
> -	free(bl);
> -	bdiff_freehunks(l.next);
> -	return result ? result : PyErr_NoMemory();
> -}
> -
> -/*
> - * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
> - * reduce whitespace sequences to a single space and trim remaining whitespace
> - * from end of lines.
> - */
> -static PyObject *fixws(PyObject *self, PyObject *args)
> -{
> -	PyObject *s, *result = NULL;
> -	char allws, c;
> -	const char *r;
> -	Py_ssize_t i, rlen, wlen = 0;
> -	char *w;
> -
> -	if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
> -		return NULL;
> -	r = PyBytes_AsString(s);
> -	rlen = PyBytes_Size(s);
> -
> -	w = (char *)malloc(rlen ? rlen : 1);
> -	if (!w)
> -		goto nomem;
> -
> -	for (i = 0; i != rlen; i++) {
> -		c = r[i];
> -		if (c == ' ' || c == '\t' || c == '\r') {
> -			if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
> -				w[wlen++] = ' ';
> -		} else if (c == '\n' && !allws
> -                       && wlen > 0 && w[wlen - 1] == ' ') {
> -			w[wlen - 1] = '\n';
> -		} else {
> -			w[wlen++] = c;
> -		}
> -	}
> -
> -	result = PyBytes_FromStringAndSize(w, wlen);
> -
> -nomem:
> -	free(w);
> -	return result ? result : PyErr_NoMemory();
> -}
> -
> -
> -static char mdiff_doc[] = "Efficient binary diff.";
> -
> -static PyMethodDef methods[] = {
> -	{"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
> -	{"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
> -	{"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
> -	{NULL, NULL}
> -};
> -
> -#ifdef IS_PY3K
> -static struct PyModuleDef bdiff_module = {
> -	PyModuleDef_HEAD_INIT,
> -	"bdiff",
> -	mdiff_doc,
> -	-1,
> -	methods
> -};
> -
> -PyMODINIT_FUNC PyInit_bdiff(void)
> -{
> -	return PyModule_Create(&bdiff_module);
> -}
> -#else
> -PyMODINIT_FUNC initbdiff(void)
> -{
> -	Py_InitModule3("bdiff", methods, mdiff_doc);
> -}
> -#endif
> -
> diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff.h
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/mercurial/bdiff.h	Wed Jul 13 10:46:26 2016 +0200
> @@ -0,0 +1,21 @@
> +#ifndef _HG_BDIFF_H_
> +#define _HG_BDIFF_H_
> +
> +struct bdiff_line {
> +	int hash, n, e;
> +	ssize_t len;
> +	const char *l;
> +};
> +
> +struct bdiff_hunk;
> +struct bdiff_hunk {
> +	int a1, a2, b1, b2;
> +	struct bdiff_hunk *next;
> +};
> +
> +int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr);
> +int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b, int bn,
> +	struct bdiff_hunk *base);
> +void bdiff_freehunks(struct bdiff_hunk *l);
> +
> +#endif
> diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff_module.c
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/mercurial/bdiff_module.c	Wed Jul 13 10:46:26 2016 +0200
> @@ -0,0 +1,203 @@
> +/*
> + bdiff.c - efficient binary diff extension for Mercurial
> +
> + Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
> +
> + This software may be used and distributed according to the terms of
> + the GNU General Public License, incorporated herein by reference.
> +
> + Based roughly on Python difflib
> +*/
> +
> +#define PY_SSIZE_T_CLEAN
> +#include <Python.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <limits.h>
> +
> +#include "bdiff.h"
> +#include "bitmanipulation.h"
> +
> +
> +static PyObject *blocks(PyObject *self, PyObject *args)
> +{
> +	PyObject *sa, *sb, *rl = NULL, *m;
> +	struct bdiff_line *a, *b;
> +	struct bdiff_hunk l, *h;
> +	int an, bn, count, pos = 0;
> +
> +	l.next = NULL;
> +
> +	if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
> +		return NULL;
> +
> +	an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
> +	bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
> +
> +	if (!a || !b)
> +		goto nomem;
> +
> +	count = bdiff_diff(a, an, b, bn, &l);
> +	if (count < 0)
> +		goto nomem;
> +
> +	rl = PyList_New(count);
> +	if (!rl)
> +		goto nomem;
> +
> +	for (h = l.next; h; h = h->next) {
> +		m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
> +		PyList_SetItem(rl, pos, m);
> +		pos++;
> +	}
> +
> +nomem:
> +	free(a);
> +	free(b);
> +	bdiff_freehunks(l.next);
> +	return rl ? rl : PyErr_NoMemory();
> +}
> +
> +static PyObject *bdiff(PyObject *self, PyObject *args)
> +{
> +	char *sa, *sb, *rb;
> +	PyObject *result = NULL;
> +	struct bdiff_line *al, *bl;
> +	struct bdiff_hunk l, *h;
> +	int an, bn, count;
> +	Py_ssize_t len = 0, la, lb;
> +	PyThreadState *_save;
> +
> +	l.next = NULL;
> +
> +	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
> +		return NULL;
> +
> +	if (la > UINT_MAX || lb > UINT_MAX) {
> +		PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
> +		return NULL;
> +	}
> +
> +	_save = PyEval_SaveThread();
> +	an = bdiff_splitlines(sa, la, &al);
> +	bn = bdiff_splitlines(sb, lb, &bl);
> +	if (!al || !bl)
> +		goto nomem;
> +
> +	count = bdiff_diff(al, an, bl, bn, &l);
> +	if (count < 0)
> +		goto nomem;
> +
> +	/* calculate length of output */
> +	la = lb = 0;
> +	for (h = l.next; h; h = h->next) {
> +		if (h->a1 != la || h->b1 != lb)
> +			len += 12 + bl[h->b1].l - bl[lb].l;
> +		la = h->a2;
> +		lb = h->b2;
> +	}
> +	PyEval_RestoreThread(_save);
> +	_save = NULL;
> +
> +	result = PyBytes_FromStringAndSize(NULL, len);
> +
> +	if (!result)
> +		goto nomem;
> +
> +	/* build binary patch */
> +	rb = PyBytes_AsString(result);
> +	la = lb = 0;
> +
> +	for (h = l.next; h; h = h->next) {
> +		if (h->a1 != la || h->b1 != lb) {
> +			len = bl[h->b1].l - bl[lb].l;
> +			putbe32((uint32_t)(al[la].l - al->l), rb);
> +			putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
> +			putbe32((uint32_t)len, rb + 8);
> +			memcpy(rb + 12, bl[lb].l, len);
> +			rb += 12 + len;
> +		}
> +		la = h->a2;
> +		lb = h->b2;
> +	}
> +
> +nomem:
> +	if (_save)
> +		PyEval_RestoreThread(_save);
> +	free(al);
> +	free(bl);
> +	bdiff_freehunks(l.next);
> +	return result ? result : PyErr_NoMemory();
> +}
> +
> +/*
> + * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
> + * reduce whitespace sequences to a single space and trim remaining whitespace
> + * from end of lines.
> + */
> +static PyObject *fixws(PyObject *self, PyObject *args)
> +{
> +	PyObject *s, *result = NULL;
> +	char allws, c;
> +	const char *r;
> +	Py_ssize_t i, rlen, wlen = 0;
> +	char *w;
> +
> +	if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
> +		return NULL;
> +	r = PyBytes_AsString(s);
> +	rlen = PyBytes_Size(s);
> +
> +	w = (char *)malloc(rlen ? rlen : 1);
> +	if (!w)
> +		goto nomem;
> +
> +	for (i = 0; i != rlen; i++) {
> +		c = r[i];
> +		if (c == ' ' || c == '\t' || c == '\r') {
> +			if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
> +				w[wlen++] = ' ';
> +		} else if (c == '\n' && !allws
> +                       && wlen > 0 && w[wlen - 1] == ' ') {
> +			w[wlen - 1] = '\n';
> +		} else {
> +			w[wlen++] = c;
> +		}
> +	}
> +
> +	result = PyBytes_FromStringAndSize(w, wlen);
> +
> +nomem:
> +	free(w);
> +	return result ? result : PyErr_NoMemory();
> +}
> +
> +
> +static char mdiff_doc[] = "Efficient binary diff.";
> +
> +static PyMethodDef methods[] = {
> +	{"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
> +	{"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
> +	{"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
> +	{NULL, NULL}
> +};
> +
> +#ifdef IS_PY3K
> +static struct PyModuleDef bdiff_module = {
> +	PyModuleDef_HEAD_INIT,
> +	"bdiff",
> +	mdiff_doc,
> +	-1,
> +	methods
> +};
> +
> +PyMODINIT_FUNC PyInit_bdiff(void)
> +{
> +	return PyModule_Create(&bdiff_module);
> +}
> +#else
> +PyMODINIT_FUNC initbdiff(void)
> +{
> +	Py_InitModule3("bdiff", methods, mdiff_doc);
> +}
> +#endif
> diff -r 9d57b790d7bc -r b85d11bb4cd0 setup.py
> --- a/setup.py	Wed Jul 13 10:07:17 2016 +0200
> +++ b/setup.py	Wed Jul 13 10:46:26 2016 +0200
> @@ -556,8 +556,9 @@
>  extmodules = [
>      Extension('mercurial.base85', ['mercurial/base85.c'],
>                depends=common_depends),
> -    Extension('mercurial.bdiff', ['mercurial/bdiff.c'],
> -              depends=common_depends),
> +    Extension('mercurial.bdiff', ['mercurial/bdiff.c',
> +                                  'mercurial/bdiff_module.c'],
> +              depends=common_depends + ['mercurial/bdiff.h']),
>      Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c'],
>                depends=common_depends),
>      Extension('mercurial.mpatch', ['mercurial/mpatch.c'],
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Yuya Nishihara - July 15, 2016, 2:45 p.m.
On Thu, 14 Jul 2016 13:08:02 +0200, Maciej Fijalkowski wrote:
> # HG changeset patch
> # User Maciej Fijalkowski <fijall@gmail.com>
> # Date 1468399586 -7200
> #      Wed Jul 13 10:46:26 2016 +0200
> # Node ID b85d11bb4cd07b65f64d843686cb6b526753a455
> # Parent  9d57b790d7bc89dce7fab396c237fe7d6e1a4ade
> bdiff: split bdiff into cpy-aware and cpy-agnostic part

(I assumed this was new version of mdiff series, but I was wrong. :)

The series looks good to me, but I guess this patch loses copy
information from bdiff.c to bdiff_module.c.

Can you resend with diff.git=True ?

https://www.selenic.com/mercurial/hgrc.5.html#diff

Patch

diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff.c
--- a/mercurial/bdiff.c	Wed Jul 13 10:07:17 2016 +0200
+++ b/mercurial/bdiff.c	Wed Jul 13 10:46:26 2016 +0200
@@ -9,33 +9,19 @@ 
  Based roughly on Python difflib
 */
 
-#define PY_SSIZE_T_CLEAN
-#include <Python.h>
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
 
 #include "compat.h"
-#include "util.h"
 #include "bitmanipulation.h"
-
-struct bdiff_line {
-	int hash, n, e;
-	ssize_t len;
-	const char *l;
-};
+#include "bdiff.h"
 
 struct pos {
 	int pos, len;
 };
 
-struct bdiff_hunk;
-struct bdiff_hunk {
-	int a1, a2, b1, b2;
-	struct bdiff_hunk *next;
-};
-
-static int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
+int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
 {
 	unsigned hash;
 	int i;
@@ -244,7 +230,7 @@ 
 	}
 }
 
-static int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
+int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
 		int bn, struct bdiff_hunk *base)
 {
 	struct bdiff_hunk *curr;
@@ -298,7 +284,7 @@ 
 	return count;
 }
 
-static void bdiff_freehunks(struct bdiff_hunk *l)
+void bdiff_freehunks(struct bdiff_hunk *l)
 {
 	struct bdiff_hunk *n;
 	for (; l; l = n) {
@@ -307,186 +293,4 @@ 
 	}
 }
 
-static PyObject *blocks(PyObject *self, PyObject *args)
-{
-	PyObject *sa, *sb, *rl = NULL, *m;
-	struct bdiff_line *a, *b;
-	struct bdiff_hunk l, *h;
-	int an, bn, count, pos = 0;
 
-	l.next = NULL;
-
-	if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
-		return NULL;
-
-	an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
-	bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
-
-	if (!a || !b)
-		goto nomem;
-
-	count = bdiff_diff(a, an, b, bn, &l);
-	if (count < 0)
-		goto nomem;
-
-	rl = PyList_New(count);
-	if (!rl)
-		goto nomem;
-
-	for (h = l.next; h; h = h->next) {
-		m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
-		PyList_SetItem(rl, pos, m);
-		pos++;
-	}
-
-nomem:
-	free(a);
-	free(b);
-	bdiff_freehunks(l.next);
-	return rl ? rl : PyErr_NoMemory();
-}
-
-static PyObject *bdiff(PyObject *self, PyObject *args)
-{
-	char *sa, *sb, *rb;
-	PyObject *result = NULL;
-	struct bdiff_line *al, *bl;
-	struct bdiff_hunk l, *h;
-	int an, bn, count;
-	Py_ssize_t len = 0, la, lb;
-	PyThreadState *_save;
-
-	l.next = NULL;
-
-	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
-		return NULL;
-
-	if (la > UINT_MAX || lb > UINT_MAX) {
-		PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
-		return NULL;
-	}
-
-	_save = PyEval_SaveThread();
-	an = bdiff_splitlines(sa, la, &al);
-	bn = bdiff_splitlines(sb, lb, &bl);
-	if (!al || !bl)
-		goto nomem;
-
-	count = bdiff_diff(al, an, bl, bn, &l);
-	if (count < 0)
-		goto nomem;
-
-	/* calculate length of output */
-	la = lb = 0;
-	for (h = l.next; h; h = h->next) {
-		if (h->a1 != la || h->b1 != lb)
-			len += 12 + bl[h->b1].l - bl[lb].l;
-		la = h->a2;
-		lb = h->b2;
-	}
-	PyEval_RestoreThread(_save);
-	_save = NULL;
-
-	result = PyBytes_FromStringAndSize(NULL, len);
-
-	if (!result)
-		goto nomem;
-
-	/* build binary patch */
-	rb = PyBytes_AsString(result);
-	la = lb = 0;
-
-	for (h = l.next; h; h = h->next) {
-		if (h->a1 != la || h->b1 != lb) {
-			len = bl[h->b1].l - bl[lb].l;
-			putbe32((uint32_t)(al[la].l - al->l), rb);
-			putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
-			putbe32((uint32_t)len, rb + 8);
-			memcpy(rb + 12, bl[lb].l, len);
-			rb += 12 + len;
-		}
-		la = h->a2;
-		lb = h->b2;
-	}
-
-nomem:
-	if (_save)
-		PyEval_RestoreThread(_save);
-	free(al);
-	free(bl);
-	bdiff_freehunks(l.next);
-	return result ? result : PyErr_NoMemory();
-}
-
-/*
- * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
- * reduce whitespace sequences to a single space and trim remaining whitespace
- * from end of lines.
- */
-static PyObject *fixws(PyObject *self, PyObject *args)
-{
-	PyObject *s, *result = NULL;
-	char allws, c;
-	const char *r;
-	Py_ssize_t i, rlen, wlen = 0;
-	char *w;
-
-	if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
-		return NULL;
-	r = PyBytes_AsString(s);
-	rlen = PyBytes_Size(s);
-
-	w = (char *)malloc(rlen ? rlen : 1);
-	if (!w)
-		goto nomem;
-
-	for (i = 0; i != rlen; i++) {
-		c = r[i];
-		if (c == ' ' || c == '\t' || c == '\r') {
-			if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
-				w[wlen++] = ' ';
-		} else if (c == '\n' && !allws
-			  && wlen > 0 && w[wlen - 1] == ' ') {
-			w[wlen - 1] = '\n';
-		} else {
-			w[wlen++] = c;
-		}
-	}
-
-	result = PyBytes_FromStringAndSize(w, wlen);
-
-nomem:
-	free(w);
-	return result ? result : PyErr_NoMemory();
-}
-
-
-static char mdiff_doc[] = "Efficient binary diff.";
-
-static PyMethodDef methods[] = {
-	{"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
-	{"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
-	{"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
-	{NULL, NULL}
-};
-
-#ifdef IS_PY3K
-static struct PyModuleDef bdiff_module = {
-	PyModuleDef_HEAD_INIT,
-	"bdiff",
-	mdiff_doc,
-	-1,
-	methods
-};
-
-PyMODINIT_FUNC PyInit_bdiff(void)
-{
-	return PyModule_Create(&bdiff_module);
-}
-#else
-PyMODINIT_FUNC initbdiff(void)
-{
-	Py_InitModule3("bdiff", methods, mdiff_doc);
-}
-#endif
-
diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/bdiff.h	Wed Jul 13 10:46:26 2016 +0200
@@ -0,0 +1,21 @@ 
+#ifndef _HG_BDIFF_H_
+#define _HG_BDIFF_H_
+
+struct bdiff_line {
+	int hash, n, e;
+	ssize_t len;
+	const char *l;
+};
+
+struct bdiff_hunk;
+struct bdiff_hunk {
+	int a1, a2, b1, b2;
+	struct bdiff_hunk *next;
+};
+
+int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr);
+int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b, int bn,
+	struct bdiff_hunk *base);
+void bdiff_freehunks(struct bdiff_hunk *l);
+
+#endif
diff -r 9d57b790d7bc -r b85d11bb4cd0 mercurial/bdiff_module.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/bdiff_module.c	Wed Jul 13 10:46:26 2016 +0200
@@ -0,0 +1,203 @@ 
+/*
+ bdiff.c - efficient binary diff extension for Mercurial
+
+ Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
+
+ This software may be used and distributed according to the terms of
+ the GNU General Public License, incorporated herein by reference.
+
+ Based roughly on Python difflib
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#include "bdiff.h"
+#include "bitmanipulation.h"
+
+
+static PyObject *blocks(PyObject *self, PyObject *args)
+{
+	PyObject *sa, *sb, *rl = NULL, *m;
+	struct bdiff_line *a, *b;
+	struct bdiff_hunk l, *h;
+	int an, bn, count, pos = 0;
+
+	l.next = NULL;
+
+	if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
+		return NULL;
+
+	an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
+	bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
+
+	if (!a || !b)
+		goto nomem;
+
+	count = bdiff_diff(a, an, b, bn, &l);
+	if (count < 0)
+		goto nomem;
+
+	rl = PyList_New(count);
+	if (!rl)
+		goto nomem;
+
+	for (h = l.next; h; h = h->next) {
+		m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
+		PyList_SetItem(rl, pos, m);
+		pos++;
+	}
+
+nomem:
+	free(a);
+	free(b);
+	bdiff_freehunks(l.next);
+	return rl ? rl : PyErr_NoMemory();
+}
+
+static PyObject *bdiff(PyObject *self, PyObject *args)
+{
+	char *sa, *sb, *rb;
+	PyObject *result = NULL;
+	struct bdiff_line *al, *bl;
+	struct bdiff_hunk l, *h;
+	int an, bn, count;
+	Py_ssize_t len = 0, la, lb;
+	PyThreadState *_save;
+
+	l.next = NULL;
+
+	if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
+		return NULL;
+
+	if (la > UINT_MAX || lb > UINT_MAX) {
+		PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
+		return NULL;
+	}
+
+	_save = PyEval_SaveThread();
+	an = bdiff_splitlines(sa, la, &al);
+	bn = bdiff_splitlines(sb, lb, &bl);
+	if (!al || !bl)
+		goto nomem;
+
+	count = bdiff_diff(al, an, bl, bn, &l);
+	if (count < 0)
+		goto nomem;
+
+	/* calculate length of output */
+	la = lb = 0;
+	for (h = l.next; h; h = h->next) {
+		if (h->a1 != la || h->b1 != lb)
+			len += 12 + bl[h->b1].l - bl[lb].l;
+		la = h->a2;
+		lb = h->b2;
+	}
+	PyEval_RestoreThread(_save);
+	_save = NULL;
+
+	result = PyBytes_FromStringAndSize(NULL, len);
+
+	if (!result)
+		goto nomem;
+
+	/* build binary patch */
+	rb = PyBytes_AsString(result);
+	la = lb = 0;
+
+	for (h = l.next; h; h = h->next) {
+		if (h->a1 != la || h->b1 != lb) {
+			len = bl[h->b1].l - bl[lb].l;
+			putbe32((uint32_t)(al[la].l - al->l), rb);
+			putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
+			putbe32((uint32_t)len, rb + 8);
+			memcpy(rb + 12, bl[lb].l, len);
+			rb += 12 + len;
+		}
+		la = h->a2;
+		lb = h->b2;
+	}
+
+nomem:
+	if (_save)
+		PyEval_RestoreThread(_save);
+	free(al);
+	free(bl);
+	bdiff_freehunks(l.next);
+	return result ? result : PyErr_NoMemory();
+}
+
+/*
+ * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
+ * reduce whitespace sequences to a single space and trim remaining whitespace
+ * from end of lines.
+ */
+static PyObject *fixws(PyObject *self, PyObject *args)
+{
+	PyObject *s, *result = NULL;
+	char allws, c;
+	const char *r;
+	Py_ssize_t i, rlen, wlen = 0;
+	char *w;
+
+	if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
+		return NULL;
+	r = PyBytes_AsString(s);
+	rlen = PyBytes_Size(s);
+
+	w = (char *)malloc(rlen ? rlen : 1);
+	if (!w)
+		goto nomem;
+
+	for (i = 0; i != rlen; i++) {
+		c = r[i];
+		if (c == ' ' || c == '\t' || c == '\r') {
+			if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
+				w[wlen++] = ' ';
+		} else if (c == '\n' && !allws
+			  && wlen > 0 && w[wlen - 1] == ' ') {
+			w[wlen - 1] = '\n';
+		} else {
+			w[wlen++] = c;
+		}
+	}
+
+	result = PyBytes_FromStringAndSize(w, wlen);
+
+nomem:
+	free(w);
+	return result ? result : PyErr_NoMemory();
+}
+
+
+static char mdiff_doc[] = "Efficient binary diff.";
+
+static PyMethodDef methods[] = {
+	{"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
+	{"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
+	{"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
+	{NULL, NULL}
+};
+
+#ifdef IS_PY3K
+static struct PyModuleDef bdiff_module = {
+	PyModuleDef_HEAD_INIT,
+	"bdiff",
+	mdiff_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_bdiff(void)
+{
+	return PyModule_Create(&bdiff_module);
+}
+#else
+PyMODINIT_FUNC initbdiff(void)
+{
+	Py_InitModule3("bdiff", methods, mdiff_doc);
+}
+#endif
diff -r 9d57b790d7bc -r b85d11bb4cd0 setup.py
--- a/setup.py	Wed Jul 13 10:07:17 2016 +0200
+++ b/setup.py	Wed Jul 13 10:46:26 2016 +0200
@@ -556,8 +556,9 @@ 
 extmodules = [
     Extension('mercurial.base85', ['mercurial/base85.c'],
               depends=common_depends),
-    Extension('mercurial.bdiff', ['mercurial/bdiff.c'],
-              depends=common_depends),
+    Extension('mercurial.bdiff', ['mercurial/bdiff.c',
+                                  'mercurial/bdiff_module.c'],
+              depends=common_depends + ['mercurial/bdiff.h']),
     Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c'],
               depends=common_depends),
     Extension('mercurial.mpatch', ['mercurial/mpatch.c'],