Zero-Cost Statics in C++: Exploiting Linker Features
2025-07-19
This article tackles the performance overhead of static variable initialization in C++. Block-scoped static variables typically incur runtime costs, such as synchronization using `__cxa_guard_acquire`. The author proposes an optimization leveraging UNIX linker features: pre-allocating space in a dedicated section and performing initialization during global initialization. This eliminates runtime overhead, making block-scoped statics as efficient as file-scoped ones. The article details the implementation, including handling section attribute conflicts from inline functions and template members, ultimately achieving zero-cost optimization.
Read more
Development