-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Avoid initialization checks when accessing thread locals #44025
Copy link
Copy link
Closed
Labels
A-thread-localsArea: Thread local storage (TLS)Area: Thread local storage (TLS)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-thread-localsArea: Thread local storage (TLS)Area: Thread local storage (TLS)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Thread locals are often slower than they need to be. For example, if we have:
Every access of the form
FOO.with(|foo| ...)will first check whetherFOOis initialized, and then initialize it if this is the first access. On some (most?) platforms we can statically initialize thread locals with a constant expression (in this case the constant expression is777).A check on every access can be fairly costly, and we should try to avoid checks whenever possible. It is possible to avoid them by using
#[thread_local]instead ofthread_local!, but that is an unstable feature without a clear stabilization plan...In #17954 @alexcrichton said:
@eddyb answered:
@arielb1 suggests:
@alexcrichton adds:
My question after all that would be:
Can we perhaps make the
thread_local!macro expand to a special lang item that provides two implementations - one for the lazy initialization case and one for the static initialization zero-checks case? Then the compiler would choose one of them, depending on whether the initialization expression is a constant expression.