On Thursday, September 1, 2016 at 8:45:02 AM UTC-4, Luke Mauldin wrote:
>
> After I added the unsafe.Pointer conversion the code compiled: 
> https://play.golang.org/p/QTPyhZzKZH
>
> So my understanding is that line 15 is jut a pointer type conversion to 
> slice that is backed by the C array, so it doesn't matter if I choose 1 << 
> 20 entries or 1 << 22 entries, in both cases the runtime is just creating a 
> slice?
>
>
Yes, it's just a type conversion, and (*[1 << 20]Tag) is only a type. As 
long as the size is large enough to hold your data, have the slice 
operation be within bounds, and be within the size limits of the system, it 
doesn't matter so much.

 

> Another question, which of the two approaches (pointer arithmetic vs 
> iterating over sliced back by C array) is more efficient to create a Go 
> slice?  This code will be invoked hundreds of times per minute to convert 
> C arrays coming from C functions into memory safe Go slices that will then 
> be used throughout the Go program.
>
>
Hundreds of times per minute isn't very much. Once you've converted the 
array to a Go slice, it's accessed the same as any other Go slice. There's 
no need to calculate the pointer arithmetic by hand when you have the 
compiler to do that for you. 

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to