Understanding WeakValueDictionary

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

Understanding WeakValueDictionary

NorbertHartl
While looking at the code in voyage I got to WeakValueDictionary as voyage uses it as object cache. If I looked correctly the WeakValueDictionary is based on equality and not identity. Is that correct? Feels wrong!

Norbert


Reply | Threaded
Open this post in threaded view
|

Re: Understanding WeakValueDictionary

Igor Stasenko

On 17 April 2014 10:26, Norbert Hartl <[hidden email]> wrote:
While looking at the code in voyage I got to WeakValueDictionary as voyage uses it as object cache. If I looked correctly the WeakValueDictionary is based on equality and not identity. Is that correct? Feels wrong!


well, comparison used for keys..
and i agree that for caching, an identity comparison makes much more sense,
since you want it to be fast, and identity comparison is no doubt faster than
equality comparison, especially if keys are complex objects which implement equality based on comparing own fields.

but for that, i think we would need new kind of dictionary, e.g.
WeakValueIdentityKeyDictionary (or some similar name). 

Btw, for caching i prefer to use round-robin cache with limited size
(like that cache never grows too big, and instead simply drops/overrides existing entries when it become full).. this helps maintaining cache size under control.

A good example is LRUCache, which is very suitable method to cache objects.. albeit at cost of some extra processing for accessing it.
 
Norbert

--
Best regards,
Igor Stasenko.
Reply | Threaded
Open this post in threaded view
|

Re: Understanding WeakValueDictionary

NorbertHartl

Am 17.04.2014 um 16:06 schrieb Igor Stasenko <[hidden email]>:


On 17 April 2014 10:26, Norbert Hartl <[hidden email]> wrote:
While looking at the code in voyage I got to WeakValueDictionary as voyage uses it as object cache. If I looked correctly the WeakValueDictionary is based on equality and not identity. Is that correct? Feels wrong!


well, comparison used for keys..
and i agree that for caching, an identity comparison makes much more sense,
since you want it to be fast, and identity comparison is no doubt faster than
equality comparison, especially if keys are complex objects which implement equality based on comparing own fields.

Agreed. But I think that equality based comparison is not only slower but wrong! Implementing an object cache based on equality can easily screw up your graph. You can call it the crappiest kind of become: you’ve never wanted!

but for that, i think we would need new kind of dictionary, e.g.
WeakValueIdentityKeyDictionary (or some similar name). 

Agreed.

Btw, for caching i prefer to use round-robin cache with limited size

You mean those things where you can state: „Hey, I’m fast because I exchanged * with + ?“ :)

Norbert

(like that cache never grows too big, and instead simply drops/overrides existing entries when it become full).. this helps maintaining cache size under control.

A good example is LRUCache, which is very suitable method to cache objects.. albeit at cost of some extra processing for accessing it.
 
Norbert

--
Best regards,
Igor Stasenko.

Reply | Threaded
Open this post in threaded view
|

Re: Understanding WeakValueDictionary

Jesús Marí
I'm trying to make a voyage version for CouchDB and I've copied the VoyageMongoCache class, but I've been experimenting estrange behavior using this class, because of the "equality" of the objects.
What I have done is to change the class of reversedObject from WeakKeyDictionary to WeakIdentityKeyDictionary and the method

at: anOID put: anObject
        self compactIfNeeded.
        self mutex critical: [
                objects at: anOID put: anObject.
                reversedObjects at: anObject put: anOID ].

to:  
at: anUUID put: anObject

        self compactIfNeeded.
        self mutex critical: [
                reversedObjects at: anObject ifAbsent:[
                                        objects at: anUUID put: anObject.
                                        reversedObjects at: anObject put:anUUID] ].

so now there is only one instance of an object in the cache. May be it could be useful for you?
Reply | Threaded
Open this post in threaded view
|

Re: Understanding WeakValueDictionary

Sean P. DeNigris
Administrator
Apparently never reached the list. Forwarding for Jesús...

Jesús wrote
I'm trying to make a voyage version for CouchDB and I've copied the VoyageMongoCache class, but I've been experimenting estrange behavior using this class, because of the "equality" of the objects.
What I have done is to change the class of reversedObject from WeakKeyDictionary to WeakIdentityKeyDictionary and the method

at: anOID put: anObject
        self compactIfNeeded.
        self mutex critical: [
                objects at: anOID put: anObject.
                reversedObjects at: anObject put: anOID ].

to:  
at: anUUID put: anObject

        self compactIfNeeded.
        self mutex critical: [
                reversedObjects at: anObject ifAbsent:[
                                        objects at: anUUID put: anObject.
                                        reversedObjects at: anObject put:anUUID] ].

so now there is only one instance of an object in the cache. May be it could be useful for you?
Cheers,
Sean