Skip to content
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

gh-130373: Avoid locking in _LOAD_ATTR_WITH_HINT #130372

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DinoV
Copy link
Contributor

@DinoV DinoV commented Feb 20, 2025

@DinoV DinoV requested review from mpage and colesbury February 20, 2025 17:05
@DinoV DinoV changed the title gh-xxxx: Avoid locking in _LOAD_ATTR_WITH_HINT gh-130373: Avoid locking in _LOAD_ATTR_WITH_HINT Feb 20, 2025
POP_INPUT(dict);
DEOPT_IF(true);
}
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dk) + hint;
if (ep->me_key != name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the read of ep->me_key needs to be atomic (can be relaxed).

Comment on lines 2314 to 2317
} else if (dk != FT_ATOMIC_LOAD_PTR(dict->ma_keys)) {
PyStackRef_CLOSE(attr);
POP_INPUT(dict);
DEOPT_IF(true);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this check dk != FT_ATOMIC_LOAD_PTR(dict->ma_keys). Even if the dict->ma_keys changed, we should still have loaded a valid previous value at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems right to me, I think that means we also don't need the check here? https://github.com/python/cpython/blob/main/Objects/dictobject.c#L1527

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. There's also a similar check a few lines below that in the same function:

cpython/Objects/dictobject.c

Lines 1547 to 1550 in 69426fc

if (dk != _Py_atomic_load_ptr(&mp->ma_keys)) {
Py_DECREF(value);
goto read_failed;
}

@@ -2286,38 +2286,40 @@ dummy_func(
}

op(_LOAD_ATTR_WITH_HINT, (hint/1, owner, dict: PyDictObject * -- attr)) {
PyDictKeysObject *dk = FT_ATOMIC_LOAD_PTR(dict->ma_keys);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we need to check the equivalent of ensure_shared_on_read either here or in _CHECK_ATTR_WITH_HINT.

@DinoV DinoV force-pushed the nolock_loadattr_with_hint branch 3 times, most recently from 163dd7b to 6f8b1d7 Compare February 20, 2025 21:13
@DinoV DinoV force-pushed the nolock_loadattr_with_hint branch from 6f8b1d7 to 15c28cb Compare February 20, 2025 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants