Hi, On 2023-06-08 16:47:48 +0300, Konstantin Knizhnik wrote: > Actually TLS not not more expensive then accessing struct fields (at least > at x86 platform), consider the following program:
It really depends on the OS and the architecture, not just the architecture. And even on x86-64 Linux, the fact that you're using the segment offset in the address calculation means you can't use the more complicated addressing modes for other reasons. And plenty instructions, e.g. most (?) SSE instructions, won't be able to use that kind of addressing directly. Even just compiling your, example you can see that with gcc -O2 you get considerably faster code with the non-TLS version. As a fairly extreme example, here's the mingw -O3 compiled code: use_struct: movq xmm1, QWORD PTR .LC0[rip] movq xmm0, QWORD PTR [rcx] add DWORD PTR 8[rcx], 1 paddd xmm0, xmm1 movq QWORD PTR [rcx], xmm0 ret use_tls: sub rsp, 40 lea rcx, __emutls_v.a[rip] call __emutls_get_address lea rcx, __emutls_v.b[rip] add DWORD PTR [rax], 1 call __emutls_get_address lea rcx, __emutls_v.c[rip] add DWORD PTR [rax], 1 call __emutls_get_address add DWORD PTR [rax], 1 add rsp, 40 ret Greetings, Andres Freund