News for hackers. Stuff that matters.
Re:Zig 0.7.0 tagged by secondcoming on Sunday November 08, 2020 @04:41PM (#25029348)
"Carefully chosen undefined behavior. For example, in Zig both signed and unsigned integers have undefined behavior on overflow, contrasted to only signed integers in C. This facilitates optimizations that are not available in C. "

Whoa, whoa, whoa. UB on unsigned!? Best of luck.

Re:Zig 0.7.0 tagged by xandroo on Sunday November 08, 2020 @04:49PM (#25029402)
It’s checked in debug modes (overflow is a runtime error). You can explicitly use the overflow operator (+%) if you know that’s the behavior you want.
Re:Zig 0.7.0 tagged by jedisct1 on Sunday November 08, 2020 @04:51PM (#25029421)
In ReleaseSafe mode, too.
Re:Zig 0.7.0 tagged by secondcoming on Sunday November 08, 2020 @04:53PM (#25029431)
Fine, but the bugs this will cause when people port code across from C or C++... probably painful.

So, to be safe people should build both Debug and Release?

Re:Zig 0.7.0 tagged by dnautics on Sunday November 08, 2020 @04:58PM (#25029475)
It's going to be a real strategy to write your code in releasesafe, identify hot code, and mark it off as releasefast on a function by function basis
Re:Zig 0.7.0 tagged by jedisct1 on Sunday November 08, 2020 @04:56PM (#25029462)
Develop in debug mode, deploy in safe release mode, and when you're absolutely sure that everything is safe, you can switch to the fast release mode.

It is also possible to selectively disable/enable safety for individual code blocks.

Re:Zig 0.7.0 tagged by xandroo on Sunday November 08, 2020 @04:58PM (#25029479)
Alternatively, there may be unfound overflow bugs in that C/C++ code.

Worth noting that UB is also checked in ReleaseSafe mode. Related issue: https://github.com/ziglang/zig/issues/2301

Re:Zig 0.7.0 tagged by secondcoming on Sunday November 08, 2020 @05:15PM (#25029615)
Only with signed ints, since unsigned overflow is defined behaviour.
Re:Zig 0.7.0 tagged by gameswithgo on Sunday November 08, 2020 @05:07PM (#25029552)
probably deploy in saferelease mode always unless you are making something like a single player video game, or that otherwise has incredible performance needs but does not deal with untrusted data.
Re:Zig 0.7.0 tagged by dnautics on Sunday November 08, 2020 @04:50PM (#25029408)
There's slightly different looking operators if you want explicit overflow behaviour, where it's not UB.
Re:Zig 0.7.0 tagged by Gibbon1 on Sunday November 08, 2020 @06:19PM (#25030014)
I keep coming back to the idea that you should let the user decide. And optimizing on UB by default is always the wrong thing. Because for 99% of code performance is secondary to correctness. The compiler should only do that when requested.

A point: Benchmarks produce metrics measured in seconds. Bugs don't produce metrics you can measure but the costs are unbounded.

Re:Zig 0.7.0 tagged by simias on Monday November 09, 2020 @08:01AM (#25034302)
>I keep coming back to the idea that you should let the user decide. And optimizing on UB by default is always the wrong thing.

Aren't you contradicting yourself here?

And I do think that some UB optimizations make a lot of sense. Actually if you look at the way Rust does it, it gets rid of many UBs not by adding checks for them but by explicitly forbidding the UB condition to occur in safe code (therefore "optimizing on UB" by enforcing that the UB condition cannot happen in correct code, not by adding checks for it and slowing things down). That's why for instance pointer arithmetic is unsafe in Rust, since arbitrary pointer offsets can lead to UB.

Although I agree that there are a few C UBs that I think ought to be relaxed because they don't make a lot of sense for modern architectures though (signed overflow being the main one).

Re:Zig 0.7.0 tagged by Gibbon1 on Monday November 09, 2020 @01:26PM (#25037880)
I just draw on firmware that I write for micro-controllers. Most of the code isn't in a hot path. There is probably 100 lines worth out of 50,000. That's what I mean by optimizing on UB by default at least in my case seems wrong. I'm also in the apposite situation from a web service where if a worker thread hangs you just kill it and restart it. And you can push update to the cloud.

Bringing up signed overflow. I did some tests once flipping back and forth between using a int and an unsigned int and wasn't impressed with the speed up. I've also done tests with aligned and an packed data structures and on a modern processor, also not impressed.

Re:Zig 0.7.0 tagged by wtetzner on Monday November 09, 2020 @04:29PM (#25040167)
> Aren't you contradicting yourself here?

I don't think it's a contradiction. I think what's being said is that it's the wrong default, i.e. the special different-looking operators should be the ones with UB, not the obvious "normal"-looking ones.

Re:Zig 0.7.0 tagged by Gibbon1 on Monday November 09, 2020 @05:29PM (#25040734)
Exactly. At least my experience there is code where you really care about performance and you are willing to do the distance. And then there is a lot of code where you just want it to work with no surprises.