Date: Fri, 23 May 2025 23:05:26 +0200
There is no guarantee that the value in the switch(value) instruction is one of the string constants.
If it is possible that it has the same hash, but is a different value, then an actual comparison has to follow the selection of the branch with the matching hash.
If you really want to optimize, then there are faster ways than hashing:
switch (color) {
case "red":
case "blue":
case "green":
}
only the first letter (r or b or g) or e.g. only the second letter (e or l or r) can be read to know, which one it is, iff you know, color is one of the three string values.
> So... you want it to break if there's a hash collision? I'm not sure
> if I can call that an "optimization".
The entries are known before forming the
table, so collision can be avoided by
perfect hashing.
See also https://github.com/serge-sans-paille/frozen
Received on 2025-05-23 21:13:05