ARTICLE AD BOX
We have a service that when sending messages to us, includes a uint8_t error code. With several hundred possible codes. In our application, we translate this into about 8 categories of errors in an enum class, i.e. enum class error_type { A, B, C, ...}.
This is very straightforward to do, some variant of:
error_type translate_error_type(uint8_t error_code) { switch(error_code) { case 0: return error_type::A; case 1: return error_type::D; // ... hundreds of more cases to deal with. Unforturnely not a good translation between error_codes and our internal classes over ranges of error_code values to make optimization easy. } }In real data though, about 5 cases almost completely dominate.
How would you write this function so a compiler (gcc11 in this specific case) would generate better code than a single huge jump table to optimize for latency if one of the five dominant cases is hit? Or, is a single huge jump table essentially ultimately the best the compiler can do?
Note: unfortunately, looking at the generated assembly in gcc for marking cases [[likely]], I don't believe this functions well if multiple cases are marked as likely.
