3 ms·
It makes execution reproducible.
by lightgreen 6y ago
It makes execution reproducible.
- anonymoushn 6y agoAlmost any hash table implementation will give the same iteration order for the same sequence of insertions and deletions across program executions. So this would already be true even if the order was not the insertion order. Golang starts iterations in a random position each run to prevent people from relying on the ordering.
- lightgreen 6y agoNot “almost any”. Good hashtables intentionally mix random numbers into hashes to prevent DOS attacks. And also, even if hashtables are stable, the hashes of the objects might not be (depend on the addresses for example).
- arnvald 6y agoI think Go does it very well here (since they don't want to guarantee the order) - getting consistent order in almost every case is the worst of both worlds, because people won't learn about the underlying implementation, they'll notice that it seems ordered and will rely on this behaviour.
- anonymoushn 6y agoAs lightgreen points out in a sibling comment, it's common recently for languages to use a secure randomized hash like siphash and generate different orderings per program execution. It seems like that's the approach more languages should take.