c-ffi,go-lockbook: better support for uuids #8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently in the "C interface" as well call it, file IDs (which are v4 UUIDs) are passed over FFI as C strings (both ways), and they're stored as strings in Go. There are two major changes in this PR:
Memory Efficiency Throughout
Memory usage for a UUID as a string in Go is 52 bytes:
That's not to mention the overhead of heap allocating and freeing a C string to pass over FFI each way.
Ergonomics, Particularly in Go
For developers consuming the C API as is, it'd be better to have one less heap allocated value to think about, especially when the value in question (a UUID) has a small and known size. But for Go developers, it'd be cool to use a UUID library such as gofrs/uuid instead of plain old strings.
Improvements & Summary
UUID values are stack allocated and copied both ways over FFI. They're stored as a
uuid.UUID
in Go (which is essentially a[16]byte
). No heap allocations either way, and the C values in Go aren't even managed by the GC.Side note: C only passes arrays by pointer, and
cbindgen
has adjusted for this and will warn you if you try it. Therefore, I had to use the "trick" of wrapping the 16 byte array in astruct
. While this is considered bad practice by some, I decided that it'll be fine in this case, which is anytime a standalone UUID needs to be passed over FFI. It looks like:Closes #6.