GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-115103: Implement delayed free mechanism for free-threaded builds by colesbury · Pull Request #115367 · python/cpython

Conversation

@colesbury

@colesbury colesbury commented Feb 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This adds _PyMem_FreeDelayed() and supporting functions. The
_PyMem_FreeDelayed() function frees memory with the same allocator as
PyMem_Free(), but after some delay to ensure that concurrent lock-free
readers have finished.

  • Issue: Add delayed reclamation mechanism for free-threaded build (QSBR) #115103

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury
colesbury requested a review from DinoV February 12, 2024 20:47
@bedevere-app bedevere-app Bot mentioned this pull request Feb 12, 2024
Add delayed reclamation mechanism for free-threaded build (QSBR) #115103
Closed
@colesbury colesbury added skip news topic-free-threading labels Feb 12, 2024
@colesbury colesbury mentioned this pull request Feb 14, 2024
gh-115103: Implement delayed memory reclamation (QSBR) #115180
Merged
@colesbury
pythongh-115103: Implement delayed free mechanism for free-threaded b…
5e9f1dc
…uilds

This adds `_PyMem_FreeDelayed()` and supporting functions. The
`_PyMem_FreeDelayed()` function frees memory with the same allocator as
`PyMem_Free()`, but after some delay to ensure that concurrent lock-free
readers have finished.
@colesbury
colesbury force-pushed the gh-115103-delayed-free branch from a661366 to 5e9f1dc Compare February 16, 2024 20:35
@colesbury
colesbury marked this pull request as ready for review February 16, 2024 20:53
@colesbury
colesbury requested a review from ericsnowcurrently as a code owner February 16, 2024 20:53
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 16, 2024
DinoV
DinoV approved these changes Feb 16, 2024
View reviewed changes

@DinoV DinoV left a comment

Copy link
Copy Markdown
Contributor

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.

Looks pretty solid, let me know what you think of the comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/obmalloc.c
process_queue(struct llist_node *head, struct _qsbr_thread_state *qsbr)
{
while (!llist_empty(head)) {
struct _mem_work_chunk *buf = work_queue_first(head);

@DinoV DinoV Feb 16, 2024

Copy link
Copy Markdown
Contributor

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.

Is there a reason we'e not just doing something like:
for (Py_ssize_t i = buf->rd_idx; i < buf->wr_idx; i++) {
...
}

Or the while loop like _PyMem_FiniDelayed with the polling from here added in vs just going back through the head of the list each time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@colesbury colesbury Feb 17, 2024

Copy link
Copy Markdown
Contributor Author

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.

I changed the loop to avoid grabbing the head every iteration

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/obmalloc.c Outdated
struct _mem_work_chunk *buf = work_queue_first(head);

if (buf->rd_idx == buf->wr_idx) {
llist_remove(&buf->node);

@DinoV DinoV Feb 16, 2024

Copy link
Copy Markdown
Contributor

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.

It seems like we should wait for buf->rd_idx == WORK_ITEMS_PER_CHUNK before freeing and removing?

Otherwise it seems like we could end up in a situation where we're doing some delay frees, process them all, and then quickly need to do more delayed frees and end up re-allocating this 4k buffer repeatedly.

And if this is any buffer other than the last then buf->wr_idx == WORK_ITEMS_PER_CHUNK so we'd always free all but one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@colesbury colesbury Feb 17, 2024

Copy link
Copy Markdown
Contributor Author

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.

Yeah, that's a good idea. We can do that for the thread queues but not the interpreter queue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Feb 16, 2024
@colesbury
Keep last buffer in thread's queue to reduce allocations
0d12f6b
corona10
corona10 approved these changes Feb 17, 2024
View reviewed changes

@corona10 corona10 left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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!
(I 've taken a look at QSBR mechanism due to working on list allocation while making list thread-safe)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
DinoV
DinoV approved these changes Feb 20, 2024
View reviewed changes

@DinoV DinoV left a comment

Copy link
Copy Markdown
Contributor

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury
colesbury merged commit e3ad6ca into python:main Feb 20, 2024
@colesbury
colesbury deleted the gh-115103-delayed-free branch February 20, 2024 18:04
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 20, 2024
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
@colesbury @woodruffw
pythongh-115103: Implement delayed free mechanism for free-threaded b…
ad1ca5a
…uilds (python#115367)

This adds `_PyMem_FreeDelayed()` and supporting functions. The
`_PyMem_FreeDelayed()` function frees memory with the same allocator as
`PyMem_Free()`, but after some delay to ensure that concurrent lock-free
readers have finished.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@colesbury @diegorusso
pythongh-115103: Implement delayed free mechanism for free-threaded b…
b445298
…uilds (python#115367)

This adds `_PyMem_FreeDelayed()` and supporting functions. The
`_PyMem_FreeDelayed()` function frees memory with the same allocator as
`PyMem_Free()`, but after some delay to ensure that concurrent lock-free
readers have finished.
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull request Jan 22, 2025
@colesbury @LukasWoodtli
pythongh-115103: Implement delayed free mechanism for free-threaded b…
bdbaa84
…uilds (python#115367)

This adds `_PyMem_FreeDelayed()` and supporting functions. The
`_PyMem_FreeDelayed()` function frees memory with the same allocator as
`PyMem_Free()`, but after some delay to ensure that concurrent lock-free
readers have finished.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@DinoV DinoV DinoV approved these changes
@corona10 corona10 corona10 approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@colesbury @DinoV @corona10
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.