News for hackers. Stuff that matters.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Thursday April 27, 2017 @07:38PM (#14215770)
> Exceptions cause all sorts of problems(like violating the principal of only paying for what you use).

Exceptions are zero-cost in terms of performance on any sane architecture (this excludes x86, but not x64). The only cost that you're paying is the extra memory for the tables, which are all statically allocated.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by muizelaar on Thursday April 27, 2017 @10:44PM (#14216544)
Compiling with exceptions can still have performance cost because the addition of implicit control flow at every function call.

For example the following code will compile differently depending on whether goo() can throw.

struct k { void p[4]; };

void goo(k s);

k foo() { k r; k s; goo(&s); r = s; return r; }

If goo() doesn't throw the extra variable s can be eliminated.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @01:03AM (#14216971)
If goo doesn't throw, why not declare it nothrow?

Although I still don't see why, in your example, one of the variables cannot be elided even if goo does throw. Since there's no point at which address of r is taken, much less escapes the scope, it should always be safe to elide r and just return s, no?

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by aristidb on Thursday April 27, 2017 @10:54PM (#14216574)
Try indenting your code section by 4 spaces. :)
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by flamedoge on Friday April 28, 2017 @02:18AM (#14217222)
you mean r? you can't eliminate s regardless of goo throwing.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Thursday April 27, 2017 @09:18PM (#14216219)
They're zero(ish) cost at runtime, but focusing on just that is missing something: pervasive unchecked exceptions inhibit optimisations, both in compilers and in libraries, which have to be more defensive to preserve correctness.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @12:56AM (#14216946)
If you replace exceptions with e.g. return values, it doesn't really change anything for the libraries - if they expect to be able to deal with an error, they still have to defensively preserve correctness.

OTOH the library always has the option to say "this particular thing that you give me should be nothrow; if it throws, that's U.B.", and not even try being defensive in the face of exceptions when they come out of such a thing.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Friday April 28, 2017 @04:02AM (#14217690)
I think you missed the precise qualification of my comment: this only applies when the compiler can't tell what exceptions can occur, like in C++, Rust and even Java, and so needs to be able to handle any/all of them. The most basic (and most relevant) query is "can this call throw?", but even that is impossible to answer without global information when there are unchecked exceptions.

In statically typed languages, return values, like (fully) checked exceptions, are different, as they're exactly the sort of global information the compiler needs. Functions can statically guarantee the exact set of ways in which it can interact with its caller, and the compiler can understand. This means a function that, say, sorts a list with a less-than function (that returns bool) knows that every time that function exits, it gives a value of true or false, no exceptions (hah).

In any case, just writing a library that says "UB to throw" isn't quite enough, as the compiler won't understand, and will still structure code to handle exceptions "sensibly", even thought it doesn't need to. Fixing that means being able to communicate nothrow to the compiler (which is nice too), but this starts to no longer be a pervasive unchecked error system: it's somewhat of a binary form of checking.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @04:34PM (#14222839)
Well, we were talking about exceptions in C++, where there is a standard way to communicate nothrow. So yes, you're right that it's no longer a pervasively unchecked error system - but that's precisely why they're not any worse than Result<T,E> at this point. Effectively you can treat C++ as having every function return Result<T,E> by default, with nothrow being an opt-out to just return T.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by milesrout on Friday April 28, 2017 @02:00AM (#14217147)
The thing is, in C++ you have to explicitly write noexcept for that to happen.

If you use optional<T> you have to explicitly write that, and turning off exceptions means you don't have to write noexcept everywhere.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by searealist on Friday April 28, 2017 @12:05AM (#14216802)
I guess that's why you Rust guys don't have exceptions, so you don't have to pay this cost. </s>
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Friday April 28, 2017 @12:09AM (#14216820)
(Explanation of the /s: panics use the same mechanism, so the compiler and libraries unfortunately still have to be just as defensive when they're enabled. When the compiler is in -C panic=abort mode, it doesn't suffer from this, but I don't think libraries have a great way to customise/improve their behaviour in that case at the moment.)
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @07:52PM (#14215835)
Don't you need RTTI as well? That's not been zero-cost on all compilers I've used in the past.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @12:57AM (#14216953)
You do, but C++ includes RTTI separately from exceptions (dynamic_cast and typeid), so it's going to be there regardless. If you turn it off, then we're not talking about standard C++ anymore, but some subset.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by wvenable on Friday April 28, 2017 @01:33AM (#14217062)
When you throw and exception, C++ compilers add RTTI for those classes even when RTTI is separately disabled. You cannot call dynamic_cast and type_id but the generated code is the same.

I don't really see this as much of an issue given you have to have this RTTI to catch the exception properly.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @03:42AM (#14217604)
My point is that RTTI is required by other things in the C++ standard, not just exceptions. So it's still "free" if we're only considering exceptions separately by themselves, and leave all other features as they are in standard C++ (and if you disable RTTI entirely, then you no longer have standard C++, since those other features also stop working).

If we consider the combo of exceptions+RTTI, then yes, dropping both at the same time lets you get rid of some further overhead (although again it's memory-only, not compute cycles). But then we have to discuss the lost benefits of those other features, not just exceptions alone, to judge whether doing so is worthwhile or not.

BTW, I may well be wrong here, but isn't RTTI generation for exceptions (when it's otherwise disabled) an example of "only pay for what you use"? i.e. wouldn't the compiler then only generate RTTI entries for those types that are either thrown somewhere, or are caught somewhere? If so, any library code that doesn't throw itself, and only needs to allow exceptions to pass through it, is still zero-overhead in that regard.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by easytiger on Friday April 28, 2017 @04:30AM (#14217825)
> Exceptions are zero-cost in terms of performance on any sane architecture

Not when the exception is triggered

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @04:35PM (#14222847)
If you trigger an exception, then you're not paying for something that you don't use anymore.