Final code is here: https://github.com/dc0d/bufferpool

I hope I got it right!

On Monday, April 23, 2018 at 9:48:20 PM UTC+4:30, Kaveh Shahbazian wrote:
>
> @rog Thanks! Three-value slice operator was a very nice hint.
>
> The original intention was to implement a buffer-pool (pool of []byte) 
> without fragmenting the memory. But I was not sure if I was doing it right.
>
> Seems now, assuming the pool consumers are polite goroutines, this can be 
> a good solution. The pool will be used in a TCP server.
>
> On Monday, April 23, 2018 at 6:23:09 PM UTC+4:30, rog wrote:
>>
>> You original example has a problem that others have pointed out because 
>> it's possible for one goroutine to step on the part of the backing array 
>> used 
>> by others. In principle though your technique looks OK to me as long 
>> as you prevent that happening, for example by using the three-value slice 
>> operator to set the capacity as well as the length of the bucket items. 
>>
>> https://play.golang.org/p/vzQlULC1zs7 
>>
>> It's OK to read and write items of the same underlying array as long 
>> as any given element of the array is only accessed by a single goroutine 
>> (or appropriate synchronisation is used). 
>>
>>
>> On 23 April 2018 at 10:38, Kaveh Shahbazian <kaveh.sh...@gmail.com> 
>> wrote: 
>> > Also -race does not complain about this: 
>> > https://play.golang.org/p/IeA4npcemf5 
>> > 
>> > 
>> > On Monday, April 23, 2018 at 10:51:05 AM UTC+4:30, Kaveh Shahbazian 
>> wrote: 
>> >> 
>> >> @Silviu The code is mutating same item from two goroutines. While the 
>> >> original target is to create a buffer pool that their items is not 
>> being 
>> >> mutated from two goroutines - actually they can not do that because at 
>> a 
>> >> specific time only one goroutine has access to one buffer. 
>> >> 
>> >> @Tamas Regarding "If there's no uncoordinated write and read/write of 
>> the 
>> >> same slot, then it's race-free", is it safe to use slices with a 
>> shared 
>> >> underlying array, to be mutated from different goroutine? Every 
>> goroutine 
>> >> has access to one slice and that slice is only accessible to that one 
>> >> goroutine - until the goroutine is done and returns the slice to the 
>> pool. 
>> > 
>> > -- 
>> > 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. 
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>

-- 
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