Making cgo-based callbacks?
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 9.2k
- Forks
- 1.2k
- Avg merge
- 19m
- Merged PRs (30d)
- 4
Description
I've got a callback function which, in one of my key queries, is called thousands of times. Initially I did the "easy" thing of making a Golang-based callback, using conn.RegisterFunc(). Profiling revealed that queries were spending the vast majority of their time in the marshalling code.
I recently did some benchmark testing, and found that I can get a 20x speedup in this function by writing it in C, and loading it as an extension. (Benchmarking fingered the golang-based callback as taking nearly 1600ns, and the C-based callback as taking only 80ns.)
However, actually making a separate dynamically-loadable executable is a bit inconvenient for a Go project; I'd have to switch from the built-in Golang build system to a makefile-based system or something. That's a pain, particularly when I just need to define 1 C function and call 1 C function.
What I'd really like to be able to do is something like this:
/*
[Define 'myFunc']
*/
import "C"
/* ... */
func myFuncHook(conn *sqlite3.SQLiteConn) error {
db := conn.GetContext() // Return underlying SQLiteContext
ret = C.sqlite3_create_function(db, "myFunc", 3, SQLITE_UTF8, NULL, C.myFunc /* ... */)
/* ... */
}
And then set myFuncHook as the ConnectionHooks. I see that the underlying C SQLite connection has its own type, but I don't see how to get the connection from the hook callback.
Is that possible? And if it's not possible, could it be made possible?
Alternately, would it make sense to add a RegisterCFunc method, which would take a C function pointer?
I might be able to send you a PR if I knew what kind of solution you'd prefer.
Thanks in advance!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing ConnectionHooks, conn.RegisterFunc(), and the underlying SQLite connection API referenced in the issue. Compare the proposed cgo callback approach with a RegisterCFunc method, then define a specific supported API and its completion criteria before implementing it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sqlite
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100