Quadrupling code performance with a "useless" if

(purplesyringa.moe)

60 points | by birdculture 2 hours ago

5 comments

  • mcv 15 minutes ago
    Interesting. I thought modern CPU optimisation required avoiding branches, but here adding the branch allows the branch pediction to parallelise what it otherwise couldn't.
  • anthonj 36 minutes ago
    I think this call for something similar to "__builtin_expect" or linux' likely()/unlikely().

    Not very clean, but better than inserting obscure optimisations in the source.

    • purplesyringa 24 minutes ago
      That would be great, if only it worked as intended! From the perspective of an optimizing compiler, `a == b ? a : b` is worse than `b` regardless of the probability you assign to `a == b`.
    • gblargg 35 minutes ago
      Assuming compilers are smart enough to insert an unnecessary branch to break the dependency.
    • MaxBarraclough 24 minutes ago
      I was wondering the same thing. Also, could profile-guided optimisation help here?
  • anematode 2 hours ago
    Brilliant! Hadn't seen this technique before.
  • anirudhak47 2 hours ago
    latency optimization is a skill. I liked how you went till CSE pass. I myself wrote several passes to go to lowest latency possible
  • akoboldfrying 1 hour ago
    This is really surprising! I've never considered the possibility that using an equality test to skip a write that would be a no-op could break a dependency and thus lead to higher perf overall if the "equal" outcome occurs often enough. This might be applicable in many situations where you "edit" some data in-place, but most of the time there are few or no changes.