Skip to content

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

Merged
merged 5 commits into from
Apr 21, 2021

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Apr 21, 2021

@rhettinger rhettinger added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
@bedevere-bot
Copy link

🤖 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.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
Copy link
Member

@vstinner vstinner left a 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.

cc @corona10 @shihai1991

@@ -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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@rhettinger rhettinger added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
@bedevere-bot
Copy link

🤖 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.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
state = get_functools_state_by_type(Py_TYPE(obj));
if (state == NULL) {
Py_DECREF(cachedict);
Py_DECREF(obj);
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rhettinger rhettinger added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
@bedevere-bot
Copy link

🤖 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.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 21, 2021
Copy link
Member

@vstinner vstinner left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants