Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2019-11-11 13:52:32


Mathias Gaunard <mathias.gaunard_at_[hidden]> wrote:
> On Sun, 10 Nov 2019 at 14:16, Phil Endecott via Boost
> <boost_at_[hidden]> wrote:
>>
>> Specifically thinking about strings, there are numerous ways
>> that it could be done. Starting with
>
> Even for integers there are multiple ways it could be done.
> It's up to the compiler to decide what is best.

Yes, it's important to remember that anything that converts
to a conventional switch statement (for example, by hashing
the string) then relies on how the compiler chooses to
implement it. There are probably different implementations
for sparse vs. dense case values, for example. If the
compiler turns a sparse switch statement into an if-else-if
chain, you might as well have an if-else-if chain in the
first place (in terms of performance).

>> (5) hashing, and then checking:
>>
>> switch (hash(s)) {
>> case "aa"HASH: if (s=="aa") f(); break;
>> case "ab"HASH: if (s=="ab") g(); break;
>> case "xy"HASH: if (s=="xy") h(); break;
>> }
>
> That code doesn't really need to check, if your hashes collide the
> compiler will reject the code.

You need to check if s might not match any of the case labels
but might collide with them.

Regards, Phil.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk