Skip to content

Increase performance of _config_dict_get_bool(PyObject *dict, const char *name, int *p_flag) by removing unused PY_DECREF(item) #145228

@benediktjohannes

Description

@benediktjohannes

In Python/interpconfig.c we use Py_DECREF(item); in line 118 here

static int
_config_dict_get_bool(PyObject *dict, const char *name, int *p_flag)
{
    PyObject *item;
    if (_config_dict_get(dict, name, &item) < 0) {
        return -1;
    }
    // For now we keep things strict, rather than using PyObject_IsTrue().
    int flag = item == Py_True;
    if (!flag && item != Py_False) {
        Py_DECREF(item);
        config_dict_invalid_type(name);
        return -1;
    }
    Py_DECREF(item);
    *p_flag = flag;
    return 0;
}

while it's not necessary here

    Py_DECREF(item);
    *p_flag = flag;
    return 0;

because we use if (!flag && item != Py_False) and if this returns true, then we return and only in the other case we come to

    Py_DECREF(item);
    *p_flag = flag;
    return 0;

which then means that

a. !flag is false which means that flag is true which means that item == PY_TRUE which doesn't have to be decremented
b. item == Py_FALSE which doesn't have to be decremented
c. both

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions