logoalt Hacker News

Quadrupling code performance with a "useless" if

81 pointsby birdculturetoday at 7:37 AM11 commentsview on HN

Comments

throawayonthetoday at 10:58 AM

lobsters comment points out [[unlikely]] works here for clang

https://clang.godbolt.org/z/r4xYWfPfe

edit: oh the article also mentions it now :)

mcvtoday at 10:08 AM

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.

show 1 reply
anematodetoday at 8:07 AM

Brilliant! Hadn't seen this technique before.

anthonjtoday at 9:47 AM

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.

show 3 replies
anirudhak47today at 8:21 AM

latency optimization is a skill. I liked how you went till CSE pass. I myself wrote several passes to go to lowest latency possible

akoboldfryingtoday at 8:34 AM

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.

6510today at 11:07 AM

my js brain keeps thinking encoding[i] = next_j[i][j];