-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking Issue for const-initialized thread locals #84223
Comments
I've requested this be tagged In benchmarks locally the call overhead of host->wasm decreases from 20ns to 18ns (10% reduction) with the usage of const-initialized-thread-locals (which are also Another data point I see linked here is that rustc is using There was some brief discussion on Zulip about the syntax when I originally asked to stabilize. One thing I'll clarify is that this doesn't literally use Overall the biggest thing to decide here I think is the syntax. One alternative is a whole new macro, but I think that it'd be ideal if we could keep it in one macro to not have to worry about multiple. Otherwise though I'm happy to implement whatever syntax. |
Discussed in the library api team meeting. We'd like @rust-lang/lang to sign off on this. |
This seems like a fine thing to stabilize; however, just to be clear, is this feature any use without |
This doesn't actually use |
It seems a little funky to me to stabilize a syntax that, at a surface level, matches that of a pre-existing unstable feature (const blocks). Even though it may not require const blocks under the hood, in the long-term I think users would expect that the set of things allowed inside That is, if we decided that some surface syntax or semantics should be different inside |
Oh, wait, maybe I misunderstood. I didn't realize that |
For reference, the actual macro is (trimmed down a bit): macro_rules! thread_local {
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => (
// ...
);
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
// ...
);
// ...
} The actual expanded code ends up doing: #[thread_local]
static mut VAL: $t = $init; I believe what @cramertj is describing is indeed a risk of using the Personally I'm fine implementing some other syntax if there's too many worries about using the |
@rfcbot fcp merge We discussed this in the @rust-lang/lang meeting today. Consensus was that we should move forward with stabilization here, so I am beginning an FCP. Here are our notes:
|
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:
Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
I don't understand how this would work. If the set of things allowed in |
If this happened I believe we could fix the inconsistency at an edition boundary the same way we did for |
@rfcbot concern LocalKey type A variable defined with Right now, reusing the same type works fine, as it just a wrapped function pointer. But I can imagine that a thread local key without lazy initializer might have a different implementation or even api than the one with. (E.g. |
@rust-lang/lang
If this happened in a way where more things were accepted by a static than by I wonder if we might indulge briefly in a thought experiment. If we knew that static items and |
With a concern about reusing |
I am concerned because this differs from the const block. #![feature(inline_const)]
#![feature(thread_local_const_init)]
use std::cell::Cell;
static V: u32 = 1;
std::thread_local!{
// This is accepted, because this isn't actually const.
static KEY: Cell<u32> = const { Cell::new(V) };
// This is not, because this is actually const, and const cannot refer to statics.
static KEY2: Cell<u32> = (const { Cell::new(V) });
} |
To what extent are the rules for evaluating const INIT: $t = $init;
#[thread_local]
static mut VAL: $t = INIT; There is still the more fundamental concern though about perhaps not wanting to use |
This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: rust-lang#84223 (comment)
@rfcbot resolve LocalKey type A The only thing to note is that if we do ever add a |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
std: Tweak expansion of thread-local const This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: rust-lang#84223 (comment)
std: Tweak expansion of thread-local const This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: rust-lang#84223 (comment)
If this is a pure performance optimization, can the macro just "do the right thing" without any user facing syntax? |
std: Tweak expansion of thread-local const This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: rust-lang#84223 (comment)
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
I've posted a stabilization PR for this at #91355 |
…-const, r=m-ou-se std: Stabilize the `thread_local_const_init` feature This commit is intended to follow the stabilization disposition of the FCP that has now finished in rust-lang#84223. This stabilizes the ability to flag thread local initializers as `const` expressions which enables the macro to generate more efficient code for accessing it, notably removing runtime checks for initialization. More information can also be found in rust-lang#84223 as well as the tests where the feature usage was removed in this PR. Closes rust-lang#84223
This has had a stabilization PR merged, but the todo list still lists the stabilization PR as missing. |
I'm also not able to find docs on this other than that the macro now lists these patterns in the declaration: https://doc.rust-lang.org/std/macro.thread_local.html (that is nothing says "and by the way..." but maybe I'm missing it?) Nor can i find anything in the release notes or via Google. The only thing that seems to exist as to this being stable is that the PR merged a long time ago. What's the actual status? I haven't tried to use this yet on stable but presumably I can? |
Cool! thanks. TLS doesn't come up super often but there was at least one case not that long ago where we were nightly for the attribute version of this in perf sensitive code. |
…d, r=thomcc Document `const {}` syntax for `std::thread_local`. It exists and is pretty cool. More people should use it. It was added in rust-lang#83416 and stabilized in rust-lang#91355 with the tracking issue rust-lang#84223.
Feature gate:
#![feature(thread_local_const_init)]
This is a tracking issue for the
const
-initialization of thread locals in the standard library added in #83416.Public API
The API here is intended to be exclusively within the syntax of the preexisting
thread_local!
macro. The initialization expression for thread local keys can now be surrounded withconst { ... }
to indicate that the value should be const-evaluated and initialized.Note that this also supports the multi-definition form:
Steps / History
Unresolved Questions
const
blocks, ok here? Are there other options for syntax?The text was updated successfully, but these errors were encountered: