113 points · by cvilsmeier · 3y ago · 61 comments
https://www.synopsys.com/blogs/software-security/unlicensed-...
https://gitlab.com/cznic/sqlite
https://datastation.multiprocess.io/blog/2022-05-12-sqlite-i...
It seems like a pretty good cgo use case: a decent amount of work, which is typically slow enough that cgo overhead isn’t perf critical (because DB usually means disk reads), a super robust and well tested C library with a super well maintained cgo wrapper (mattn).
No, the performance is certainly orders of magnitude faster than serializing over std streams on a subprocess (c ffi calls in cgo are 10s of nanoseconds).
But one of the big draws of golang is the write-once-compile-anywhere toolchain and calls cgo makes that harder.
As soon as you or a library touches cgo, you have to deal with finding and setting up C cross-compilation tooling for your target(s), dynamic linking details, tons of stuff that may have nothing to do with your code or be an area you're an expert in as a Go developer.
I think Go should just pull a Zig and just embed a full blown C compiler into go build.
Using this cznic's pure-Go sqlite saved the day.
I also once tried to figure out a way to cross compile mattn/go-sqlite3 to 32bit Windows with zig, and failed.
The best way to make cross compiling painless is to not use cgo at all. Which is why I use modernc/sqlite whenever possible now.
Btw, bundling a C compiler is also much harder when you don't build on top of LLVM.
If I’m choosing to use a C framework (SQLite) I’m okay signing up for the environment costs. Prefer that over abstractions in an intermediate layer that might not be maintained in a few years.
If you drop that interface, you get much better performance.
See: https://github.com/eatonphil/gosqlite for example.
Edit: Nevermind, you did include crawshaw (which doesn't use database/sql) in your benchmark!
1: https://zig.news/kristoff/building-sqlite-with-cgo-for-every...
https://github.com/FiloSottile/homebrew-musl-cross
It works pretty well! It's a thing you might keep in your back pocket to test builds from your ARM dev machine on a dev host, and then let the CI/CD system build the real version later.
That's the elevator pitch (so far) for: https://github.com/ncruces/go-sqlite3/tree/main
Modernc/libc takes a rather different approach. It's got some pretty funny files in it. https://gitlab.com/cznic/libc/-/blob/master/musl_openbsd_arm...
Is this go? https://gitlab.com/cznic/libc/-/blob/master/libc_openbsd.go?...
I mean technically I suppose it is code that conforms to the go language grammar, but I'm not sure why a language purist would accept this.
people are into programming because things can be reasoned, there is logic behind everything. yet there are non-senses that just try to waste people's time & energy by introducing some of their personal briefs backed by no logic into the whole thing.
cross-compiling regular c projects like sqlite should never be an excuse, I don't believe there is anything that can't be scripted when it come to cross-compiling sqlite.
But there's this if you wanna try: https://github.com/ebitengine/purego
You're right, there are use-cases where SQLite is not appropriate. But nothing beats the ease of installation/backup/maintenance of SQLite compared to server databases like MySQL or Postgres. Another point is development: For unit-tests, I found that initializing a SQLite database for each test-run is much easier (and faster) than having a VM that runs Postgres/MySQL/etc, which I have to spin up and tear down before/after each test run.
That's exactly what I thought, too. But, to my own surprise, it's as fast as CGO solutions, in most cases even faster. You may check here: https://github.com/cvilsmeier/sqinn-go-bench
I'd want a benchmark that is just select * from table, where table is a few gigabytes…
I've been using cgo for accessing sqlite for ages, never ever had any trouble. cgo is not remotely as bad as what people might believe. give cgo a go, and you will realize that it saves your time, let you focus on your projects rather than non-senses like how to use stdin/stdout redirection to bypass cgo.