by cempaka · 2y ago · 0 comments
Haha well, fun fact, Java did this as well after a bunch of code was broken by a JDK upgrade which changed HashMap iteration order that programmers had been relying upon. Java does at least have ordered maps in the standard lib though. IMO it is a questionable decision to spend CPU resources on randomization in order to mollycoddle programmers with a flawed understanding of an API like this, but then again I'm not the one who gets the backlash when their stuff breaks.
Also, on the subject of nullability, while JSR305 may be considered dead, there's still pretty active work on the Java nullability question both from the angle of tooling (https://www.infoq.com/news/2024/08/jspecify-java-nullability...) and language design (https://openjdk.org/jeps/8316779).
OrderedDict types are nice sometimes but shouldn't be the default behavior, IMO.
There are good reasons for the second point (about default/named parameters) -- any calling code is making some assumptions based on that default value so there's a risk it can't be changed or added to. If you really want a default value, make a wrapper function for it. In the example it would be a simple matter of defining ReplaceAll with three arguments that always passes -1 to Replace(...)
% repeat 5 perl -e '%h=(a => 1, b => 2, c=> 3); print for keys %h; print "\n"'
bca
cba
bac
bac
bacOf the same vintage is `Tie::IxHash`, which retains insertion order
% repeat 5 perl -MTie::IxHash -E 'tie my %foo, "Tie::IxHash"; @foo{qw{ c a b }} = (1)x3; printf("%s%s%s\n",keys %foo)'
cab
cab
cab
cab
cab
[0] https://metacpan.org/release/NI-S/perl5.003_02a/view/pod/per...Before it was only dependent on the per-process seed. So the seed could be computed given enough attempts to iter a hash (eg a public JSON API)
That's why we don't do `load` and `load_safe`; we do `load` and `load_unsafe`.
This is incorrect (or I’m misunderstanding you). OpenJDK’s HashMap doesn’t use randomization, and the iteration order is thus deterministic under that implementation, although the API specification does not guarantee it. To mitigate DoS attacks, keys in the same hash bucket are stored as a balanced tree. For keys that implement Comparable (strings in particular), this guarantees O(n log n).
It takes about 3 ns to choose a random starting bucket, which is basically free relative to the iteration itself.
for _, k := range slices.Sorted(maps.Keys(m)) {
v := m[k]
_ = v // do something with k and v
}
though, it's not as bad as before: keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
v := m[k]
_ = v
}thats what you call overfitting kids
Plus it's in the class of things that's pretty easy to implement yourself now. There's always a huge supply of "but the library could just compose these two things for me". If you stick them all in things get bloated. You could literally have written it in the time it took to write the complaint. You got 80% of the way there as it is, I just tweaked your code a bit to turn it into an iterator: https://go.dev/play/p/agBGl_rT7XS
Weird
An "ordered map" is not a hash table. I think they want a tree.
But you can get the keys and sort them.
I really do not see the problem
Use a tree if order matters, Hash if not.
(Since I am not a Go programmer, maybe I missed something)
A map ordered by keys is a SortedMap in Java. While ordered, LinkedHashMap is not a SortedMap. In other words, unordered < ordered < sorted.
An ordered map is absolutely a hashmap.
> But you can get the keys and sort them.
That gives you a sorted thing, which is completely different.
> Use a tree if order matters, Hash if not.
That is incorrect. “Ordered” in the context of maps generally denotes the preservation of insertion order, and more rarely the ability to change that order. Trees don’t help with that, quite the opposite.
I have never heard of O(1) insert and retrieval from anything ordered.
So, no. An ordered map is not a hashmap
Then you’ve not gotten out much. Here’s one: https://docs.python.org/3/library/stdtypes.html#dict
Here’s an other one: https://docs.python.org/3/library/collections.html#collectio...
Here’s a third one: https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap...
And a fourth: https://docs.oracle.com/en/java/javase/22/docs/api/java.base...
> So, no. An ordered map is not a hashmap
Still wrong.
Blocked channel reads and writes are unblocked in FIFO order, not random. Mutexes are similar (afaict not quite identical, but the intent is the same).
Go randomizes a lot and I am thrilled they do that, and I knew about mutexes already, but the chan part was a surprise to me. It's reasonable and matches mutexes, so that's probably for the better, but still a bit oof.
This should be obvious since the buffer is optional.
> but the chan part was a surprise to me
With multiple receivers it is effectively random _which_ receiver gets the wakeup.
>With multiple receivers it is effectively random _which_ receiver gets the wakeup.
That's why I brought it up: no it isn't. It's ordered. Intentionally.
https://github.com/golang/go/issues/11506
It's pretty easy to prove to yourself too, it only takes a couple dozen lines to write a test. I'm not confident it's guaranteed in all scenarios (mutexes are not, for example), but I've yet to see it do anything but perfect FIFO when not racing on starting both read and write simultaneously.
---
A single select statement with multiple eligible channel operations is random, which is part of why I expected blocked channel operations themselves to be random. But nope.
In case Go developer switches to a different language, we don't want to build bad habits. Map key iteration should be non-deterministically deterministic.
Then they are creating fragile software. They're always one language version upgrade away from disaster.