New submission from pdox <p...@alum.mit.edu>:

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.

----------
components: Interpreter Core
messages: 304420
nosy: pdox
priority: normal
severity: normal
status: open
title: Ensure that all PyTypeObject fields are set to non-NULL defaults
type: performance
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31791>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to