Patchwork [2,of,2] patches: release the GIL while applying the patch

login
register
mail settings
Submitter Boris Feld
Date Feb. 6, 2018, 2:28 p.m.
Message ID <345f1c897538cd2b0a5a.1517927283@FB>
Download mbox | patch
Permalink /patch/27380/
State Accepted
Headers show

Comments

Boris Feld - Feb. 6, 2018, 2:28 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1517839431 -3600
#      Mon Feb 05 15:03:51 2018 +0100
# Node ID 345f1c897538cd2b0a5a2fd3952b880206fd9339
# Parent  eee9cca843bc557274945688ea52e16539d45e67
# EXP-Topic parallel-patching
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 345f1c897538
patches: release the GIL while applying the patch

This will allow multiple threads to apply patches at the same time.
Gregory Szorc - Feb. 6, 2018, 9:14 p.m.
On Tue, Feb 6, 2018 at 6:28 AM, Boris Feld <boris.feld@octobus.net> wrote:

> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1517839431 -3600
> #      Mon Feb 05 15:03:51 2018 +0100
> # Node ID 345f1c897538cd2b0a5a2fd3952b880206fd9339
> # Parent  eee9cca843bc557274945688ea52e16539d45e67
> # EXP-Topic parallel-patching
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r
> 345f1c897538
> patches: release the GIL while applying the patch
>
> This will allow multiple threads to apply patches at the same time.
>

Please use Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros, as that
is what the Python docs recommend. I'm not sure why we don't do this
consistently in our code today. IMO we should port existing code to these
macros.


>
> diff --git a/mercurial/cext/mpatch.c b/mercurial/cext/mpatch.c
> --- a/mercurial/cext/mpatch.c
> +++ b/mercurial/cext/mpatch.c
> @@ -77,6 +77,7 @@ patches(PyObject *self, PyObject *args)
>         int r = 0;
>         char *out;
>         Py_ssize_t len, outlen, inlen;
> +       PyThreadState *_save;
>
>         if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
>                 return NULL;
> @@ -110,7 +111,9 @@ patches(PyObject *self, PyObject *args)
>                 goto cleanup;
>         }
>         out = PyBytes_AsString(result);
> +       _save = PyEval_SaveThread();
>         r = mpatch_apply(out, in, inlen, patch);
> +       PyEval_RestoreThread(_save);
>         if (r < 0) {
>                 Py_DECREF(result);
>                 result = NULL;
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>

Patch

diff --git a/mercurial/cext/mpatch.c b/mercurial/cext/mpatch.c
--- a/mercurial/cext/mpatch.c
+++ b/mercurial/cext/mpatch.c
@@ -77,6 +77,7 @@  patches(PyObject *self, PyObject *args)
 	int r = 0;
 	char *out;
 	Py_ssize_t len, outlen, inlen;
+	PyThreadState *_save;
 
 	if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
 		return NULL;
@@ -110,7 +111,9 @@  patches(PyObject *self, PyObject *args)
 		goto cleanup;
 	}
 	out = PyBytes_AsString(result);
+	_save = PyEval_SaveThread();
 	r = mpatch_apply(out, in, inlen, patch);
+	PyEval_RestoreThread(_save);
 	if (r < 0) {
 		Py_DECREF(result);
 		result = NULL;