-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
base: main
Are you sure you want to change the base?
Conversation
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.
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; | ||
} |
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.
Do we need early return here?
It looks like it will be same eventually.
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; | |
} |
Benchmarking script, based on issue. The reported numbers are the RSS (resident-set-size) of the process on 5 second intervals, in MB.
The last item shows about as good as we can expect to do by making the QSBR processing more aggressive at freeing. |
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.