News for hackers. Stuff that matters.
Re:The Use of assert() in SQLite by repsilat on Friday April 26, 2019 @10:40PM (#19763427)
> The assert() macro in C is roughly just if ... { panic() } in Go

The article very clearly says that (unlike normal C behaviour) it's a no-op in release builds of SQLite, because otherwise they get a ~3x slowdown. Does Go compile away those `if` statements?

Re:The Use of assert() in SQLite by Stratoscope on Friday April 26, 2019 @11:02PM (#19763503)
> Does Go compile away those `if` statements, or did you not read the article?

The HN guidelines specifically request that we not make comments like this.

> Please don't insinuate that someone hasn't read an article. "Did you even read the article? It mentions that" can be shortened to "The article mentions that."

https://news.ycombinator.com/newsguidelines.html

Re:The Use of assert() in SQLite by repsilat on Friday April 26, 2019 @11:07PM (#19763528)
You're right, it was out of line. Edited, and thanks.
Re:The Use of assert() in SQLite by Stratoscope on Friday April 26, 2019 @11:15PM (#19763544)
Well, thank you for taking criticism so well! We all get a little snippy at times - goodness knows I've done it myself more than I want to admit.

I was about to delete my comment since it was no longer relevant, but maybe I will leave it here since your reply and edit are such a great example of how to be constructive and civil. (OTOH, let me know if you'd prefer for me to delete it.) Thanks!

Re:The Use of assert() in SQLite by crawshaw on Saturday April 27, 2019 @08:40AM (#19765224)
I find the fastest way to introduce bugs into programs is to compile programs differently in development than in production. It also warps developer's perspectives of how their program performs. As they say at NASA, "Test what you fly, fly what you test."

That said, you can do this easily enough in Go. Define a package:

  package debug

  const Debug = true

  func Assert(cond bool, msg string) {
        if Debug && cond {
                panic(msg)
        }
  }
Have your build system generate the file that contains Debug = true. In optimization mode, make it Debug = false. As it is const, the compiler will propagate it, Assert will be inlined and the dead code removed. If you don't like relying on the compiler, have your code generator generate empty Assert functions. If you are working in a very bad code base that catches all panics, you'll need an extra 5 lines of code in the implementation of Assert, and your stack traces won't be pretty. (Roughly: put the condition check in a goroutine, block the Assert call on a channel signal from that goroutine.) I'd recommend the first project be to remove those panic catch-alls.

But really, all of this is only appropriate when you are far out in the weeds. Ship your program with those asserts. If they are too slow for production they are too slow for development.

Re:The Use of assert() in SQLite by SQLite on Saturday April 27, 2019 @05:28PM (#19767735)
"Test what you fly and fly what you test" is what we live by at SQLite. Testing is done three times:

(1) One test uses compiler and preprocessor options set up to measure coverage. This is not so much a test of SQLite itself as it is a test of the test logic, to verify that the testing provides 100% MC/DC.

(2) The second test runs with assert(), ALWAYS(), and NEVER() enabled. This is like a unit test. It verifies internal assumptions and state, at the expense of running 3x slower.

(3) Finally, we build as for delivery and test once again. This is the string test, where we "test what we fly".

All three test runs must give the same answer (modulo performance) before a release.

During day-to-day development work, we usually run (2), but occasionally toss in a (1) or (3) just to confirm that we haven't introduced any gaps in test coverage, or bugs that are masked by the debugging logic.

So you are correct that you should not do all your testing using one configuration and then deliver a different build. But that does not mean you can't have extra logic in your code that helps do unit testing and debugging during development and which is excluded from release builds. You just need to make sure that you rerun all tests in the release configuration.

Re:The Use of assert() in SQLite by kasey_junk on Saturday April 27, 2019 @08:54AM (#19765267)
That will still evaluate the cond in all cases. Passing a func will make it lazy but might have performance implications (I’d have to test it).
Re:The Use of assert() in SQLite by crawshaw on Saturday April 27, 2019 @08:59AM (#19765281)
It won't, because of inlining and dead code removal. (At least, it didn't back around Go 1.11 when I last tested this.)
Re:The Use of assert() in SQLite by chronial on Saturday April 27, 2019 @05:09PM (#19767651)
If you do Assert(ExpensiveOp()), removing all of it is only a valid optimization if ExpensiveOp() is 100% side-effect free. But even when that is the case, the compiler won't inline ExpensiveOp() and go through the effort of proving that it's side effect free if it's sufficiently complex.
Re:The Use of assert() in SQLite by kasey_junk on Saturday April 27, 2019 @10:13AM (#19765633)
Maybe if the thing that is generating the cond at the call location is inline-able, but not if it's doing an expensive operation.

https://play.golang.org/p/Ts6QqRrWwuQ

I couldn't get that to not trigger the lock.

[later] I could get it to remove it if you pass in a func instead.

Re:The Use of assert() in SQLite by chronial on Saturday April 27, 2019 @05:10PM (#19767656)
Your Cond() has a side-effect. It can never be optimized away.
Re:The Use of assert() in SQLite by kasey_junk on Saturday April 27, 2019 @07:19PM (#19768243)
Correct. Which is one of the differences between this approach & macros. Though the lazy approach appears to work.
Re:The Use of assert() in SQLite by bbatha on Saturday April 27, 2019 @12:38AM (#19763802)
It’s not the if staments: for an assertion the panic branch should be ice-cold and likely proven by invariants up the stack frame — other assertions or plain old conditionals — so in theory should be optimized away every time. It’s the expression in the condition that’s often expensive. The assert macro eliminates these trivially with the preprocessor , but go cannot as easily because the condition expression may have side effects.
Re:The Use of assert() in SQLite by tptacek on Friday April 26, 2019 @11:48PM (#19763654)
Help me understand why this matters? Is it really Hipp's argument that Go is unsuitable for serious systems work because invariant signaling in the code is too slow? That seems pretty nitpicky, and also ironic since the "serious" systems language to which he's comparing Go is C.
Re:The Use of assert() in SQLite by pcwalton on Saturday April 27, 2019 @01:53AM (#19764027)
Not being able to have code that compiles in debug but doesn't in release is a pretty serious issue for CPU-bound performance-critical code. Otherwise, you're going to be commenting things out a lot or using preprocessors, both of which are annoying and slow down development velocity.

As an example, checking errors on GPU is enormously slow because you stall the CPU until the hardware FIFO drains every time you do. You can't ship software that checks for errors after issuing each DirectX or OpenGL call. But writing graphics code is also really annoying without a debug mode that checks for errors after each render command, because the failure mode of graphics programs tends to be "black screen" or "screen full of garbage".

Re:The Use of assert() in SQLite by Spivak on Saturday April 27, 2019 @12:21PM (#19766267)
Isn't a preprocessor the right place for something like this? It doesn't have to operate at the level of source code but if you want your actual code/AST, not just the behavior, to be different from dev to prod then you need a language that compiles to Go?

Baking that into the language seems like it just replaces Go with that meta-language and hides the Go underneath like how "C" is really CPP-lang with a C compiler underneath.

Re:The Use of assert() in SQLite by RHSeeger on Saturday April 27, 2019 @12:45AM (#19763833)
I would take anything Dr Hipp says very seriously. While he may be wrong, I wouldn't discard his thoughts without some facts to back it up. SQLite, both its functionality and amazing test suite, as well as his other achievements, certainly make him worthy of my respect.
Re:The Use of assert() in SQLite by tptacek on Saturday April 27, 2019 @01:06AM (#19763898)
Not to be snarky, but because it's relevant to the discussion:

https://www.zdnet.com/article/sqlite-bug-impacts-thousands-o...

This stuff doesn't happen in Go and Rust programs.

Re:The Use of assert() in SQLite by apta on Saturday April 27, 2019 @10:33AM (#19765728)
What does that specifically have to do with golang or Rust?
Re:The Use of assert() in SQLite by tptacek on Saturday April 27, 2019 @11:28AM (#19765999)
Hipp's argument --- which comes kind of out of nowhere in his post --- is that Go is unsuitable as a serious systems language because it lacks compile-time asserts. Which is, I think, ironic, because his language is C, and so produces memory corruption vulnerabilities that languages like Go and Rust preclude.

I'd rather have Go's guarantees than C's.

Re:The Use of assert() in SQLite by helper on Saturday April 27, 2019 @12:00AM (#19763693)
In go you can use build tags to achieve this. The compiler will drop function calls to empty function implementations (tested on go 1.11.5).