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-112532: Improve mimalloc page visiting #114133

Merged
merged 1 commit into from
Jan 22, 2024
Merged

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jan 16, 2024

This adds support for visiting abandoned pages in mimalloc and improves the performance of the page visiting code. Abandoned pages contain memory blocks from threads that have exited. At some point, they may be later reclaimed by other threads. We still need to visit those pages in the free-threaded GC because they contain live objects.

This also reduces the overhead of visiting mimalloc pages:

  • Special cases for full, empty, and pages containing only a single block.
  • Fix free_map to use one bit instead of one byte per block.
  • Use fast integer division by a constant algorithm when computing block offset from block size and index.
  • Faster looping over bitmap.
  • Fix bug where blocks in the delayed-free list were erroneously visited as if they were live.

This adds support for visiting abandoned pages in mimalloc and improves
the performance of the page visiting code. Abandoned pages contain
memory blocks from threads that have exited. At some point, they may be
later reclaimed by other threads. We still need to visit those pages in
the free-threaded GC because they contain live objects.

This also reduces the overhead of visiting mimalloc pages:

 * Special cases for full, empty, and pages containing only a single
   block.
 * Fix free_map to use one bit instead of one byte per block.
 * Use fast integer division by a constant algorithm when computing
   block offset from block size and index.
Copy link
Contributor

@DinoV DinoV left a comment

Choose a reason for hiding this comment

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

Just curious about the unused function(s), otherwise LGTM!

}

// Visit all blocks in a abandoned segments
bool _mi_abandoned_pool_visit_blocks(mi_abandoned_pool_t* pool, uint8_t page_tag, bool visit_blocks, mi_block_visit_fun* visitor, void* arg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This (and therefore the previous 2 functions) doesn't seem to be used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These will be used in the upcoming GC PR. Here is an example usage:

https://github.com/colesbury/cpython/blob/8314c7c1d9d9670d4a83b9dc12f23611493c8eaa/Python/gc_free_threading.c#L226-L227

I put them in this PR because:

  1. Keeping the mimalloc changes separate makes them a bit easier to track and upstream
  2. The GC PR will be big and doing this first makes the upcoming PR a bit smaller

@colesbury
Copy link
Contributor Author

@DinoV, would you please merge this when you are ready?

@DinoV DinoV merged commit 412920a into python:main Jan 22, 2024
33 checks passed
@colesbury colesbury deleted the 112532-visitor branch January 22, 2024 21:14
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
This adds support for visiting abandoned pages in mimalloc and improves
the performance of the page visiting code. Abandoned pages contain
memory blocks from threads that have exited. At some point, they may be
later reclaimed by other threads. We still need to visit those pages in
the free-threaded GC because they contain live objects.

This also reduces the overhead of visiting mimalloc pages:

 * Special cases for full, empty, and pages containing only a single
   block.
 * Fix free_map to use one bit instead of one byte per block.
 * Use fast integer division by a constant algorithm when computing
   block offset from block size and index.
daanx added a commit to microsoft/mimalloc that referenced this pull request Jun 3, 2024
daanx added a commit to microsoft/mimalloc that referenced this pull request Jun 3, 2024
daanx added a commit to microsoft/mimalloc that referenced this pull request Jun 3, 2024
daanx added a commit to microsoft/mimalloc that referenced this pull request Jun 3, 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.

None yet

2 participants