Skip to content

Refactor representation of thread local state. - #751

Merged
Matthew Parkinson (mjp41) merged 8 commits into
microsoft:mainfrom
mjp41:thread_local_refactor
Mar 21, 2025
Merged

Matthew Parkinson (mjp41) merged 8 commits into
microsoft:mainfrom
mjp41:thread_local_refactor

Conversation

@mjp41

@mjp41 Matthew Parkinson (mjp41) commented Feb 20, 2025 •

Copy link
Copy Markdown
Member

Change thread local state to be a single pointer to a backend allocated structure. Previously, the TLS contained a complex structure, but that didn't interact well with loading numerous copies at once.

@mjp41
Matthew Parkinson (mjp41) force-pushed the thread_local_refactor branch 2 times, most recently from 318a880 to 0614a01 Compare February 20, 2025 13:52
@mjp41 Matthew Parkinson (mjp41) changed the title WIP: Refactor representation of local state. WIP: Refactor representation of thread local state. Feb 20, 2025
@mjp41
Matthew Parkinson (mjp41) force-pushed the thread_local_refactor branch 3 times, most recently from 80d24e0 to 0fd6768 Compare February 21, 2025 14:22
Comment thread src/snmalloc/global/globalalloc.h Outdated
Comment thread CMakeLists.txt Outdated
Comment thread src/snmalloc/global/libc.h Outdated
@nwf

Copy link
Copy Markdown
Contributor

Only found time to make it into "Restrict ThreadAlloc usage to globalalloc", but so far so great. Thanks for nwfifying the commit stack. :D

@mjp41
Matthew Parkinson (mjp41) force-pushed the thread_local_refactor branch 5 times, most recently from 41392d1 to 654bcfd Compare February 21, 2025 20:46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This continues to look really nice. (Read up through "WIP", exclusive.)

Comment thread src/snmalloc/mem/check_init.h Outdated
Comment thread src/snmalloc/mem/localalloc.h Outdated
return self->alloc_not_small<zero_mem, CheckInitDefault>(size);
},
size,
this);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's a bit of a pity that something so formulaic can't, AFAIK, be templated away.

Comment thread src/snmalloc/mem/freelist_queue.h
@mjp41

Copy link
Copy Markdown
Member Author

Nathaniel Wesley Filardo (@nwf) thanks for the comments. I've addressed them. And I landed the first set of commits in #750.

Comment thread src/snmalloc/mem/corealloc.h
@mjp41
Matthew Parkinson (mjp41) force-pushed the thread_local_refactor branch 2 times, most recently from 997eed4 to ab58e2f Compare February 27, 2025 16:25
@mjp41 Matthew Parkinson (mjp41) changed the title WIP: Refactor representation of thread local state. Refactor representation of thread local state. Mar 7, 2025
The check init code was tightly integrated into LocalAllocator.  This commit pull that code out into ThreadAlloc, and passes a template parameter into the remaining LocalAllocator to perform the relevant TLS manipulations.  This removes some of the awkward layering around register_clean_up.
Fully disable lotsofthreads test

Need to investigate if the test is unreliable, or we have actually
regressed perf.  A quick mimalloc-bench didn't show any regressions.
This introduces one additional branch on when processing a batch of messages, but it is likely to only be hit when a lot of messages are processed.
This combines the notion of CoreAlloc, LocalAlloc and LocalCache into a single class.  Previously, these were separated so that a more complex structure would be stored directly in the TLS.  This however, proved to be bad for compatibility if the allocator is part of the libc implementation.

This commit collapses all the stages of the allocator into a single class. This simplifies the sequencing and overall is a nice reduction in complexity.
@mjp41

Matthew Parkinson (mjp41) commented Mar 21, 2025 •

Copy link
Copy Markdown
Member Author

I have been through this carefully today, and am going to merge. There is one outstanding issue that I am not sure how we should modify the domestication test. There is complexity with how BatchIt interacts. I will raise an issue to fix this down the line, but this shouldn't block other work.

@mjp41
Matthew Parkinson (mjp41) merged commit dff1057 into microsoft:main Mar 21, 2025
@mjp41
Matthew Parkinson (mjp41) deleted the thread_local_refactor branch March 21, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants