2 ms·
Huh. I always check for collisions if I'm using a random number as a unique key. I guess it's faster to just not check, but it's just a single seek to check for
by erikpukinskis 11y ago
Huh. I always check for collisions if I'm using a random number as a unique key. I guess it's faster to just not check, but it's just a single seek to check for existence. It's pretty fast, and you almost never have to do it twice.
As a side benefit you can get by with shorter (more human readable, typeable, fits-in-a-tweet) keys because you correct the few collisions you get.
If the extra keyspace seek were ever a serious performance bottleneck I would just skip the collision check in that one spot and use a proper RNG as OP is suggesting. But how often does that really happen? Very few systems need to generate millions of unique keys a second.
- hawkice 11y agoIf he was actually hitting a cycle in the numbers generated, check/retry won't help -- eventually your entire network will bottleneck on DB reads without making any writes.
- anyfoo 11y agoChecking for collisions becomes hardly possible in distributed systems pretty quickly (take git as an extreme example), and provides virtually no benefit if you can trust your RNG. At those astronomical possibilities, there are many things that can go wrong before collisions become viable. Think about it: Why bother checking, when you have a somewhat similarly egregious probability that a random bit flip occurs right after your check? In the situations you encountered, it's probably simple and cheap, but in the cases where it's not (the situation in the article being a probable example), it can be expensive, error-prone complexity without gain.