Skip to content

Add lint that suggest using const thread_local whenever possible #12015

Closed
@SUPERCILEX

Description

@SUPERCILEX

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()) };
}

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions