WeakRegistry>>finalizeValues & WeakKeyDictionary

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

WeakRegistry>>finalizeValues & WeakKeyDictionary

Igor Stasenko
I found few questionable places in WeakRegistry/WeakKeyDictionary
which want to discuss with list.

a WeakRegistry uses associationsDo:  to determine which keys are GCed,
and to collect values needed to send #finalize.

There are at least two things to object:
- why WeakRegistry shows up the keys = nil in associationsDo:, while
in #keys, #keysDo: it just skips over them, indicating that there no
associations with nil keys? This breaks the behavior consistency IMHO.

- if you carefully check the WeakRegistry>>finalizeValues method,
you'll see that dictionary will be scanned twice (first time in
associationsDo:, second time in valueDictionary>>finalizeValues: ).
Its scanned once only in case if there's no keys=nil and no values
needed to be finalized.

For both issues above, i think its better to add a method into
WeakKeyDictionary , lets say
#collectNilKeyValues , which will do good things for WeakRegistry by
cleaning itself from nil keys and at same time return a collection of
values needed to be finalized.

And then, there will be no need to show up keys = nil in
#associationsDo: and WeakKeyDictionary will behave as if there no
keys=nil for any request outside.

I'm also wonder, why WeakRegistry uses WeakKeyDictionary instead of
WeakIdentityKeyDictionary.

Just consider example:

WeakRegistry default add: myObject executor: myExecutor.
WeakRegistry default add: myObject copy executor: myCopyExecutor.
(suppose myObject = myObject copy return true , and myObject hash =
myObject copy hash too)

I din't tested it, but i think that in result you'll get
myCopyExecutor>>finalize sent when myObject will be GCed, but not its
copy.

--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: WeakRegistry>>finalizeValues & WeakKeyDictionary

Igor Stasenko
oops, found a typo:

- why __WeakKeyDictionary___ shows up the keys = nil in associationsDo:, while
in #keys, #keysDo: it just skips over them, indicating that there no
associations with nil keys? This breaks the behavior consistency IMHO.

Reply | Threaded
Open this post in threaded view
|

Re: WeakRegistry>>finalizeValues & WeakKeyDictionary

Igor Stasenko
Just as follow-up, i wrote test which fails randomly:

weakIdentityKeysTestFor: aClass
        | d k1 k2 |
        d := aClass new.

        k1 := 'x' copy.
        k2 := k1 copy.

        d at: k1 put: 1.
        d at: k2 put: 2.

        self assert:  (d at: k1) == 1 .
        self assert:  (d at: k2) == 2 .

        d at: k1 put: k2.
        k1 := nil.
        k2 := nil.
        Smalltalk garbageCollect.
        self assert:  d size = 2 .
        self assert:  (d includesKey: nil) not. <<< fails here randomly
        self assert: ( d values includes: 2).
        self assert: ( d values includes: 1) not.
        d finalizeValues.
        self assert: d size = 1 .
        Smalltalk garbageCollect.
        d finalizeValues.
        self assert:  d size = 0 .

an argument passed to function is WeakIdentityKeyDictionary.
So, the question is: can WeakKeyDictionary show keys=nil for any
requests or not?
I suggest, that since nil reserved for GCed key values, it must raise
an error whenever someone going to put/get value with nil key.

--
Best regards,
Igor Stasenko AKA sig.