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-112087: Update list_get_item_ref to optimistically avoid locking #116353

Merged
merged 7 commits into from
Mar 5, 2024

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Mar 5, 2024

@corona10
Copy link
Member Author

corona10 commented Mar 5, 2024

!buildbot nogil

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @corona10 for commit eda29bc 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • AMD64 Ubuntu NoGIL Refleaks PR
  • x86-64 MacOS Intel ASAN NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • ARM64 MacOS M1 NoGIL PR

@corona10 corona10 marked this pull request as ready for review March 5, 2024 13:07
@corona10 corona10 requested a review from colesbury March 5, 2024 13:07
@corona10
Copy link
Member Author

corona10 commented Mar 5, 2024

!buildbot nogil

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @corona10 for commit 9071dd4 🤖

The command will test the builders whose names match following regular expression: nogil

The builders matched are:

  • AMD64 Ubuntu NoGIL Refleaks PR
  • x86-64 MacOS Intel ASAN NoGIL PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • x86-64 MacOS Intel NoGIL PR
  • AMD64 Windows Server 2022 NoGIL PR
  • AMD64 Ubuntu NoGIL PR
  • ARM64 MacOS M1 NoGIL PR

@corona10
Copy link
Member Author

corona10 commented Mar 5, 2024

@colesbury

IIUC, This PR should be the last PR for #112087
Please let me know if I missed some parts.

Objects/listobject.c Outdated Show resolved Hide resolved
if (ob_item == NULL) {
return NULL;
}
Py_ssize_t cap = _Py_atomic_load_ssize_relaxed(&op->allocated);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay for now, but we need to store the capacity at the start of the ob_item allocation in the free-threaded build.

Copy link
Member Author

Choose a reason for hiding this comment

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

We may need to create a issue for tracking this.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's necessary for thread-safety so I think it's covered by #112087

Copy link
Member Author

@corona10 corona10 Mar 5, 2024

Choose a reason for hiding this comment

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

By the way, we already store the op->allocated at the ob_item allocation.
So adding assertion will be enough?
assert(cap != -1 && cap >= size);

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We currently store it in the PyListObject. We also need to store it in the same memory allocation as the ob_item array, like a pre-header.

Here are some pointers to the relevant code in nogil-3.12:

The problem with the current code is that the list may be resized concurrently with the access. The bounds check may be stale. Putting the value of "allocated" as an immutable field avoids this problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I got it. I understood what you want to say. Let's handle it at a separate PR.

Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
@corona10 corona10 requested a review from colesbury March 5, 2024 15:34
Objects/listobject.c Outdated Show resolved Hide resolved
corona10 and others added 2 commits March 6, 2024 05:01
Co-authored-by: Sam Gross <colesbury@gmail.com>
@corona10 corona10 requested a review from colesbury March 5, 2024 20:04
Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

This looks good to me. I think it's best to address the capacity/allocated changes in a separate PR.

@corona10 corona10 merged commit d2f1b0e into python:main Mar 5, 2024
34 checks passed
@corona10 corona10 deleted the gh-112087-single-item-locked branch March 5, 2024 23:21
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
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.

3 participants