bmatsuo / lmdb-go

Bindings for the LMDB C library
BSD 3-Clause "New" or "Revised" License
158 stars 59 forks source link

Confusion over this code #142

Closed msackman closed 2 years ago

msackman commented 2 years ago

https://github.com/bmatsuo/lmdb-go/blob/a14b5a390eff52e52a20eceb3020038b3d291432/lmdb/cursor.go#L165

I know it's been a while since you worked on this code. I'm working on my own, slightly different, Go binding to LMDB. Your library is very thorough, and I have gained from your approach with your bridging C to avoid unnecessary copies of keys and values when sending to C.

But the referenced code confuses me. If the LMDB returns the exact same pointer to the key data, then why is it not safe to build a slice from that? Shouldn't this be the same as two slices which use the same backing array? I'm sure I must be missing something, but I disabled this section of code and nothing fails in your benchmarks or tests (though I've had to edit them a little to make them work again with modern Go).

No worries if you can't remember or have just moved on to other things. But if anything comes to mind, I'd be grateful to learn more about why this needs special handling. Many thanks.

msackman commented 2 years ago

I think this must be due to the requirement that:

[C code] must not store any Go pointers in Go memory, even temporarily

https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers