# Aleksey Kladov - Fast Thread Locals in Rust (Highlights) ![rw-book-cover|256](https://readwise-assets.s3.amazonaws.com/static/images/article3.5c705a01b476.png) ## Metadata **Cover**:: https://readwise-assets.s3.amazonaws.com/static/images/article3.5c705a01b476.png **Source**:: #from/readwise **Zettel**:: #zettel/fleeting **Status**:: #x **Authors**:: [[Aleksey Kladov]] **Full Title**:: Fast Thread Locals in Rust **Category**:: #articles #readwise/articles **Category Icon**:: 📰 **URL**:: [matklad.github.io](https://matklad.github.io//2020/10/03/fast-thread-locals-in-rust.html) **Host**:: [[matklad.github.io]] **Highlighted**:: [[2020-10-11]] **Created**:: [[2022-09-26]] ## Highlights - Rust thread-locals are slower than they could be. - Rust’s thread-local implementation( [1](https://github.com/rust-lang/rust/blob/6f56fbdc1c58992a9db630f5cd2ba9882d32e84b/library/std/src/thread/local.rs#L156-L188), [2](https://github.com/rust-lang/rust/blob/6f56fbdc1c58992a9db630f5cd2ba9882d32e84b/library/std/src/thread/local.rs#L445-L459) ) comes with built-in support for laziness — thread locals are initialized on the first access. - There’s an unstable `#[thread_local]` attribute for a zero-cost implementation - Rust allows cross-language **L**ink **T**ime **O**ptimization ([docs](https://doc.rust-lang.org/rustc/linker-plugin-lto.html)). - The C compiler, the Rust compiler and the linker must use the same version of LLVM. - `-flto=thin` in the C compiler flags. - | ``` export RUSTFLAGS=\ "-Clinker-plugin-lto -Clinker=clang -Clink-arg=-fuse-ld=lld" ``` |