Array append performance

bearophile bearophileHUGS at lycos.com
Mon Aug 25 14:08:50 PDT 2008


Denis Koroskin:
> I don't follow you.

After posting my message I have realized that my expressed thoughts are wrong, I am sorry. The topic looks simple, but it is not for me. This newsgroup is useful to learn intellectual humility better.


>Immutability of the slice implies that the whole array is immutable, too.<

Well, only if immutability is transitive :-)
Storing the hash value for a mutable data structure that can have references is dangerous, better to avoid doing it.
But if a data structure is transitively immutable then storing its hash value may be positive if you use lot of hashing (hash value can be computed lazily only when required, a value of 0 may mean not computed yet. Python uses -1 for such purpose).
Finding some compromise between those two extrema seems tricky to me.
D 2.0 strings are immutable, so storing their hash value may be positive. The presence of such stored hash value may even be activated or not by a global compilation constant given to the compiler :o)


> Besides, how often do you take a hash of an array other than string?

In D I have never done it, so I presume not often.
(In Python I use the hash value of immutable arrays often, because they are more handy than D structs.)

Simple related note: capacity of the array must not be used to compute its hash value.

Bye,
bearophile



More information about the Digitalmars-d mailing list