Skip to content

Deduplicate minipal thread ID TLS cache. - #131991

Open
lateralusX wants to merge 3 commits into
dotnet:mainfrom
lateralusX:lateralusX/fix-minipal-tls-inflation
Open

Deduplicate minipal thread ID TLS cache.#131991
lateralusX wants to merge 3 commits into
dotnet:mainfrom
lateralusX:lateralusX/fix-minipal-tls-inflation

Conversation

@lateralusX

@lateralusX lateralusX commented Aug 7, 2026

Copy link
Copy Markdown
Member

Summary

Deduplicate the TLS cache used by minipal_get_current_thread_id.

Fixes #131954.

Root cause

minipal_get_current_thread_id previously declared its cached thread ID as a function-local static thread_local variable in thread.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_dynamic instead of _dl_tlsdesc_return, adding overhead to allocation, thread-static access, and other common runtime paths.

Changes

  • Move the cached thread ID into a single minipal compilation unit.
  • Externally declare the shared TLS variable from thread.h.
  • Use a common macro for the C and C++ TLS storage-class spellings.

Validation

A test with two independent C and C++ translation units showed:

  • Before: two local 8-byte TLS slots and a 16-byte TLS segment.
  • After: one shared 8-byte TLS slot.
  • GCC C++ emits no TLS dynamic-initialization relocation for the shared variable.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

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.

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_LOCAL and uses it to declare a shared TLS variable minipal_cached_thread_id in thread.h.
  • Adds thread.c to define minipal_cached_thread_id (under the same WASM/reentrancy guards).
  • Updates minipal’s CMake source list to compile thread.c on 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.

Comment thread src/native/minipal/thread.h Outdated
Copilot AI review requested due to automatic review settings August 7, 2026 10:45
@EgorBo

EgorBo commented Aug 7, 2026

Copy link
Copy Markdown
Member

@EgorBot -linux_arm64 --filter "System.Tests.Perf_UInt16.Parse*"

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/native/minipal/CMakeLists.txt
Copilot AI review requested due to automatic review settings August 7, 2026 11:04

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@AndyAyersMS

Copy link
Copy Markdown
Member

For some reason egorbot failed on main, but passed on the PR

Method Toolchain value Mean Error Ratio Allocated Alloc Ratio
Parse /main/corerun 0 NA NA ? NA ?
Parse /PR_131991/corerun 0 5.631 ns 0.0008 ns 1.00 - NA
Parse /main/corerun 12345 NA NA ? NA ?
Parse /PR_131991/corerun 12345 8.717 ns 0.0013 ns 1.00 - NA
Parse /main/corerun 65535 NA NA ? NA ?
Parse /PR_131991/corerun 65535 8.852 ns 0.0014 ns 1.00 - NA

PR results look similar to the fast "before" results we have from the lab

image image

*
* @return The current thread ID as a size_t value.
*/
static inline size_t minipal_get_current_thread_id(void)

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.

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.

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.

[Perf] Linux/arm64: 624 Regressions on 7/29/2026 2:14:39 AM +00:00

5 participants