-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-40137: Move state lookups out of the critical path #25492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-40137: Move state lookups out of the critical path #25492
Conversation
🤖 New build scheduled with the buildbot fleet by @rhettinger for commit 58bc078 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's a cache, I think that adding two pointers to lru_cache_object structure is a reasonable CPU vs memory trade-off. Maybe we should reuse this approach in other performance critical code paths.
Modules/_functoolsmodule.c
Outdated
@@ -1203,6 +1205,8 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |||
obj->func = func; | |||
obj->misses = obj->hits = 0; | |||
obj->maxsize = maxsize; | |||
obj->kwd_mark = state->kwd_mark; // Borrowed | |||
obj->lru_list_elem_type = state->lru_list_elem_type; // Borrowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it possible to use a strong reference instead? Maybe the module instance can be deleted but the object survives longer. You need to update lru_cache_tp_traverse() and lru_cache_tp_clear() in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
🤖 New build scheduled with the buildbot fleet by @rhettinger for commit a1e622e 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Modules/_functoolsmodule.c
Outdated
state = get_functools_state_by_type(Py_TYPE(obj)); | ||
if (state == NULL) { | ||
Py_DECREF(cachedict); | ||
Py_DECREF(obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum. I am not sure if it's safe to call lru_cache_dealloc() here, obj is not fully initialized. For example, lru_cache_unlink_list() can crash, no? Maybe call get_functools_state_by_type(type) before creating obj?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
🤖 New build scheduled with the buildbot fleet by @rhettinger for commit 5a03e52 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks, it looks safe with strong references.
https://bugs.python.org/issue40137