Skip to content

Commit

Permalink
Tag some more methods with METH_ARG_RANGE.
Browse files Browse the repository at this point in the history
Highlight:

django:
Min: 0.868683 -> 0.847636: 2.48% faster
Avg: 0.874128 -> 0.854678: 2.28% faster
Significant (t=23.333001, a=0.95)
Stddev: 0.00617 -> 0.00561: 9.95% smaller

Other benchmarks are performance-neutral.

Patch by Alex Gaynor!


git-svn-id: http://unladen-swallow.googlecode.com/svn/trunk@904 05521daa-c0b4-11dd-bb00-bd6ab96fe29a
  • Loading branch information
collinw committed Nov 20, 2009
1 parent 4193c0b commit 0b75e25
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
14 changes: 4 additions & 10 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,8 @@ PyDoc_STRVAR(throw_doc,
return next yielded value or raise StopIteration.");

static PyObject *
gen_throw(PyGenObject *gen, PyObject *args)
gen_throw(PyGenObject *gen, PyObject *typ, PyObject *val, PyObject *tb)
{
PyObject *typ;
PyObject *tb = NULL;
PyObject *val = NULL;

if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
return NULL;

/* First, check the traceback argument, replacing None with
NULL. */
if (tb == Py_None)
Expand Down Expand Up @@ -316,13 +309,14 @@ static PyGetSetDef gen_getsetlist[] = {
static PyMemberDef gen_memberlist[] = {
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO},
{"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO},
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), RO},
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), RO},
{NULL} /* Sentinel */
};

static PyMethodDef gen_methods[] = {
{"send",(PyCFunction)gen_send, METH_O, send_doc},
{"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
{"throw",(PyCFunction)gen_throw, METH_ARG_RANGE, throw_doc,
/*min_arity=*/1, /*max_arity=*/3},
{"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */
};
Expand Down
61 changes: 47 additions & 14 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6510,14 +6510,37 @@ a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
codecs.register_error that can handle UnicodeEncodeErrors.");

static PyObject *
unicode_encode(PyUnicodeObject *self, PyObject *args)
unicode_encode(PyUnicodeObject *self, PyObject *pyencoding, PyObject *pyerrors)
{
char *encoding = NULL;
char *errors = NULL;
PyObject *v;

if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
return NULL;

if (pyencoding != NULL) {
if (!(PyString_Check(pyencoding) || PyUnicode_Check(pyencoding))) {
PyErr_Format(PyExc_TypeError,
"encode() argument 1 must be string, not %.200s",
Py_TYPE(pyencoding)->tp_name);
return NULL;
}
if (PyUnicode_Check(pyencoding)) {
pyencoding = _PyUnicode_AsDefaultEncodedString(pyencoding, NULL);
}
encoding = PyString_AS_STRING(pyencoding);
}
if (pyerrors != NULL) {
if (!(PyString_Check(pyerrors) || PyUnicode_Check(pyerrors))) {
PyErr_Format(PyExc_TypeError,
"encode() argument 2 must be string, not %.200s",
Py_TYPE(pyerrors)->tp_name);
return NULL;
}
if (PyUnicode_Check(pyerrors)) {
pyerrors = _PyUnicode_AsDefaultEncodedString(pyerrors, NULL);
}
errors = PyString_AS_STRING(pyerrors);
}

v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors);
if (v == NULL)
goto onError;
Expand Down Expand Up @@ -7358,22 +7381,30 @@ old replaced by new. If the optional argument count is\n\
given, only the first count occurrences are replaced.");

static PyObject*
unicode_replace(PyUnicodeObject *self, PyObject *args)
unicode_replace(PyUnicodeObject *self, PyObject *old, PyObject *new,
PyObject *count)
{
PyUnicodeObject *str1;
PyUnicodeObject *str2;
Py_ssize_t maxcount = -1;
PyObject *result;

if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
return NULL;
str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);
str1 = (PyUnicodeObject *)PyUnicode_FromObject(old);
if (str1 == NULL)
return NULL;
str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);
return NULL;
str2 = (PyUnicodeObject *)PyUnicode_FromObject(new);
if (str2 == NULL) {
Py_DECREF(str1);
return NULL;
Py_DECREF(str1);
return NULL;
}
if (count != NULL) {
maxcount = PyInt_AsSsize_t(count);
if (maxcount == -1 && PyErr_Occurred()) {
Py_DECREF(str1);
Py_DECREF(str2);
PyErr_Format(PyExc_TypeError, "an integer is required");
return NULL;
}
}

result = replace(self, str1, str2, maxcount);
Expand Down Expand Up @@ -7954,8 +7985,10 @@ static PyMethodDef unicode_methods[] = {
/* Order is according to common usage: often used methods should
appear first, since lookup is done sequentially. */

{"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__},
{"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__},
{"encode", (PyCFunction) unicode_encode, METH_ARG_RANGE, encode__doc__,
/*min_arity=*/0, /*max_arity=*/2},
{"replace", (PyCFunction) unicode_replace, METH_ARG_RANGE, replace__doc__,
/*min_arity=*/2, /*max_arity=*/3},
{"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__},
{"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__},
{"join", (PyCFunction) unicode_join, METH_O, join__doc__},
Expand Down

0 comments on commit 0b75e25

Please sign in to comment.