News for hackers. Stuff that matters.
Re:The Use of assert() in SQLite by Asooka on Saturday April 27, 2019 @08:47AM (#19765248)
It still incurs a nontrivial runtime cost, unless you've ifdef'd it away in release builds, but even so it is a heavy price to pay in debug or semi-debug^ builds for things like container operator[] bounds checking. The best implementation of assert I've seen raises SIGTRAP on error, which neatly lets you attach a debugger and break when the error occurs, at which point you have the full stack trace and program state.

^ semi-debug in our case means minimal optimisations and debug asserts enabled.

Re:The Use of assert() in SQLite by barrkel on Saturday April 27, 2019 @09:44AM (#19765460)
Bounds checking is one of those things that's easy for a compiler to hoist out of a loop and for branch prediction to guess right when not. It has very high value to cost ratio.
Re:The Use of assert() in SQLite by jcelerier on Saturday April 27, 2019 @09:42AM (#19765453)
> It still incurs a nontrivial runtime cost,

why do you care about the runtime cost of something that makes your program crash ?

Re:The Use of assert() in SQLite by jasode on Saturday April 27, 2019 @10:28AM (#19765702)
>why do you care about the runtime cost of something that makes your program crash ?

I think both parent commenters (htfy96 and Asooka) got on a tangent because htfy96 substituted a throw exception for an assert() in gp's (hermitdev) code -- as if they were equivalent. However, exceptions have fundamentally different semantics from asserts and are not substitutes for each other. (For anyone unfamiliar with the different purposes of exceptions vs asserts, please read the top 2 answers in this Stackoverflow question.[0])

Things like array bounds checking (used as a runtime safety check instead of an "impossibility" check) would not be tested with assert().

To answer your direct question, programmers who use assert() correctly for "testing their understanding of the world by testing an impossible condition" don't want to pay a runtime penalty for it.

[0] https://stackoverflow.com/questions/1467568/debug-assert-vs-...

Re:The Use of assert() in SQLite by fredsanford on Saturday April 27, 2019 @09:49AM (#19765492)
Exceptions have a setup cost. You pay that setup cost every time an exception frame is created.
Re:The Use of assert() in SQLite by plorkyeran on Saturday April 27, 2019 @12:53PM (#19766432)
That is not true on most platforms in use today. The Itanium ABI uses "zero cost" exceptions which do not have any setup or runtime cost when an exception is not thrown (at the cost of making throwing exceptions much more expensive than other schemes). 64-bit Windows and arm64 use similar schemes.
Re:The Use of assert() in SQLite by fredsanford on Sunday April 28, 2019 @02:42AM (#19769731)
I guess I have to use weasel words in every post now.

Let's pretend my previous post started with

"In most cases, "

Re:The Use of assert() in SQLite by Dylan16807 on Sunday April 28, 2019 @06:10AM (#19770298)
But that's the point, it's not most. GCC and Clang default to zero-cost DWARF. Add Windows and Arm64 and that's almost everything.
Re:The Use of assert() in SQLite by jcelerier on Sunday April 28, 2019 @08:39AM (#19770816)
The only "relevant" platform where exceptions aren't zero cost nowadays is 32-bit windows - not even 64-bit one. so no, absolutely not "in most cases".