Skip to content

Commit 7ead4ee

Browse files
committed
requested changes
1 parent 24d4f7a commit 7ead4ee

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

Modules/_functoolsmodule.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pycore_long.h" // _PyLong_GetZero()
55
#include "pycore_moduleobject.h" // _PyModule_GetState()
66
#include "pycore_object.h" // _PyObject_GC_TRACK
7+
#include "pycore_pyatomic_ft_wrappers.h"
78
#include "pycore_pystate.h" // _PyThreadState_GET()
89
#include "pycore_tuple.h" // _PyTuple_ITEMS()
910

@@ -1175,7 +1176,11 @@ uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
11751176
{
11761177
PyObject *result;
11771178

1179+
#ifdef Py_GIL_DISABLED
1180+
_Py_atomic_add_ssize(&self->misses, 1);
1181+
#else
11781182
self->misses++;
1183+
#endif
11791184
result = PyObject_Call(self->func, args, kwds);
11801185
if (!result)
11811186
return NULL;
@@ -1197,15 +1202,23 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
11971202
}
11981203
int res = _PyDict_GetItemRef_KnownHash((PyDictObject *)self->cache, key, hash, &result);
11991204
if (res > 0) {
1205+
#ifdef Py_GIL_DISABLED
1206+
_Py_atomic_add_ssize(&self->hits, 1);
1207+
#else
12001208
self->hits++;
1209+
#endif
12011210
Py_DECREF(key);
12021211
return result;
12031212
}
12041213
if (res < 0) {
12051214
Py_DECREF(key);
12061215
return NULL;
12071216
}
1217+
#ifdef Py_GIL_DISABLED
1218+
_Py_atomic_add_ssize(&self->misses, 1);
1219+
#else
12081220
self->misses++;
1221+
#endif
12091222
result = PyObject_Call(self->func, args, kwds);
12101223
if (!result) {
12111224
Py_DECREF(key);
@@ -1281,8 +1294,8 @@ lru_cache_prepend_link(lru_cache_object *self, lru_list_elem *link)
12811294
*/
12821295

12831296
static int
1284-
bounded_lru_cache_wrapper_pre_call_lock_held(lru_cache_object *self, PyObject *args, PyObject *kwds,
1285-
PyObject **result, PyObject **key, Py_hash_t *hash)
1297+
bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject *kwds,
1298+
PyObject **result, PyObject **key, Py_hash_t *hash)
12861299
{
12871300
lru_list_elem *link;
12881301

@@ -1299,7 +1312,11 @@ bounded_lru_cache_wrapper_pre_call_lock_held(lru_cache_object *self, PyObject *a
12991312
lru_cache_extract_link(link);
13001313
lru_cache_append_link(self, link);
13011314
*result = link->result;
1315+
#ifdef Py_GIL_DISABLED
1316+
_Py_atomic_add_ssize(&self->hits, 1);
1317+
#else
13021318
self->hits++;
1319+
#endif
13031320
Py_INCREF(link->result);
13041321
Py_DECREF(key_);
13051322
return 1;
@@ -1308,12 +1325,16 @@ bounded_lru_cache_wrapper_pre_call_lock_held(lru_cache_object *self, PyObject *a
13081325
Py_DECREF(key_);
13091326
return -1;
13101327
}
1328+
#ifdef Py_GIL_DISABLED
1329+
_Py_atomic_add_ssize(&self->misses, 1);
1330+
#else
13111331
self->misses++;
1332+
#endif
13121333
return 0;
13131334
}
13141335

13151336
static PyObject *
1316-
bounded_lru_cache_wrapper_post_call_lock_held(lru_cache_object *self,
1337+
bounded_lru_cache_update_lock_held(lru_cache_object *self,
13171338
PyObject *result, PyObject *key, Py_hash_t hash)
13181339
{
13191340
lru_list_elem *link;
@@ -1462,7 +1483,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
14621483
int res;
14631484

14641485
Py_BEGIN_CRITICAL_SECTION(self);
1465-
res = bounded_lru_cache_wrapper_pre_call_lock_held(self, args, kwds, &result, &key, &hash);
1486+
res = bounded_lru_cache_get_lock_held(self, args, kwds, &result, &key, &hash);
14661487
Py_END_CRITICAL_SECTION();
14671488

14681489
if (res < 0) {
@@ -1475,7 +1496,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
14751496
result = PyObject_Call(self->func, args, kwds);
14761497

14771498
Py_BEGIN_CRITICAL_SECTION(self);
1478-
result = bounded_lru_cache_wrapper_post_call_lock_held(self, result, key, hash);
1499+
result = bounded_lru_cache_update_lock_held(self, result, key, hash);
14791500
Py_END_CRITICAL_SECTION();
14801501

14811502
return result;
@@ -1640,11 +1661,15 @@ _functools__lru_cache_wrapper_cache_info_impl(PyObject *self)
16401661
lru_cache_object *_self = (lru_cache_object *) self;
16411662
if (_self->maxsize == -1) {
16421663
return PyObject_CallFunction(_self->cache_info_type, "nnOn",
1643-
_self->hits, _self->misses, Py_None,
1664+
FT_ATOMIC_LOAD_SSIZE_RELAXED(_self->hits),
1665+
FT_ATOMIC_LOAD_SSIZE_RELAXED(_self->misses),
1666+
Py_None,
16441667
PyDict_GET_SIZE(_self->cache));
16451668
}
16461669
return PyObject_CallFunction(_self->cache_info_type, "nnnn",
1647-
_self->hits, _self->misses, _self->maxsize,
1670+
FT_ATOMIC_LOAD_SSIZE_RELAXED(_self->hits),
1671+
FT_ATOMIC_LOAD_SSIZE_RELAXED(_self->misses),
1672+
_self->maxsize,
16481673
PyDict_GET_SIZE(_self->cache));
16491674
}
16501675

@@ -1661,7 +1686,8 @@ _functools__lru_cache_wrapper_cache_clear_impl(PyObject *self)
16611686
{
16621687
lru_cache_object *_self = (lru_cache_object *) self;
16631688
lru_list_elem *list = lru_cache_unlink_list(_self);
1664-
_self->hits = _self->misses = 0;
1689+
FT_ATOMIC_STORE_SSIZE_RELAXED(_self->hits, 0);
1690+
FT_ATOMIC_STORE_SSIZE_RELAXED(_self->misses, 0);
16651691
PyDict_Clear(_self->cache);
16661692
lru_cache_clear_list(list);
16671693
Py_RETURN_NONE;

0 commit comments

Comments
 (0)