Deduplicate minipal thread ID TLS cache. - #131991
Conversation
Move the cached thread ID into a single minipal compilation unit instead of emitting one TLS slot per translation unit that includes thread.h.
|
Azure Pipelines: Successfully started running 4 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
There was a problem hiding this comment.
Pull request overview
Deduplicates the TLS-backed cache used by minipal_get_current_thread_id() by moving the cached thread ID out of a function-local TLS and into a single TLS variable defined in a dedicated minipal compilation unit.
Changes:
- Introduces
MINIPAL_THREAD_LOCALand uses it to declare a shared TLS variableminipal_cached_thread_idinthread.h. - Adds
thread.cto defineminipal_cached_thread_id(under the same WASM/reentrancy guards). - Updates minipal’s CMake source list to compile
thread.con Unix hosts.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/native/minipal/thread.h | Switches the cache from a function-local TLS static to an extern TLS variable and adds a TLS macro. |
| src/native/minipal/thread.c | Defines the shared TLS variable so all translation units refer to the same TLS slot. |
| src/native/minipal/CMakeLists.txt | Ensures thread.c is built into minipal on Unix. |
|
@EgorBot -linux_arm64 --filter "System.Tests.Perf_UInt16.Parse*" |
| * | ||
| * @return The current thread ID as a size_t value. | ||
| */ | ||
| static inline size_t minipal_get_current_thread_id(void) |
There was a problem hiding this comment.
Isn't the actual problem that this is static inline method? static means that each compilation unit gets its own copy of the code. Methods in headers should be inline, but not static - static methods in headers are a recipe to produce undesirable code duplication.


Summary
Deduplicate the TLS cache used by
minipal_get_current_thread_id.Fixes #131954.
Root cause
minipal_get_current_thread_idpreviously declared its cached thread ID as a function-localstatic thread_localvariable inthread.h.Because the function has internal linkage, each translation unit using it could emit a separate 8-byte TLS slot. Enabling the in-process crash reporter added another consumer, increasing
libcoreclr.so's TLS footprint enough to exceed glibc's optional static TLS allocation on Linux ARM64.This caused glibc to resolve CoreCLR TLS accesses through
_dl_tlsdesc_dynamicinstead of_dl_tlsdesc_return, adding overhead to allocation, thread-static access, and other common runtime paths.Changes
thread.h.Validation
A test with two independent C and C++ translation units showed: