A CGo-free port of SQLite/SQLite3

Posted by tosh 2 days ago

Counter55Comment34OpenOriginal

Comments

Comment by jsherer 2 days ago

I was confused a few weeks ago. Check out the go pkg: https://pkg.go.dev/modernc.org/sqlite

1. gitlab.com/cznic/sqlite is the primary repo

2. github.com/cznic/sqlite was the github mirror but it moved

3. github.com/mordernc-org/sqlite is the read-only mirror of the primary repo

Cheers!

Comment by porjo 2 days ago

> 3. github.com/mordernc-org/sqlite is the read-only mirror of the primary repo

Typo, should be: github.com/modernc-org/sqlite

Comment by ncruces 2 days ago

There's also my take on this [1], which does machine translation from C to Go via Wasm using [2].

It's a somewhat less faithful translation, since I manually ported the VFS (SQLite OS abstraction layer).

OTOH, each SQLite connection is sandboxed (can't access/corrupt Go memory, or other SQLite connections). And it should run everywhere Go runs.

1: https://github.com/ncruces/go-sqlite3

2: https://github.com/ncruces/wasm2go

Comment by yodon 2 days ago

Seems like there are some trademark issues with just calling this SQLite.

Comment by tptacek 2 days ago

It's cznic/sqlite in Go-land; this is just a quirk of HN titling.

Comment by gnabgib 2 days ago

A quirk of this submitter's choice? We've seen it several times before without the trademark violation:

2023: Go port of SQLite without CGo (255 points, 118 comments) https://news.ycombinator.com/item?id=35488980

2020: Cznic/sqlite: Sqlite in pure Golang (no CGO) (6 points, not interesting for discussion.. this project is old) https://news.ycombinator.com/item?id=24358826

(7 other similar submissions)

Comment by tptacek 2 days ago

I don't care, I just wrote to clarify the project, in the Go community, isn't called "sqlite". That wouldn't make sense; there are multiple (pin-compatible) sqlite interfaces for Go.

Comment by downrightmike 2 days ago

SQL would have been SEQL if not for trademark issues, the torch is passed

Comment by 9rx 2 days ago

Comment by usrnm 2 days ago

The readme really lacks the answer to the "why" question. What's the use case, why should I prefer it over real sqlite?

Comment by ameliaquining 2 days ago

The "port" terminology is misleading; this is real SQLite, compiled from C to Go using https://gitlab.com/cznic/ccgo/-/tree/master/v4 (by the same author; this library is its most widely used application). The use case is that a lot of Go codebases prefer to completely eschew FFI because a lot of the nice properties of Go's tooling and whatnot (cross-compilation is trivial, binaries are automatically static on Linux, etc.) only hold if the entire build is pure Go.

Comment by ivanjermakov 2 days ago

So because of Go's design mistakes.

Comment by 10000truths 2 days ago

No. A mistake is unintentional. Go's rejection of a C runtime dependency is a deliberate trade off, and one that has served it well for its design goals.

Comment by ameliaquining 2 days ago

Compared to what? Other languages AFAIK don't offer those properties at all, except I guess Zig.

Comment by tptacek 2 days ago

The biggest reason you'd want a cgo-free sqlite is if you're cross-compiling; for instance, from your macOS dev laptop to an x64 dev server.

Comment by spwa4 2 days ago

Exactly. Even though your m5 max laptop will run the x86 binary just fine, if you don't compile it for arm, your m5 max laptop drops most of its speed and only has the same performance as an x86 server ...

Comment by 0x20cowboy 2 days ago

If your codebase is pure golang it’s trivial to cross compile for different OSs (aside from binary signing on macOS). If you add some kind of C code you have to jump through a bunch of hoops to get a binary. Funny enough, I was looking for this exact project for that reason last week.

Comment by blubber 2 days ago

Because it's cgo-free maybe?

Comment by usrnm 2 days ago

Cgo overhead is trivial compared to what's happening inside a database engine, totally not worth it

Comment by anupcshan 2 days ago

It's not just about overhead/performance. cgo-free means no need to set up a cross-compiler if targeting other devices. Just "go build" with the right GOARCH and GOOS will let you compile a binary that will run on most devices.

Comment by dizhn 2 days ago

Static builds cannot have cgo too if I am not mistaken.

Comment by agwa 2 days ago

The following go flags let you build statically-linked cgo binaries, provided that all the C libraries that you're using support static linking and don't call the NSS functions in glibc:

-tags netgo,osusergo -linkmode external -extldflags -static

I regularly compile (cross-compile, even) static Go binaries that use the cgo sqlite package. But it's certainly a lot simpler if you can avoid cgo.

Comment by usrnm 2 days ago

I'm pretty sure that C is a much better choice if you really care about binaries that run on most devices

Comment by Ferret7446 2 days ago

Depends on what you mean by "most". The cross compile story for Go is far superior to C for the platforms it supports.

Comment by anupcshan 2 days ago

This project is for using Sqlite from Go code. It is not a general purpose replacement.

Comment by figmert 2 days ago

Absolutely not. Maybe runtime overheads are minimal, but builds are so much harder to do. And yes, you need to figure it out maybe once, but it is still a lot more effort than just pulling in a new dependency. Now repeat that same effort for every new application, vs pulling that into every new application.

Comment by raggi 2 days ago

that’s really not true when the database is all in memory, statements are prepared, and so on.

but the overheads also stack up, the database/sql api is fairly allocation heavy too unless you do a lot of work and that friction increases quite a bit with the ffi boundary.

this is not to suggest “modernc is faster” - it’s not for a lot a workloads.

there are opportunities for optimization all over both approaches.

Comment by pstuart 2 days ago

So is this a handmade port vs the translated port done by modernc?

Comment by vicek22 2 days ago

modernc is the name of the GitHub fork (identical repo) of the same project. This is machine translated, not hand written.

Comment by spwa4 2 days ago

It's machine translated with TLA, tested, and finished by adding a few extra pure go mechanisms (such as Go code-backed virtual tables)

Comment by pjjpo 1 day ago

This project seems to get shared every month or so. It's really sad that HN's signal to noise has fallen this hard... While I continue to have it in my feed, I'm more and more opening up Reddit since it just feels more robust now adays.

Comment by ftgffsdddr 2 days ago

[flagged]

Comment by zzo38computer 2 days ago

I agree that it would be better to not require JavaScripts. (But, I think it can be helpful to have mirrors on other services as well, for this and other reasons.)

However, there are some work arounds to some situations. Git could (presumably) still be used, if you have that (although you might not want the entire repository and only some files, so that is a possible issue with this). If you have a URL of a specific file that you can change "blob" to "raw" in the URL to access the raw file (this works on other services as well and is not specific to Gitlab). For commits, you can add ".patch" or ".diff" on the end of the URL (this also is not specific to Gitlab).

Comment by 2 days ago

Comment by qweqwe14 2 days ago

Just use a real web browser or enable JS, it's not that hard buddy. "Why do you keep hitting yourself?"

Comment by slopinthebag 2 days ago

[flagged]

Comment by bananamogul 2 days ago

[flagged]

Comment by wild_egg 2 days ago

No it doesn't.