Re: [go-nuts] Slices of pointers or not?

2022-02-05 Thread Amnon
Go runtime is dominated by the costs of allocation and GC.

If you return 1 persons, then if we make the (unrealistic) assumption 
that a Person contains not pointers (strings etc). then returning []Person 
would require one allocation, whereas returning []*Person would require 
10,001. And GC will be much slower as there will be 10,000 more objects to 
mark.
Iteration across the returned slice will also be much faster in the former 
case, as each Person will 
reside in (cache friendly) sequential memory locations, whereas in the 
latter case the memory location
of each person will be effectively randomised and entail a cache miss.

So in general returning []Person will be much faster. 
Of course you should make sure you pre-allocate the entire slice, rather 
than letting it grow automatically
as the result of appends.


On Friday, 4 February 2022 at 09:43:28 UTC pauloaf...@gmail.com wrote:

> I appreciate your help.
>
> Em sexta-feira, 4 de fevereiro de 2022 às 00:12:47 UTC-3, 
> ren...@ix.netcom.com escreveu:
>
>> I think the OPs question was specifically about the cost of returning 
>> from a function - it is the same. 
>>
>> > On Feb 3, 2022, at 8:03 PM, Connor Kuehl  wrote: 
>> > 
>> > On Thu, Feb 3, 2022 at 7:07 PM Paulo Júnior  
>> wrote: 
>> >> 
>> >> Hi all. 
>> >> 
>> >> I hope you are well. 
>> >> 
>> >> Is there a big difference, in terms of performance or functionality, 
>> between declaring []*Person or []Person as a return type of a function? 
>> > 
>> > If you find yourself iterating over a group of structs like this a lot 
>> > (especially if there's a lot of them to iterate over), you might want 
>> > to consider measuring the performance differences between []*Person or 
>> > []Person. With pointers, the actual structs might be far away from 
>> > each other in memory, which may prevent you from taking full advantage 
>> > of your processor's cache since it won't be able to benefit from the 
>> > spatial locality that an array (or slice) offers. 
>> > 
>> > Connor 
>> > 
>> > -- 
>> > 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...@googlegroups.com. 
>> > To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/CAC_QXZS-QEKTgvsK3TKk-yUyc1jP81HngHz6dcDV8bZwqLBT6Q%40mail.gmail.com.
>>  
>>
>>
>

-- 
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/1f76f2e2-a26a-4f05-bb5a-435f98a93219n%40googlegroups.com.


Re: [go-nuts] Slices of pointers or not?

2022-02-04 Thread Paulo Júnior
I appreciate your help.

Em sexta-feira, 4 de fevereiro de 2022 às 00:12:47 UTC-3, 
ren...@ix.netcom.com escreveu:

> I think the OPs question was specifically about the cost of returning from 
> a function - it is the same. 
>
> > On Feb 3, 2022, at 8:03 PM, Connor Kuehl  wrote:
> > 
> > On Thu, Feb 3, 2022 at 7:07 PM Paulo Júnior  
> wrote:
> >> 
> >> Hi all.
> >> 
> >> I hope you are well.
> >> 
> >> Is there a big difference, in terms of performance or functionality, 
> between declaring []*Person or []Person as a return type of a function?
> > 
> > If you find yourself iterating over a group of structs like this a lot
> > (especially if there's a lot of them to iterate over), you might want
> > to consider measuring the performance differences between []*Person or
> > []Person. With pointers, the actual structs might be far away from
> > each other in memory, which may prevent you from taking full advantage
> > of your processor's cache since it won't be able to benefit from the
> > spatial locality that an array (or slice) offers.
> > 
> > Connor
> > 
> > -- 
> > 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...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAC_QXZS-QEKTgvsK3TKk-yUyc1jP81HngHz6dcDV8bZwqLBT6Q%40mail.gmail.com
> .
>

-- 
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/397af248-c73b-429d-8eed-553bdc03ab0an%40googlegroups.com.


Re: [go-nuts] Slices of pointers or not?

2022-02-03 Thread Robert Engels
I think the OPs question was specifically about the cost of returning from a 
function - it is the same. 

> On Feb 3, 2022, at 8:03 PM, Connor Kuehl  wrote:
> 
> On Thu, Feb 3, 2022 at 7:07 PM Paulo Júnior  wrote:
>> 
>> Hi all.
>> 
>> I hope you are well.
>> 
>> Is there a big difference, in terms of performance or functionality, between 
>> declaring []*Person or []Person as a return type of a function?
> 
> If you find yourself iterating over a group of structs like this a lot
> (especially if there's a lot of them to iterate over), you might want
> to consider measuring the performance differences between []*Person or
> []Person. With pointers, the actual structs might be far away from
> each other in memory, which may prevent you from taking full advantage
> of your processor's cache since it won't be able to benefit from the
> spatial locality that an array (or slice) offers.
> 
> Connor
> 
> -- 
> 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/CAC_QXZS-QEKTgvsK3TKk-yUyc1jP81HngHz6dcDV8bZwqLBT6Q%40mail.gmail.com.

-- 
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/EF34CFE5-9A6A-4CF7-80C4-83F730518260%40ix.netcom.com.


Re: [go-nuts] Slices of pointers or not?

2022-02-03 Thread Connor Kuehl
On Thu, Feb 3, 2022 at 7:07 PM Paulo Júnior  wrote:
>
> Hi all.
>
> I hope you are well.
>
> Is there a big difference, in terms of performance or functionality, between 
> declaring []*Person or []Person as a return type of a function?

If you find yourself iterating over a group of structs like this a lot
(especially if there's a lot of them to iterate over), you might want
to consider measuring the performance differences between []*Person or
[]Person. With pointers, the actual structs might be far away from
each other in memory, which may prevent you from taking full advantage
of your processor's cache since it won't be able to benefit from the
spatial locality that an array (or slice) offers.

Connor

-- 
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/CAC_QXZS-QEKTgvsK3TKk-yUyc1jP81HngHz6dcDV8bZwqLBT6Q%40mail.gmail.com.


Re: [go-nuts] Slices of pointers or not?

2022-02-03 Thread Robert Engels
No. Because a slice is a slice. But there are several performance differences 
in the usage. 

> On Feb 3, 2022, at 7:09 PM, Paulo Júnior  wrote:
> 
> Hi all.
> 
> I hope you are well. 
> 
> Is there a big difference, in terms of performance or functionality, between 
> declaring []*Person or []Person as a return type of a function?
> 
> Code sample https://go.dev/play/p/tBAod1hZvYu
> 
> Thank you and best regards.
> Paulo.
> -- 
> 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/964eb09d-cd5f-4da1-b1ca-cac3e9b5bb69n%40googlegroups.com.

-- 
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/5C7F5D64-0B5D-41D6-9DFE-0A9CB6AC9A67%40ix.netcom.com.


Re: [go-nuts] Slices of pointers or not?

2022-02-03 Thread Marcin Romaszewicz
It depends on what you do with it, and how you use it.

[]*Person is a slice of pointers, they're small.
[]Person is a slice of structs, they're bigger than pointers.

The first requires a level of indirection to access, the second doesn't.
The first requires no copying of structs when you're iterating or resizing,
while the second requires copying entire structs.

So, either could be faster based on what you do with it.

On Thu, Feb 3, 2022 at 5:10 PM Paulo Júnior 
wrote:

> Hi all.
>
> I hope you are well.
>
> Is there a big difference, in terms of performance or functionality,
> between declaring *[]*Person* or *[]Person* as a return type of a
> function?
>
> Code sample https://go.dev/play/p/tBAod1hZvYu
>
> Thank you and best regards.
> Paulo.
>
> --
> 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/964eb09d-cd5f-4da1-b1ca-cac3e9b5bb69n%40googlegroups.com
> 
> .
>

-- 
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/CA%2Bv29LtxpB6cdryT0sfh1AXmf1X%2BJS0di40y0%2BYpmxz94Z1CeQ%40mail.gmail.com.


[go-nuts] Slices of pointers or not?

2022-02-03 Thread Paulo Júnior
Hi all.

I hope you are well. 

Is there a big difference, in terms of performance or functionality, 
between declaring *[]*Person* or *[]Person* as a return type of a function?

Code sample https://go.dev/play/p/tBAod1hZvYu

Thank you and best regards.
Paulo. 

-- 
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/964eb09d-cd5f-4da1-b1ca-cac3e9b5bb69n%40googlegroups.com.