News for hackers. Stuff that matters.
Re:My Favourite Diff by enriquto on Sunday April 05, 2020 @11:39PM (#22790723)
Are you sure? I'd guess that if the cases are an interval of consecutive integers it may do a "jmp [c]" sometime.
Re:My Favourite Diff by KMag on Monday April 06, 2020 @12:11AM (#22790844)
You're correct. Many compilers will emit jump tables if the domain is dense enough. Also, even if compiling to a bunch of conditional jumps, they'll usually emit them to require O(log N) conditional jumps (equivalent to a binary tree of nested ifs) instead of the O(N) linear arrangement suggested by the GP.
Re:My Favourite Diff by vortico on Monday April 06, 2020 @12:22AM (#22790899)
Clang compiles both switch statements and `if` statements to the same tree search code. E.g. https://godbolt.org/z/7M_3PU My point is that the optimizations you're describing for switch statements are also applied to a bunch of `if` blocks, at least in LLVM-based languages with an LLVM version from the last several years.
Re:My Favourite Diff by KMag on Monday April 06, 2020 @03:05AM (#22791463)
Sure, I didn't mean to imply that compilers never (or commonly didn't) detect that long if-chains could be optimized to jump tables, lookup tables, or if-trees. I was just answering the more narrow question the GGP was asking: if switch statements were always lowered to if-chains and never compiled to jump tables.
Re:My Favourite Diff by vortico on Monday April 06, 2020 @03:09AM (#22791481)
Ah I see, gotcha.
Re:My Favourite Diff by vortico on Monday April 06, 2020 @12:10AM (#22790842)
Looks the same to me. https://godbolt.org/z/CfhD5k Switches were invented mostly because they were an easy hint to the compiler to reduce to a tree search, but because compilers are smart today, they're obsolete unless you just prefer their syntax.

Even if the cases are consecutive, most compiler optimizers are smart enough to determine that the ASTs are equivalent.

Re:My Favourite Diff by enriquto on Monday April 06, 2020 @01:05AM (#22791033)
> they're obsolete unless you just prefer their syntax.

This is a good point.

Sometimes I prefer the (ugly) syntax of switches than that of conditionals. The reason is that a chain of conditionals is non-symmetrical (there's no condition for the first "case"), while a switch is formally invariant to permutation of the cases. I have even seen shit like this:

    if (false) ;
    else if (c == 1) { first case ; }
    else if (c == 2) { second case; }
    ...
just to preserve the symmetry along the cases.
Re:My Favourite Diff by vortico on Monday April 06, 2020 @01:21AM (#22791083)
Are you talking about the `else` being non-symmetric? You don't need it at all. I do this.

  if (c == 1) { first case ; }
  if (c == 2) { second case; }
Re:My Favourite Diff by enriquto on Monday April 06, 2020 @02:58AM (#22791437)
...and hereby I give back my programming license and go back to civil life.
Re:My Favourite Diff by pintxo on Tuesday April 07, 2020 @09:27AM (#22802987)
This might result in another fallthrough scenario though, wouldn't it?
Re:My Favourite Diff by vortico on Tuesday April 07, 2020 @04:26PM (#22807139)
No, because `c` can't be 1 and 2 at the same time.
Re:My Favourite Diff by pintxo on Wednesday April 08, 2020 @04:18PM (#22816623)
In this specific example. But you cannot rule it out in general.