Jump to page: 1 24  
Page
Thread overview
Why are globals set to tls by default? and why is fast code ugly by default?
Mar 26, 2023
ryuukk_
Mar 26, 2023
ryuukk_
Mar 26, 2023
ryuukk_
Mar 26, 2023
ryuukk_
Mar 31, 2023
Nick Treleaven
Mar 31, 2023
ryuukk_
Apr 01, 2023
IGotD-
Apr 01, 2023
IGotD-
Apr 01, 2023
Guillaume Piolat
Mar 26, 2023
Nick Treleaven
Mar 26, 2023
ryuukk_
Mar 31, 2023
Nick Treleaven
Mar 31, 2023
Nick Treleaven
Mar 31, 2023
ryuukk_
Mar 31, 2023
Dennis
Mar 31, 2023
ryuukk_
Mar 31, 2023
Dennis
Mar 31, 2023
bachmeier
Apr 01, 2023
Guillaume Piolat
Apr 01, 2023
Adam D Ruppe
Mar 26, 2023
ryuukk_
Mar 27, 2023
z
Apr 01, 2023
Ali Çehreli
Apr 01, 2023
Timon Gehr
Apr 01, 2023
IGotD-
Mar 27, 2023
z
Mar 27, 2023
wjoe
Mar 31, 2023
Nick Treleaven
Mar 27, 2023
z
Mar 27, 2023
z
Mar 27, 2023
Jacob Shtokolov
Mar 27, 2023
Guillaume Piolat
March 26, 2023

Hi,

It's common knowledge that accessing tls global is slow http://david-grs.github.io/tls_performance_overhead_cost_linux/

What i do not understand is the reasoning behind choosing tls global by default in D

What i find even more weird is writing fast code is ugly in D

Look at this ugly code

__gshared int fast_code_ugly;

It should be the opposite

Slow code ugly
Fast code beautiful

What can be done about it?

Renaming __gshared

shared is even more ugly since everything must be shared afterwards

I would have prefered if i had to manually set things to tls

@tls int slow_code_ugly;

Even C does it better: https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html

March 26, 2023

Perhaps in -betterC tls vars should be annotated with @tls, and the default is not tls just like in C?

March 27, 2023
Having TLS by default is actually quite desirable if you like your code to be safe without having to do anything extra.

As soon as you go into global to the process memory, you are responsible for synchronization. Ensuring that the state is what you want it to be.

Keep in mind that threads didn't exist when C was created. They could not change their approach without breaking everyone's code. So what they do is totally irrelevant unless its 1980.

I think its the correct way around. You can't accidentally cause memory safety issues. You must explicitly opt-into the ability to mess up your programs state.
March 26, 2023

With -betterC it's broken for 3 years btw:

https://issues.dlang.org/show_bug.cgi?id=20737

March 26, 2023

On Sunday, 26 March 2023 at 18:07:03 UTC, ryuukk_ wrote:

>

What i find even more weird is writing fast code is ugly in D

Look at this ugly code

__gshared int fast_code_ugly;

Because it should be rare that __gshared is used. And if you need it, you won't be worried about how the storage class looks because you'll be concentrating on if your design is thread-safe.

>

It should be the opposite

Then by default, @safe code can not access global variables and there can easily be accidental races between threads.

March 26, 2023

On 3/26/23 2:07 PM, ryuukk_ wrote:

>

Hi,

It's common knowledge that accessing tls global is slow http://david-grs.github.io/tls_performance_overhead_cost_linux/

What i do not understand is the reasoning behind choosing tls global by default in D

If you know a variable is not shared, then you know it can only be accessed from the current thread. This has huge implications for thread access.

However, one problem that D has no good solution is passing thread-local data to another thread (to be owned by the new thread, and no access in the current thread). That's typically the most common use case for "shared data".

-Steve

March 26, 2023
On Sunday, 26 March 2023 at 18:25:54 UTC, Richard (Rikki) Andrew Cattermole wrote:
> Having TLS by default is actually quite desirable if you like your code to be safe without having to do anything extra.
>
> As soon as you go into global to the process memory, you are responsible for synchronization. Ensuring that the state is what you want it to be.
>
> Keep in mind that threads didn't exist when C was created. They could not change their approach without breaking everyone's code. So what they do is totally irrelevant unless its 1980.
>
> I think its the correct way around. You can't accidentally cause memory safety issues. You must explicitly opt-into the ability to mess up your programs state.

It's never desirable to have it by default, the consensus should be: let the developer make the choice, currently the consensus is to punish the developer who want to make the choice

Golang doesn't even have thread local storage, yet they do very well

D doesn't have coroutines, D doesn't do anything special, it just forces you to uglyfy your code to opt out

``__gshared`` is ugly to write, ugly to read, there must be a better solution


March 26, 2023

On Sunday, 26 March 2023 at 18:29:17 UTC, Nick Treleaven wrote:

>

On Sunday, 26 March 2023 at 18:07:03 UTC, ryuukk_ wrote:

>

What i find even more weird is writing fast code is ugly in D

Look at this ugly code

__gshared int fast_code_ugly;

Because it should be rare that __gshared is used. And if you need it, you won't be worried about how the storage class looks because you'll be concentrating on if your design is thread-safe.

I don't understand this argument, if my code doesn't do threads, why should i put my variable into TLS?

If i want fast code, why should i make use of ugly syntax?

March 26, 2023

On Sunday, 26 March 2023 at 19:08:32 UTC, Steven Schveighoffer wrote:

>

On 3/26/23 2:07 PM, ryuukk_ wrote:

>

Hi,

It's common knowledge that accessing tls global is slow http://david-grs.github.io/tls_performance_overhead_cost_linux/

What i do not understand is the reasoning behind choosing tls global by default in D

If you know a variable is not shared, then you know it can only be accessed from the current thread. This has huge implications for thread access.

However, one problem that D has no good solution is passing thread-local data to another thread (to be owned by the new thread, and no access in the current thread). That's typically the most common use case for "shared data".

-Steve

I haven't explored this use case, that must explain why i find it surprising, however, i read about other languages too, and they seem to be explicit whenever they make use of TLS

C, C++, Rust, Zig, Go doesn't do TLS by default for example

March 27, 2023

On Sunday, 26 March 2023 at 18:07:03 UTC, ryuukk_ wrote:

>

shared is even more ugly since everything must be shared afterwards

The limitations of shared can be bypassed with a "function" that removes type qualifiers. return *cast(Unqual!T*) &foo(example, doesn't work as is for arrays.)

This way shared symbols can be used with functions that cannot be made compatible with both shared and non-shared.(by cannot the exact reasons seem obscure, it may be that a called function would support shared parameters but calls a function with the parameter that does not, ime this is painfully apparent with structs' member functions)

Naturally there's the risk of concurrency issues because this isn't the language documentation-recommended way of doing things, but D's library has these problems covered.

« First   ‹ Prev
1 2 3 4