FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133136: Limit excess memory held by QSBR by colesbury · Pull Request #135107 · python/cpython · GitHub

/ cpython Public

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

Closed
colesbury wants to merge 2 commits into
python:mainfrom
colesbury:gh-133136-qsbr-memory
Closed

gh-133136: Limit excess memory held by QSBR#135107
colesbury wants to merge 2 commits into
python:mainfrom
colesbury:gh-133136-qsbr-memory

Conversation

colesbury commented Jun 3, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

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 thread Objects/obmalloc.c
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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;
}

Copy link
Copy Markdown
Member

Benchmarking script, based on issue.
dict_mutate_qsbr_mem.py.txt

The reported numbers are the RSS (resident-set-size) of the process on 5 second intervals, in MB.

  • Running with the "main" branch, FT build (commit 1ffe913): 312, 543, 728, 912, 1142.

  • Running with the default build: 76, 76, 76, ....

  • Default build using mimalloc instead of pymalloc: 89, 90, 134, 134, 90.

  • This PR, which frees memory held by QSBR more quickly: 351, 374, 393, 484, 532.

  • Revised version of this PR, freeing even more quickly: 276, 297, 285, 260, 325, 281.

The last item shows about as good as we can expect to do by making the QSBR processing more aggressive at freeing.

Copy link
Copy Markdown
Member

This PR definitely helps reduce the memory held by QSBR. But, I think we might do better without adding a lot of runtime performance cost. I'm working on a revised version of this PR, currently running some benchmarks on it.

Copy link
Copy Markdown
Contributor Author

Closing in favor of #135473

colesbury closed this Jun 16, 2025
AA-Turner removed needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Aug 14, 2025
colesbury deleted the gh-133136-qsbr-memory branch February 5, 2026 17:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL