It's one of those gotcha's one will only run into when performance
is critical

I find it especially interesting because it implies that
(de)serializing two System.Objects is noticeably faster than
serializing a generic struct holding the same two items. (Unless the
performance problem is in the bucket initialization after all, not in
the object[] vs. KeyValuePair[] issue.)

       One could use the enumerator indeed, but if I add an item, is it
always appended to the list? IMHO undefined. The Dictionary<T,U> docs say:
"For purposes of enumeration, each item in the dictionary is treated as a
KeyValuePair structure representing a value and its key. The order in which
the items are returned is undefined."

       So it's undefined what order the items are returned. :)

Ah, I like this discussion :)

Right, the order is undefined, and I would even expect it to change
sometimes when adding items, because I expect Dictionary to reorganize
its internal data structures as the number of items grows. But I also
reasonably expect the enumeration order to stay the same when I don't
change the dictionary, e.g. when I just enumerate all items twice in a
row [1].

And when I consider that the deserialized dictionary should be an
unchanged copy of the original, then I would expect the serialization
order to be the same :)

However, it's moot anyway. I mean who would deserialize a dictionary
and then rely on the enumeration order? So, I don't have any problems
with your implementation, I just wanted to point out that this is the
only reason I can think of for why the Dictionary deserialization
constructor actively initializes all the buckets before adding the
deserialized items.

Fabian

[1] I can assume this, because there is no reason why it shouldn't be
the case, provided that Dictionary's implementation of GetEnumerator
doesn't use randomization [2].
[2] Okay, somebody could come up with the idea of actively randomizing
in GetEnumerator in order to get people to refrain from relying on the
order. But that would be really strange. (Although stranger things
have happened.)

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to