##// END OF EJS Templates
setdiscovery: make progress on most connected groups each roundtrip...
setdiscovery: make progress on most connected groups each roundtrip Consider history like this: o | o | | | o | | | o |/ o | o | | | o | | | o |/ o | o | | | o | | | o |/ o ~ Assume the left mainline is available in the remote repo and the other commits are only in the local repo. Also imagine that instead of 3 local branches with 3 commits on each, there are 1000 branches (the number of commits on each doesn't matter much here). In such a scenario, the current setdiscovery code will pick a sample size of 200 among these branches and ask the remote which of them it has. However, the discovery for each such branch is completely independent of the discovery for the others -- knowing whether the remote has a commit in one branch doesn't give us any information about the other branches. The discovery will therefore take at least 5 roundtrips (maybe more depending on which commit in each linear chain was sampled). Since the discovery for each branch is independent, there is no reason to let one branch wait for another, so this patch makes it so we sample at least as many commits as there are branches. It may still happen (it's very likely, even) that we get multiple samples from one branch and none from another, but that will even out over a few rounds and I think this is still a big improvement. Because of http header size limits, we still use the old behavior unless experimental.httppostargs=true. I've timed this by running `hg debugdiscovery mozilla-unified --debug` in the mozilla-try repo. Both repos were local. Before this patch, last part of the output was: 2249 total queries in 5276.4859s elapsed time: 5276.652634 seconds heads summary: total common heads: 13 also local heads: 4 also remote heads: 8 both: 4 local heads: 28317 common: 4 missing: 28313 remote heads: 12 common: 8 unknown: 4 local changesets: 2014901 common: 530373 missing: 1484528 common heads: 1dad417c28ad 4a108e94d3e2 4d7ef530fffb 5350524bb654 777e60ca8853 7d97fafba271 9cd2ab4d0029 a55ce37217da d38398e5144e dcc6d7a0dc00 e09297892ada e24ec6070d7b fd559328eaf3 After this patch, the output was (including all the samples, since there were so few now): taking initial sample query 2; still undecided: 1599476, sample size is: 108195 sampling from both directions query 3; still undecided: 810922, sample size is: 194158 sampling from both directions query 4; still undecided: 325882, sample size is: 137302 sampling from both directions query 5; still undecided: 111459, sample size is: 74586 sampling from both directions query 6; still undecided: 26805, sample size is: 23960 sampling from both directions query 7; still undecided: 2549, sample size is: 2528 sampling from both directions query 8; still undecided: 21, sample size is: 21 8 total queries in 24.5064s elapsed time: 24.670051 seconds heads summary: total common heads: 13 also local heads: 4 also remote heads: 8 both: 4 local heads: 28317 common: 4 missing: 28313 remote heads: 12 common: 8 unknown: 4 local changesets: 2014901 common: 530373 missing: 1484528 common heads: 1dad417c28ad 4a108e94d3e2 4d7ef530fffb 5350524bb654 777e60ca8853 7d97fafba271 9cd2ab4d0029 a55ce37217da d38398e5144e dcc6d7a0dc00 e09297892ada e24ec6070d7b fd559328eaf3 Differential Revision: https://phab.mercurial-scm.org/D2647

File last commit:

r41367:763b45bc default
r42594:5b34972a default
Show More
mpatch.c
215 lines | 4.7 KiB | text/x-c | CLexer
Yuya Nishihara
mpatch: switch to policy importer
r32371 /*
mpatch.c - efficient binary patching for Mercurial
This implements a patch algorithm that's O(m + nlog n) where m is the
size of the output and n is the number of patches.
Given a list of binary patches, it unpacks each into a hunk list,
then combines the hunk lists with a treewise recursion to form a
single hunk list. This hunk list is then applied to the original
text.
The text (or binary) fragments are copied directly from their source
Python objects into a preallocated output string to avoid the
allocation of intermediate Python objects. Working memory is about 2x
the total number of hunks.
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.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdlib.h>
#include <string.h>
#include "bitmanipulation.h"
#include "compat.h"
#include "mpatch.h"
Gregory Szorc
cext: reorder #include...
r34439 #include "util.h"
Yuya Nishihara
mpatch: switch to policy importer
r32371
static char mpatch_doc[] = "Efficient binary patching.";
static PyObject *mpatch_Error;
static void setpyerr(int r)
{
switch (r) {
case MPATCH_ERR_NO_MEM:
PyErr_NoMemory();
break;
case MPATCH_ERR_CANNOT_BE_DECODED:
PyErr_SetString(mpatch_Error, "patch cannot be decoded");
break;
case MPATCH_ERR_INVALID_PATCH:
PyErr_SetString(mpatch_Error, "invalid patch");
break;
}
}
struct mpatch_flist *cpygetitem(void *bins, ssize_t pos)
{
Gregory Szorc
cext: use modern buffer protocol in mpatch_flist()...
r40028 Py_buffer buffer;
struct mpatch_flist *res = NULL;
Yuya Nishihara
mpatch: switch to policy importer
r32371 int r;
Augie Fackler
mpatch: allow clang-format oversight...
r36245 PyObject *tmp = PyList_GetItem((PyObject *)bins, pos);
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!tmp) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
if (PyObject_GetBuffer(tmp, &buffer, PyBUF_CONTIG_RO)) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Gregory Szorc
cext: use modern buffer protocol in mpatch_flist()...
r40028 if ((r = mpatch_decode(buffer.buf, buffer.len, &res)) < 0) {
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!PyErr_Occurred()) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 setpyerr(r);
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Gregory Szorc
cext: use modern buffer protocol in mpatch_flist()...
r40028 res = NULL;
Yuya Nishihara
mpatch: switch to policy importer
r32371 }
Gregory Szorc
cext: use modern buffer protocol in mpatch_flist()...
r40028
PyBuffer_Release(&buffer);
Yuya Nishihara
mpatch: switch to policy importer
r32371 return res;
}
Augie Fackler
mpatch: allow clang-format oversight...
r36245 static PyObject *patches(PyObject *self, PyObject *args)
Yuya Nishihara
mpatch: switch to policy importer
r32371 {
PyObject *text, *bins, *result;
struct mpatch_flist *patch;
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 Py_buffer buffer;
Yuya Nishihara
mpatch: switch to policy importer
r32371 int r = 0;
char *out;
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 Py_ssize_t len, outlen;
Yuya Nishihara
mpatch: switch to policy importer
r32371
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins)) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Yuya Nishihara
mpatch: switch to policy importer
r32371
len = PyList_Size(bins);
if (!len) {
/* nothing to do */
Py_INCREF(text);
return text;
}
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 if (PyObject_GetBuffer(text, &buffer, PyBUF_CONTIG_RO)) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 }
Yuya Nishihara
mpatch: switch to policy importer
r32371
patch = mpatch_fold(bins, cpygetitem, 0, len);
if (!patch) { /* error already set or memory error */
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!PyErr_Occurred()) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 PyErr_NoMemory();
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 result = NULL;
goto cleanup;
Yuya Nishihara
mpatch: switch to policy importer
r32371 }
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 outlen = mpatch_calcsize(buffer.len, patch);
Yuya Nishihara
mpatch: switch to policy importer
r32371 if (outlen < 0) {
r = (int)outlen;
result = NULL;
goto cleanup;
}
result = PyBytes_FromStringAndSize(NULL, outlen);
if (!result) {
result = NULL;
goto cleanup;
}
out = PyBytes_AsString(result);
Boris Feld
patches: release the GIL while applying the patch...
r36381 /* clang-format off */
{
Py_BEGIN_ALLOW_THREADS
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 r = mpatch_apply(out, buffer.buf, buffer.len, patch);
Boris Feld
patches: release the GIL while applying the patch...
r36381 Py_END_ALLOW_THREADS
}
/* clang-format on */
Boris Feld
patches: move assignment outside the conditional...
r35959 if (r < 0) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 Py_DECREF(result);
result = NULL;
}
cleanup:
mpatch_lfree(patch);
Gregory Szorc
cext: use modern buffer protocol in patches()...
r40027 PyBuffer_Release(&buffer);
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!result && !PyErr_Occurred()) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 setpyerr(r);
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Yuya Nishihara
mpatch: switch to policy importer
r32371 return result;
}
/* calculate size of a patched file directly */
Augie Fackler
mpatch: allow clang-format oversight...
r36245 static PyObject *patchedsize(PyObject *self, PyObject *args)
Yuya Nishihara
mpatch: switch to policy importer
r32371 {
long orig, start, end, len, outlen = 0, last = 0, pos = 0;
Py_ssize_t patchlen;
char *bin;
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!PyArg_ParseTuple(args, PY23("ls#", "ly#"), &orig, &bin,
&patchlen)) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Yuya Nishihara
mpatch: switch to policy importer
r32371
while (pos >= 0 && pos < patchlen) {
start = getbe32(bin + pos);
end = getbe32(bin + pos + 4);
len = getbe32(bin + pos + 8);
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (start > end) {
Yuya Nishihara
mpatch: switch to policy importer
r32371 break; /* sanity check */
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Yuya Nishihara
mpatch: switch to policy importer
r32371 pos += 12 + len;
outlen += start - last;
last = end;
outlen += len;
}
if (pos != patchlen) {
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 if (!PyErr_Occurred()) {
Augie Fackler
mpatch: allow clang-format oversight...
r36245 PyErr_SetString(mpatch_Error,
"patch cannot be decoded");
Augie Fackler
cleanup: use clang-tidy to add missing {} around one-line statements...
r41367 }
Yuya Nishihara
mpatch: switch to policy importer
r32371 return NULL;
}
outlen += orig - last;
return Py_BuildValue("l", outlen);
}
static PyMethodDef methods[] = {
Augie Fackler
mpatch: allow clang-format oversight...
r36245 {"patches", patches, METH_VARARGS, "apply a series of patches\n"},
{"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"},
{NULL, NULL},
Yuya Nishihara
mpatch: switch to policy importer
r32371 };
static const int version = 1;
#ifdef IS_PY3K
static struct PyModuleDef mpatch_module = {
Augie Fackler
mpatch: allow clang-format oversight...
r36245 PyModuleDef_HEAD_INIT, "mpatch", mpatch_doc, -1, methods,
Yuya Nishihara
mpatch: switch to policy importer
r32371 };
PyMODINIT_FUNC PyInit_mpatch(void)
{
PyObject *m;
m = PyModule_Create(&mpatch_module);
if (m == NULL)
return NULL;
Augie Fackler
mpatch: allow clang-format oversight...
r36245 mpatch_Error =
PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL);
Yuya Nishihara
mpatch: switch to policy importer
r32371 Py_INCREF(mpatch_Error);
PyModule_AddObject(m, "mpatchError", mpatch_Error);
PyModule_AddIntConstant(m, "version", version);
return m;
}
#else
Augie Fackler
mpatch: allow clang-format oversight...
r36245 PyMODINIT_FUNC initmpatch(void)
Yuya Nishihara
mpatch: switch to policy importer
r32371 {
PyObject *m;
m = Py_InitModule3("mpatch", methods, mpatch_doc);
Augie Fackler
mpatch: allow clang-format oversight...
r36245 mpatch_Error =
PyErr_NewException("mercurial.cext.mpatch.mpatchError", NULL, NULL);
Yuya Nishihara
mpatch: switch to policy importer
r32371 PyModule_AddIntConstant(m, "version", version);
}
#endif