Skip to content

gh-133136: Limit excess memory held by QSBR #135107

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jun 3, 2025

The free threading build uses QSBR to delay the freeing of dictionary keys and list arrays when the objects are accessed by multiple threads in order to allow concurrent reads to proceeed with holding the object lock. The requests are processed in batches to reduce execution overhead, but for large memory blocks this can lead to excess memory usage.

Take into account the size of the memory block when deciding when to process QSBR requests.

The free threading build uses QSBR to delay the freeing of dictionary
keys and list arrays when the objects are accessed by multiple threads
in order to allow concurrent reads to proceeed with holding the object
lock. The requests are processed in batches to reduce execution
overhead, but for large memory blocks this can lead to excess memory
usage.

Take into account the size of the memory block when deciding when to
process QSBR requests.
Comment on lines +1146 to +1162
should_advance_qsbr(_PyThreadStateImpl *tstate, size_t size)
{
// If the deferred memory exceeds 1 MiB, we force an advance in the
// shared QSBR sequence number to limit excess memory usage.
static const size_t QSBR_DEFERRED_LIMIT = 1024 * 1024;
if (size > QSBR_DEFERRED_LIMIT) {
tstate->qsbr->memory_deferred = 0;
return 1;
}

tstate->qsbr->memory_deferred += size;
if (tstate->qsbr->memory_deferred > QSBR_DEFERRED_LIMIT) {
tstate->qsbr->memory_deferred = 0;
return 1;
}
return 0;
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need early return here?
It looks like it will be same eventually.

Suggested change
should_advance_qsbr(_PyThreadStateImpl *tstate, size_t size)
{
// If the deferred memory exceeds 1 MiB, we force an advance in the
// shared QSBR sequence number to limit excess memory usage.
static const size_t QSBR_DEFERRED_LIMIT = 1024 * 1024;
if (size > QSBR_DEFERRED_LIMIT) {
tstate->qsbr->memory_deferred = 0;
return 1;
}
tstate->qsbr->memory_deferred += size;
if (tstate->qsbr->memory_deferred > QSBR_DEFERRED_LIMIT) {
tstate->qsbr->memory_deferred = 0;
return 1;
}
return 0;
}
should_advance_qsbr(_PyThreadStateImpl *tstate, size_t size)
{
// If the deferred memory exceeds 1 MiB, we force an advance in the
// shared QSBR sequence number to limit excess memory usage.
static const size_t QSBR_DEFERRED_LIMIT = 1024 * 1024;
tstate->qsbr->memory_deferred += size;
if (tstate->qsbr->memory_deferred > QSBR_DEFERRED_LIMIT) {
tstate->qsbr->memory_deferred = 0;
return 1;
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy