Message304420
Ensure that every function pointer in every PyTypeObject is set to a non-NULL default, to spare the cost of checking for NULL with every use. As a basic example, consider PyNumber_Negative:
PyObject *
PyNumber_Negative(PyObject *o)
{
PyNumberMethods *m;
if (o == NULL) {
return null_error();
}
m = o->ob_type->tp_as_number;
if (m && m->nb_negative)
return (*m->nb_negative)(o);
return type_error("bad operand type for unary -: '%.200s'", o);
}
If "tp_as_number" and "nb_negative" were always guaranteed non-NULL, then the function could omit the second if statement, and invoke the function pointer directly. To maintain the existing behavior, the default nb_negative function would be set to the following:
PyObject* nb_negative_default(PyObject *o) {
return type_error("bad operand type for unary -: '%.200s'", o);
}
This removes two NULL-checks from the PyNumber_Negative. Many other operators and builtins would be able to benefit in the same way. |
|
Date |
User |
Action |
Args |
2017-10-15 06:22:29 | pdox | set | recipients:
+ pdox |
2017-10-15 06:22:29 | pdox | set | messageid: <1508048549.34.0.213398074469.issue31791@psf.upfronthosting.co.za> |
2017-10-15 06:22:29 | pdox | link | issue31791 messages |
2017-10-15 06:22:28 | pdox | create | |
|