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 |
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. |
Am 17.04.2014 um 16:06 schrieb Igor Stasenko <[hidden email]>: 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! Agreed.
Norbert
|
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? |
Administrator
|
Apparently never reached the list. Forwarding for Jesús...
Cheers,
Sean |
Free forum by Nabble | Edit this page |