Skip to content
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

Add lint that suggest using const thread_local whenever possible #12015

Closed
SUPERCILEX opened this issue Dec 25, 2023 · 3 comments · Fixed by #12026
Closed

Add lint that suggest using const thread_local whenever possible #12015

SUPERCILEX opened this issue Dec 25, 2023 · 3 comments · Fixed by #12026
Assignees
Labels
A-lint Area: New lints

Comments

@SUPERCILEX
Copy link

SUPERCILEX commented Dec 25, 2023

What it does

Tells you to use const thread_locals because they offer many more performance opportunities due to not having to check for re-initialization on every call.

See this godbolt for the potential performance impact: https://godbolt.org/z/rnPsb6Y7v

Advantage

Performance

Drawbacks

None I think? Honestly I don't understand why this is even a feature, i.e. why the compiler can't do this automatically.

Example

thread_local! {
    static BUF: RefCell<String> = RefCell::new(String::new());
}

Could be written as:

thread_local! {
    static BUF: RefCell<String> = const { RefCell::new(String::new()) };
}
@SUPERCILEX SUPERCILEX added the A-lint Area: New lints label Dec 25, 2023
@SUPERCILEX
Copy link
Author

Quote from @thomcc (https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Stabilizing.20.60thread_local_const_init.60.3F/near/410023178):

Realistically, there are no downsides to using the const{} thread local init syntax.

So looks like we're good to make this a machine applicable lint and enable it by default.

@m-rph
Copy link
Contributor

m-rph commented Dec 26, 2023

@rustbot claim

@SUPERCILEX
Copy link
Author

That was fast, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants