Skip to content

Commit f7c2496

Browse files
committed
perf: convert update() from METH_VARARGS to METH_O
METH_O passes the single argument directly without tuple allocation. Also eliminates PyArg_ParseTuple overhead for the common single-arg case.
1 parent ebe5d45 commit f7c2496

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

src/_xxhash.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,12 @@ PyDoc_STRVAR(
574574
"Update the xxh32 object with the string data. Repeated calls are\n"
575575
"equivalent to a single call with the concatenation of all the arguments.");
576576

577-
static PyObject *PYXXH32_update(PYXXH32Object *self, PyObject *args)
577+
static PyObject *PYXXH32_update(PYXXH32Object *self, PyObject *arg)
578578
{
579579
Py_buffer buf;
580580

581-
buf.buf = buf.obj = NULL;
582-
583-
if (!PyArg_ParseTuple(args, "y*:update", &buf)) {
581+
if (PyObject_GetBuffer(arg, &buf, PyBUF_SIMPLE) < 0)
584582
return NULL;
585-
}
586583

587584
PYXXH32_do_update(self, &buf);
588585

@@ -685,7 +682,7 @@ static PyObject *PYXXH32_reset(PYXXH32Object *self)
685682
}
686683

687684
static PyMethodDef PYXXH32_methods[] = {
688-
{"update", (PyCFunction)PYXXH32_update, METH_VARARGS, PYXXH32_update_doc},
685+
{"update", (PyCFunction)PYXXH32_update, METH_O, PYXXH32_update_doc},
689686
{"digest", (PyCFunction)PYXXH32_digest, METH_NOARGS, PYXXH32_digest_doc},
690687
{"hexdigest", (PyCFunction)PYXXH32_hexdigest, METH_NOARGS, PYXXH32_hexdigest_doc},
691688
{"intdigest", (PyCFunction)PYXXH32_intdigest, METH_NOARGS, PYXXH32_intdigest_doc},
@@ -930,15 +927,12 @@ PyDoc_STRVAR(
930927
"Update the xxh64 object with the string data. Repeated calls are\n"
931928
"equivalent to a single call with the concatenation of all the arguments.");
932929

933-
static PyObject *PYXXH64_update(PYXXH64Object *self, PyObject *args)
930+
static PyObject *PYXXH64_update(PYXXH64Object *self, PyObject *arg)
934931
{
935932
Py_buffer buf;
936933

937-
buf.buf = buf.obj = NULL;
938-
939-
if (!PyArg_ParseTuple(args, "y*:update", &buf)) {
934+
if (PyObject_GetBuffer(arg, &buf, PyBUF_SIMPLE) < 0)
940935
return NULL;
941-
}
942936

943937
PYXXH64_do_update(self, &buf);
944938

@@ -1041,7 +1035,7 @@ static PyObject *PYXXH64_reset(PYXXH64Object *self)
10411035
}
10421036

10431037
static PyMethodDef PYXXH64_methods[] = {
1044-
{"update", (PyCFunction)PYXXH64_update, METH_VARARGS, PYXXH64_update_doc},
1038+
{"update", (PyCFunction)PYXXH64_update, METH_O, PYXXH64_update_doc},
10451039
{"digest", (PyCFunction)PYXXH64_digest, METH_NOARGS, PYXXH64_digest_doc},
10461040
{"hexdigest", (PyCFunction)PYXXH64_hexdigest, METH_NOARGS, PYXXH64_hexdigest_doc},
10471041
{"intdigest", (PyCFunction)PYXXH64_intdigest, METH_NOARGS, PYXXH64_intdigest_doc},
@@ -1285,15 +1279,12 @@ PyDoc_STRVAR(
12851279
"Update the xxh3_64 object with the string data. Repeated calls are\n"
12861280
"equivalent to a single call with the concatenation of all the arguments.");
12871281

1288-
static PyObject *PYXXH3_64_update(PYXXH3_64Object *self, PyObject *args)
1282+
static PyObject *PYXXH3_64_update(PYXXH3_64Object *self, PyObject *arg)
12891283
{
12901284
Py_buffer buf;
12911285

1292-
buf.buf = buf.obj = NULL;
1293-
1294-
if (!PyArg_ParseTuple(args, "y*:update", &buf)) {
1286+
if (PyObject_GetBuffer(arg, &buf, PyBUF_SIMPLE) < 0)
12951287
return NULL;
1296-
}
12971288

12981289
PYXXH3_64_do_update(self, &buf);
12991290

@@ -1404,7 +1395,7 @@ static PyObject *PYXXH3_64_reset(PYXXH3_64Object *self)
14041395
}
14051396

14061397
static PyMethodDef PYXXH3_64_methods[] = {
1407-
{"update", (PyCFunction)PYXXH3_64_update, METH_VARARGS, PYXXH3_64_update_doc},
1398+
{"update", (PyCFunction)PYXXH3_64_update, METH_O, PYXXH3_64_update_doc},
14081399
{"digest", (PyCFunction)PYXXH3_64_digest, METH_NOARGS, PYXXH3_64_digest_doc},
14091400
{"hexdigest", (PyCFunction)PYXXH3_64_hexdigest, METH_NOARGS, PYXXH3_64_hexdigest_doc},
14101401
{"intdigest", (PyCFunction)PYXXH3_64_intdigest, METH_NOARGS, PYXXH3_64_intdigest_doc},
@@ -1649,14 +1640,12 @@ PyDoc_STRVAR(
16491640
"Update the xxh3_128 object with the string data. Repeated calls are\n"
16501641
"equivalent to a single call with the concatenation of all the arguments.");
16511642

1652-
static PyObject *PYXXH3_128_update(PYXXH3_128Object *self, PyObject *args)
1643+
static PyObject *PYXXH3_128_update(PYXXH3_128Object *self, PyObject *arg)
16531644
{
16541645
Py_buffer buf;
1655-
buf.buf = buf.obj = NULL;
16561646

1657-
if (!PyArg_ParseTuple(args, "y*:update", &buf)) {
1647+
if (PyObject_GetBuffer(arg, &buf, PyBUF_SIMPLE) < 0)
16581648
return NULL;
1659-
}
16601649

16611650
PYXXH3_128_do_update(self, &buf);
16621651

@@ -1785,7 +1774,7 @@ static PyObject *PYXXH3_128_reset(PYXXH3_128Object *self)
17851774
}
17861775

17871776
static PyMethodDef PYXXH3_128_methods[] = {
1788-
{"update", (PyCFunction)PYXXH3_128_update, METH_VARARGS, PYXXH3_128_update_doc},
1777+
{"update", (PyCFunction)PYXXH3_128_update, METH_O, PYXXH3_128_update_doc},
17891778
{"digest", (PyCFunction)PYXXH3_128_digest, METH_NOARGS, PYXXH3_128_digest_doc},
17901779
{"hexdigest", (PyCFunction)PYXXH3_128_hexdigest, METH_NOARGS, PYXXH3_128_hexdigest_doc},
17911780
{"intdigest", (PyCFunction)PYXXH3_128_intdigest, METH_NOARGS, PYXXH3_128_intdigest_doc},

0 commit comments

Comments
 (0)