From Jason Turner
Watch on YouTube

Summary

This episode explores the hidden performance costs associated with accessing static variables in C++. Jason demonstrates how static variable access in C++11 (with its thread-safe initialization guarantee) requires checking a guard variable on each access, which can significantly impact performance in tight loops. Using Compiler Explorer and Callgrind profiling, he compares direct static variable access against storing a reference to that static as a member variable, showing that caching the reference results in approximately 2.3x fewer instructions executed. The episode highlights how modern compilers can optimize away redundant static access patterns but may not eliminate the guard variable check overhead.

Resources

Tags