Avoid decrefs by moving state lookup earlier in the function · python/cpython@5a03e52

Original file line numberDiff line numberDiff line change

@@ -1159,6 +1159,11 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)

11591159

return NULL;

11601160

}

11611161
1162+

state = get_functools_state_by_type(type);

1163+

if (state == NULL) {

1164+

return NULL;

1165+

}

1166+
11621167

/* select the caching function, and make/inc maxsize_O */

11631168

if (maxsize_O == Py_None) {

11641169

wrapper = infinite_lru_cache_wrapper;

@@ -1189,13 +1194,6 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)

11891194

return NULL;

11901195

}

11911196
1192-

state = get_functools_state_by_type(Py_TYPE(obj));

1193-

if (state == NULL) {

1194-

Py_DECREF(cachedict);

1195-

Py_DECREF(obj);

1196-

return NULL;

1197-

}

1198-
11991197

obj->root.prev = &obj->root;

12001198

obj->root.next = &obj->root;

12011199

obj->wrapper = wrapper;