Objects returned by Get() are not special in any way. They will be GCd just 
like an object returned by new(). In fact, they will often be just a 
new()'d object.
There is no association of such an object with the pool.  A pool is just a 
fancy free list. Get might return a previously Put'd object instead of a 
new() one.

Your scheme is unsafe. Storing a *Object as a uintptr means the underlying 
Object will be collected out from under your map.

On Thursday, September 27, 2018 at 2:15:11 PM UTC-7, Francis wrote:
>
> Ah, ok. In that case keeping a reference to a *Object in a uintptr will 
> not prevent it from being garbage collected.
>
> For each garbage collection cycle the runtime visits every memory 
> reference that is still 'alive'. Any references which were not visited will 
> be reclaimed as free memory.
>
> So if you wrote a map `map[uintptr]bool` the GC will not visit any of the 
> locations pointed to by any of the `uintptr`s in the map. So they would all 
> look like unused memory locations, and they would be reclaimed by the 
> garbage collector.
>
> So when you went to cast them back into a *Object, you would be pointing 
> into memory the runtime thought had been freed.
>
> On Thursday, 27 September 2018 19:52:41 UTC+2, Peter Mogensen wrote:
>>
>>
>>
>> On 09/27/2018 07:44 PM, Francis wrote: 
>> > I believe the pool does not track the objects that it returns from a 
>> > call to `Get()`. I would be surprised if it did track these objects. 
>> > There is no real need for it to track these objects. 
>>
>> Exactly, that was my point. Else there would be no reason to call Put() 
>>
>> > I think now your proposal makes a lot more sense to me. If I understand 
>> > correctly you had understood that the sync.Pool would track all the 
>> > instances of objects it had created, via Get(), and so they wouldn't be 
>> > GCed, This would be (to me) a very surprising implementation of 
>> sync.Pool. 
>>
>> No. My understanding of sync.Pool is that only the objects Put() back 
>> into it would be subject to GC. (I could of course, just read the source) 
>> The objects handed out by Get() and still in the hand of the program 
>> would be the responsibility of the application. 
>>
>> My intention was then to handle that responsibility by temporarily 
>> storing these pointers as uintptr and by using them as a value in a map, 
>> be able to exploit the map-of-non-pointers optimization to avoid GC 
>> scans of the map. 
>>
>> ... and of course remember to Put() back any map entries delete()'ed 
>> from the map and any entries left before the reference to the map it 
>> self is forgotten and the map is left to be garbage collected. 
>>
>> /Peter 
>>
>>
>>
>>

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