Dictionary deepCopy Issue...

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

Dictionary deepCopy Issue...

Christopher J. Demers
I ran into an interesting situation regarding the way Dictionary responds to
deepCopy.  Dictionaries being hashed collections have some interesting
characteristics if the hash changes after an element has been added.  I am
aware of those issues, and have not often had problems with that.  However I
was a little surprised to see that a deepCopy of a Dictionary can break its
hash table if the keys use the default identity based hash calculation.  I
suppose I should not have been surprised.  Look at the code below for an
illustration.  The tricky thing about this is that since the default
implementation of hash is identity based unless one implements their own
non-identity based hash they will run into this interesting situation.  The
same situation applies to the LookupTable.

I suppose this is an argument for being more particular about either using
an IdentityDictionary, or implementing a more appropriate hash method.  Also
this reinforces my feeling that deepCopy needs to be used with caution.

============
"Evaluate the code one statement at a time."

dict := Dictionary new.
obj1 := Object new.
obj2 := Object new.

dict at: obj1 put: obj2.

"This works."
dict keys do: [:each |
 dict at: each].

dictDC := dict deepCopy.

"This errors."
dictDC keys do: [:each |
 dictDC at: each].

"This fixes it, and now it will work."
dictDC rehash.
dictDC keys do: [:each |
 dictDC at: each].
============

Chris


Reply | Threaded
Open this post in threaded view
|

Re: Dictionary deepCopy Issue...

Chris Uppal-3
Christopher J. Demers wrote:

> However I was a little surprised to see that a deepCopy of a Dictionary
> can break its hash table if the keys use the default identity based hash
> calculation.

FWIW, Set has the same bug.

    -- chris