Assuming that `p.privateKey = *((*C.private_key)(key))` is making a full copy, not shallow. I think you could avoid using `C.CBytes`.
You can get the address of the start of the backing array via &data[0] as per https://pkg.go.dev/cmd/cgo So maybe something like this: p.privateKey = *((*C.private_key)(unsafe.Pointer(&data[0]))) The code doesn't seem that safe though. There should be something that actually checks that the byte slice is the correct size. On Tue, 23 Aug 2022 at 00:41, David Stainton <dstainton...@gmail.com> wrote: > I recently wrote cgo bindings to the CTIDH C library (a bleeding edge post > quantum non-interactive key exchange) copies the data twice. First in the > call to C.CBytes and then again in the assignment to p.privateKey via that > pointer dereference. > > The performance hit for the twice copy is not really a problem. I'm just > curious, is there's a more proper way to write this that avoids the twice > copy? > > > // FromBytes loads a PrivateKey from the given byte slice. > func (p *PrivateKey) FromBytes(data []byte) { > key := C.CBytes(data) > defer C.free(key) > p.privateKey = *((*C.private_key)(key)) > } > > https://git.xx.network/elixxir/ctidh_cgo/-/blob/master/binding.go#L49-54 > > > Sincerely, > David > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/3b8a9098-aabb-4b53-933e-b4e4b6e21cdcn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/3b8a9098-aabb-4b53-933e-b4e4b6e21cdcn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAFuMYUxkpF_Pc_bgYxW_8DA399o66SgVJxpyrw%2Bxsr2u_4c2_w%40mail.gmail.com.