March 31, 2023
On Monday, 27 March 2023 at 08:44:41 UTC, wjoe wrote:
> e.g.: It used to be faster to ...
>
> - pre-calculate sin/cos tables, now the memory look up cost more cycles than the calculation itself
>
...
> - only redraw the parts of the screen that changed, now the branching is slower than to  redraw everything

Good to know, thanks.

> another example is sorting - Alexei wrote a blog post about how a stupid and slow sorting algorithm now performs better in multi threading. Maybe someone remembers the title/url of the post ?

Not sure, but that reminds me of Andrei's blog on partitioning (for quicksort):
https://dlang.org/blog/2020/05/14/lomutos-comeback/

The algorithm traditionally considered slower can be faster because it can be optimized to work better with branch prediction.
March 31, 2023

On Friday, 31 March 2023 at 10:26:32 UTC, Nick Treleaven wrote:

>

On Sunday, 26 March 2023 at 20:39:21 UTC, ryuukk_ wrote:

>

if my code doesn't do threads, why should i put my variable into TLS?

I don't think writing __gshared is much of a burden. You can use -vtls to print out all variables that are TLS, and add that to an automated test to check you don't have any accidentally. I have thought before that a --no-threads compiler switch that does not link the key thread functions but makes __gshared the default might be a good enhancement for your use case. Then if you accidentally call some code that uses std.parallelism internally you would get a link error.

>

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

  1. If you want fast code, why aren't you using threads?
  2. By default D supports threads and accidental data races are far worse than ugly syntax.

It is a burden to type things you don't need, and to type things that are ugly on purpose

I recently discovered -vtls and indeed is very nice to have, managed to clear out all my TLS usage, but made my code even more ugly __gshared everywhere..

>

If you want fast code, why aren't you using threads?

threads is not synonymous of fast code

and if i were to use thread, i'd not rely on globals to begin with

>

By default D supports threads and accidental data races are far worse than ugly syntax.

Debatable, but that's a good point, but that's not the point i bring, the point i bring is __gshared is ugly, so we want an ugly language?

March 31, 2023
On Friday, 31 March 2023 at 10:32:53 UTC, Nick Treleaven wrote:
> On Sunday, 26 March 2023 at 20:36:37 UTC, ryuukk_ wrote:
>> Golang doesn't even have thread local storage, yet they do very well
>
> Go doesn't have a solution to preventing data races at compile time, they just say don't share memory. But what if you accidentally share memory? That is *very* easy to do in Go. You and your users are out of luck. All you can do is run the race detector and pray that you happen to test all the code paths with it that might have data races:
>
>> The race detector only finds races that happen at runtime, so it can't find races in code paths that are not executed
>
> https://go.dev/doc/articles/race_detector

If you want TLS in GO, you specify that you need a TLS variable, just like every sane languages

Go went the smart route on top of having goroutines, making it superior to what ever D offer
March 31, 2023

On Friday, 31 March 2023 at 15:52:21 UTC, ryuukk_ wrote:

>

the point i bring is __gshared is ugly, so we want an ugly language?

Good code shouldn't look ugly, but global mutable variables are bad, so it's appropriate that they look ugly.

You can still put a single __gshared: at the top of your module.

March 31, 2023

On Friday, 31 March 2023 at 16:02:35 UTC, Dennis wrote:

>

On Friday, 31 March 2023 at 15:52:21 UTC, ryuukk_ wrote:

>

the point i bring is __gshared is ugly, so we want an ugly language?

Good code shouldn't look ugly, but global mutable variables are bad, so it's appropriate that they look ugly.

You can still put a single __gshared: at the top of your module.

I disagree, global mutables are not bad

That the same bad advice as telling people to "embrace OOP and multiple inheritance" and all the Java BS

"just put your variable into a class and make it static, and then have your singleton to access your static variables"

March 31, 2023

On Friday, 31 March 2023 at 16:26:36 UTC, ryuukk_ wrote:

>

That the same bad advice as telling people to "embrace OOP and multiple inheritance" and all the Java BS

"just put your variable into a class and make it static, and then have your singleton to access your static variables"

I agree that singletons aren't any better than global variables. The better way is to pass state through function parameters.

March 31, 2023

On Friday, 31 March 2023 at 16:26:36 UTC, ryuukk_ wrote:

>

I disagree, global mutables are not bad

That the same bad advice as telling people to "embrace OOP and multiple inheritance" and all the Java BS

"just put your variable into a class and make it static, and then have your singleton to access your static variables"

Those of us that have been scarred by reading FORTRAN 77 code would disagree. I use global mutables myself (and even the occasional goto), but if anything, it should be __GLOBAL_MUTABLE_VARIABLE to increase the pain of using them.

April 01, 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.

I think "safe" BS is going too far. Normally you don't use global variables at all but if you do the most usual is to use normal global variables with perhaps some kind of synchronization primitive. TLS is quite unusual and having TLS by default might even introduce bugs as the programmer believes that the value can be set by all threads while they are independent.

Regardless, __gshared in front of the variable isn't a huge deal but it shows that the memory model in D is a leaking bucket. Some compilers enforce synchronization primitives for global variables and are "safe" that way. However, sometimes you don't need them like in small systems that only has one thread and it just gets in the way.

TLS by default is mistake in my opinion and it doesn't really help. TLS should be discouraged as much as possible as it is complicated and slows down thread creation.

April 01, 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.

I think "safe" BS is going too far. Normally you don't use global variables at all but if you do the most usual is to use normal global variables with perhaps some kind of synchronization primitive. TLS is quite unusual and having TLS by default might even introduce bugs as the programmer believes that the value can be set by all threads while they are independent.

Regardless, __gshared in front of the variable isn't a huge deal but it shows that the memory model in D is a leaking bucket. Some compilers enforce synchronization primitives for global variables and are "safe" that way. However, sometimes you don't need them like in small systems that only has one thread and it just gets in the way.

TLS by default is mistake in my opinion and it doesn't really help. TLS should be discouraged as much as possible as it is complicated and slows down thread creation.
April 01, 2023

On Friday, 31 March 2023 at 19:43:42 UTC, bachmeier wrote:

>

Those of us that have been scarred by reading FORTRAN 77 code would disagree. I use global mutables myself (and even the occasional goto), but if anything, it should be __GLOBAL_MUTABLE_VARIABLE to increase the pain of using them.

But you kind of get into the same things with "accidental TLS". It doesn't race, but now the variable is different for every thread, which is a different kind of race.

TLS could be explicit and we wouldn't need a -vtls flag. There is no flag to warn for every use of @trusted, so in the grand scheme of things TLS is more dangerous than @trusted.