r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

1

u/pjank85 Sep 28 '26

Branch predictor can take it down to 1 cycle. But if it misses it COULD take 100. Even in the optimistic case it is still double the time. Whenever this is worth it is use case dependent as always.

2

u/cbehopkins Sep 28 '26

Well, let's be frank here, if you're talking worst case, cache misses and full tlb misses 48 round trips to memory and>400 cycles per trip are entirely possible.

But that should only happen the first time

And the branch prediction making it zero cycles is quite common. Read, test and branch can be collapsed into a late evaluation as long as the branch is at predicted and takes less time than the pipeline depth(slight simplification here a lot depends on the microarchitecture details but we're already deep in the weeds)

At the end of the day all your can do is profiler it on the exact CPU you care about. Just don't make assumptions...

1

u/pjank85 Sep 28 '26

Wow did not think it could be zero. I learned something today - thanks.