News for hackers. Stuff that matters.
Re:When zero cost abstractions aren't zero cost by CountHackulus on Monday August 09, 2021 @08:42PM (#28124040)
There are no zero cost abstractions. (According to Chandler Carruth, and he makes a convincing point). https://www.youtube.com/watch?v=rHIkrotSwcc
Re:When zero cost abstractions aren't zero cost by josephcsible on Monday August 09, 2021 @10:35PM (#28124644)
Most people consider "cost" in "zero-cost abstraction" to refer to runtime cost. That presentation is saying that if you let it refer to other kinds of cost too, then nothing is zero-cost, but to me that just means he picked a bad definition.
Re:When zero cost abstractions aren't zero cost by dkersten on Monday August 09, 2021 @10:52PM (#28124726)
And even then, I've often heard it stated as "zero cost if you don't use it", eg, exceptions have zero runtime cost unless an exception is actually thrown, so, given that they should only be thrown in exceptional cases, that really shouldn't impact your program. (Whether or not its truly zero cost depends, I guess, eg does memory count as cost? What if it makes something not fit in cache?)
Re:When zero cost abstractions aren't zero cost by bigcheesegs on Tuesday August 10, 2021 @01:15AM (#28125362)
I love that exceptions are the classic case for this, because it's not even true that "zero-cost exceptions" are zero _runtime_ cost on the non exceptional path. The most trivial example is they block vectorization.
Re:When zero cost abstractions aren't zero cost by TeMPOraL on Tuesday August 10, 2021 @04:26AM (#28126388)
Well, exceptions shouldn't be the classic case here, because - as you say - they're typically not zero cost.

Zero-cost abstraction is what you get when your abstraction gets compiled away to nothing. Like properly written value wrappers in C++, or (presumably) newtype in Rust. These things disappear from final assembly.

Re:When zero cost abstractions aren't zero cost by jcelerier on Tuesday August 10, 2021 @05:41AM (#28126823)
The definition of zero-cost abstractions, as introduced in C++, was that "it can't be slower that code than the equivalent hand-written code" (e.g. code not using the abstraction).

In that regard, exceptions are interesting as if you're on the happy path (which should be 99.999% of the time - a normal program that uses exceptions as error handling method should not encounter any exception if you do a `catch throw` on an average run of the software), they can cost less than return-value-based error handling (https://nibblestew.blogspot.com/2017/01/measuring-execution-...). If you're on a "sad path", though, they will cost more.

What is pretty sure is that since compilers learned to put the "sad" path in .cold section, the code size issue has become a 100% non-issue, the "sad" path won't bloat the hot, exception-less path ; in my experience, exceptions are in cases that matter a negative-cost abstraction.

Re:When zero cost abstractions aren't zero cost by anon1608 on Tuesday August 10, 2021 @06:58AM (#28127254)
As always with microbenchmarks...

  struct Error* create_error(const char *msg) {
    struct Error *e = malloc(sizeof(struct Error));
    e->msg = strdup(msg);
    return e;
  }

Is it measuring exceptions vs return codes, or creation cost of std::runtime_error(const char *) (small string opt?) vs malloc() + strdup?
Re:When zero cost abstractions aren't zero cost by PragmaticPulp on Monday August 09, 2021 @11:07PM (#28124796)
That’s a disingenuous misinterpretation of what “zero-cost” means. Zero-cost refers to runtime performance of compiled code.

It’s also disingenuous to pretend there isn’t some downside to not using the abstraction. Obviously you need to evaluate the tradeoffs.

High level code itself is a sort of abstraction. We could write raw assembly all the time to remove that abstraction and associated costs, but clearly that’s not very productive.

Re:When zero cost abstractions aren't zero cost by quotemstr on Monday August 09, 2021 @11:21PM (#28124857)
Chandler is simply wrong here. Every single line of code has a cost. There's nevertheless massive benefit to that cost not occuring on millions of end user machines and instead only once in the mind of the developer.