On Jun 7, 2013, at 11:34 AM, Camille Teruel wrote:

>  On 7 juin 2013, at 11:19, Pharo Issue Tracker wrote:
> 
>> A FogBugz case was edited by Stephane Ducasse.
>> 
>> Case ID:      10840
>> Title:        WeakSet>>#size utterly broken
>> Status:       Resolved (Fix Review Needed)
>> Category:     Bug
>> Project:      Collection
>> Area:         Misc
>> Priority:     3 - Must Fix
>> Milestone:    Pharo3.0: 30/03/2014
>> Assigned To:  Camille Teruel
>> 
>> URL:          https://pharo.fogbugz.com/f/cases/10840
>> 
>> Last message:
>> Please can you send a mail to the mailing-list so that we all discuss this 
>> problems.
> 
> Hello everyone,
> 
> WeakSet>>#size is broken as demonstrated by the following snippet:
> 
> set := WeakSet new.
> set add: Object new.
> Smalltalk garbageCollect.
> set size. "answers 1 instead of 0"
> 
> That is because a WeakSet has no mean to know when its items get garbage 
> collected, and thus the tally cannot be updated.
> So we need to override #size in Weak, I propose:
> size
>       | counter |
>       counter := 0.
>       self do: [ :e | counter := counter + 1 ].
>       ^ counter
> But that it rather inefficient for such a simple query.
> Any proposition?

I would leave size as is, with a comment that by default, it should not be 
trusted.

Then provide an alternative WeakSet constructor, which returns an instance 
which registers objects added to it for finalization, with a finalizing action 
basically doing size := size - 1.
One complication is to make sure to detect and handle the case where object 
being added is already registered for finalization.

I think the current size-inaccurate should still be the default, as the 
alternate will (for obvious reasons) sacrifice speed of normal ops for a fast, 
more* accurate size, and as such should only be used when that is important.

Cheers,
Henry

* In the sense that, I'm not sure what GC actions actually trigger the 
finalization process… scavenging? Either way higher-than-finalization priority 
threads can still strictly speaking return inaccurate sizes 

Reply via email to