Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #55299

Re: Any ideas on faster TLS ?

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: Any ideas on faster TLS ?
Date 2017-03-01 12:36 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2017Mar1.133612@mips.complang.tuwien.ac.at> (permalink)
References <o90deq$vvp$1@dont-email.me> <yuednW2Kf9GZZC7FnZ2dnUU7-KHNnZ2d@supernews.com> <o911tb$rfl$1@dont-email.me>

Show all headers | View raw


Ron Aaron <rambamist@gmail.com> writes:
>
>
>On 02/27/2017 12:01, Andrew Haley wrote:
>
>> This is a very baffling post.  TLS on x86/Linux generates one
>> instruction for a read of a __thread variable:
>> 
>>     mov    %fs:offset,%eax
>
>Indeed, it's only one instruction.  But it's a very expensive one.

The best way to find out how expensive is to isolate and measure it,
so I did that:

I compiled the following microbenchmark:

#include <stdio.h>
__thread long a[]={0};

int main()
{
  long l=0;
  long i;
  for (i=0; i<100000000; i++)
    l = a[l];
  printf("%ld\n",l);
  return 0;
}

The inner loop looks as follows:

With __thread:                   Without __thread:
   a: mov %fs:0x0(,%rsi,8),%rsi   a: mov 0x0(,%rsi,8),%rsi
  13: sub $0x1,%rax              12: sub $0x1,%rax
  17: jne a <main+0xa>           16: jne a <main+0xa>

  7 cycles/iteration (Haswell)   5 cycles/iteration (Haswell)
  4 cycles/iteration (K8)        3 cycles/iteration (K8)

This microbenchmark measures the latency of these loads in the first
line.  So the %fs: prefix costs 2 cycles, which is measurable, but not
very expensive.  The alternative is using a general-purpose register
for holding the value that %fs holds; the resulting inner-loop code is

  1a:  mov    (%rdx,%rsi,8),%rsi
  1e:  sub    $0x1,%rax
  22:  jne    1a <main+0x1a>

for this microbenchmark and uses 5 Haswell and 3 K8 cycles/iteration,
i.e., no extra cost.  However, in other cases the addition may cost an
extra instruction, and it costs a general-purpose register in any
case.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-02-27 07:30 +0200
  Re: Any ideas on faster TLS ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-02-27 04:01 -0600
    Re: Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-02-27 13:19 +0200
      Re: Any ideas on faster TLS ? Alex <alex@rivadpm.com> - 2017-02-27 12:56 +0000
        Re: Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-02-27 15:10 +0200
        Re: Any ideas on faster TLS ? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2017-02-27 17:08 +0100
      Re: Any ideas on faster TLS ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-02-27 08:06 -0600
        Re: Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-02-27 16:10 +0200
          Re: Any ideas on faster TLS ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-02-27 09:31 -0600
            Re: Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-02-28 07:05 +0200
            Re: Any ideas on faster TLS ? Paul Rubin <no.email@nospam.invalid> - 2017-02-27 23:15 -0800
              Re: Any ideas on faster TLS ? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2017-02-28 04:02 -0600
                Re: Any ideas on faster TLS ? Bernd Paysan <bernd@net2o.de> - 2017-03-11 19:58 +0000
      Re: Any ideas on faster TLS ? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-01 12:36 +0000
        Re: Any ideas on faster TLS ? Ron Aaron <rambamist@gmail.com> - 2017-03-01 15:11 +0200
          Re: Any ideas on faster TLS ? Alex <alex@rivadpm.com> - 2017-03-03 01:18 +0000
            Re: Any ideas on faster TLS ? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-03 12:55 +0000
              Re: Any ideas on faster TLS ? Alex <alex@rivadpm.com> - 2017-03-03 14:59 +0000
                Re: Any ideas on faster TLS ? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-03-03 16:15 +0000
          Optimizing (was: Any ideas on faster TLS ?) Ron Aaron <rambamist@gmail.com> - 2017-03-17 13:14 +0200
  Re: Any ideas on faster TLS ? rickman <gnuarm@gmail.com> - 2017-02-28 01:13 -0500
    Re: Any ideas on faster TLS ? Paul Rubin <no.email@nospam.invalid> - 2017-02-27 22:48 -0800
      Re: Any ideas on faster TLS ? rickman <gnuarm@gmail.com> - 2017-02-28 03:50 -0500

csiph-web