Live data from Hacker News

A history of branch prediction

danluu.com

21–30 of 68 posts

Re: A history of branch prediction

#21
post #20

I really have problems reading this website. You don't have to make a website bloated to make it readable: http://bettermotherfuckingwebsite.com

Have a look at the Chromium/Chrome's extension Just Read before / after https://i.imgur.com/Ihy5wQh.png

I've tried a couple of these types of extensions, Just Read is the best one I've found so far.

Re: A history of branch prediction

#22
post #19

I really have problems reading this website. You don't have to make a website bloated to make it readable: http://bettermotherfuckingwebsite.com

This is even better, in my opinion: https://bestmotherfucking.website/ Anyway, I agree. Text should always have a max width. Long lines of text aren't really readable. I use this in my SurfingKeys settings, and use it on a lot of sites to make them more readable: mapkey(' ', 'Centers the current page', function() { document.body.style.cssText = "font-family: sans-serif !important"; document.body.style.cssText += "col…

Disagree, or at least if there's a width that's too wide then I haven't found it yet. Let the user set the width they want in their browser, don't force your own max width on them.

Re: A history of branch prediction

#23
post #22
post #19

Earlier quoted context omitted.

This is even better, in my opinion: https://bestmotherfucking.website/ Anyway, I agree. Text should always have a max width. Long lines of text aren't really readable. I use this in my SurfingKeys settings, and use it on a lot of sites to make them more readable: mapkey(' ', 'Centers the current page', function() { document.body.style.cssText = "font-family: sans-serif !important"; document.body.style.cssText += "col…

Disagree, or at least if there's a width that's too wide then I haven't found it yet. Let the user set the width they want in their browser, don't force your own max width on them.

Typography people generally agree that lines that are too long are harder to read and recommend fairly short lines, less than 15 words. They have a number of studies backing them up. Maybe you're an outlier.

Re: A history of branch prediction

#24
Figures 12 and 14 are the same but I think the figure used is only supposed to be like that for figure 14, not for figure 12.

The "two-bit" scheme that fig 12 is for does not have branch history, whereas "two-level adaptive, global" which has fig 14 fits the bill.

Re: A history of branch prediction

#25
post #17

Earlier quoted context omitted.

The compiler is (probably) smarter than you. Generally speaking, it will automatically decide which branches are most likely and arrange them accordingly (e.g. for things like for loops and while loops especially, where the biggest gains are). You'd likely gain more performance out of algorithmic changes, and then a number of other processor optimizations (like vectorization and pre-fetch hints) first. Also, CPU manu…

Whether or not it's an early optimization depends on when you add the __builtin_expect, no? Perhaps you already have identified a very hot branch in your code that the compiler fails to treat correctly even though you provided it with profile data.

[deleted]

Re: A history of branch prediction

#26

Very informative. I missed the part about 1500000 BC though – a time when our ancestors lived in the branches of trees? Another beginner-friendly explanation of the effects of branch prediction is this Stack Overflow post which compares a processor to a train: https://stackoverflow.com/questions/11227809/why-is-it-faste...

Maybe the author is referring to the apparition of the homo erectus

Re: A history of branch prediction

#27

Top quality article. Now we need one with specifics of how to write code that's aware of this. For instance when do use what compiler hints. Anyone have links or books?

There's a couple of things you can realistically do:

(1) Mark branches as likely/unlikely to be taken. eg. debug code might be marked as unlikely. The Linux kernel does this, and it's explained here (for GCC): https://stackoverflow.com/questions/109710/likely-unlikely-m... [There is some question about whether this is really worthwhile, but I trust the kernel developers ...]

(2) Use profile-guided optimization (PGO), which involves running the program, collecting information about branches and other optimizations, then recompiling with these hints. See eg: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_g...

There are also two negative things to avoid doing:

(3) Some things like threaded code (used by FORTH interpreters) and bytecode interpreters reuse the same piece of code followed by a branch, where the branch jumps to the next high level instruction. This code structure defeats branch prediction.

(4) Don't do strange stuff to the return stack, because modern processors keep track of the return stack and predict branch targets (ie. of RET instructions). A CALL with an unmatched RET or vice versa can defeat branch prediction.

I would say that doing anything else is too difficult, or means that you have to become a compiler writer, but I'm interested to know if there are any other practical techniques.

Re: A history of branch prediction

#29
post #20

I really have problems reading this website. You don't have to make a website bloated to make it readable: http://bettermotherfuckingwebsite.com

Have a look at the Chromium/Chrome's extension Just Read before / after https://i.imgur.com/Ihy5wQh.png

and for those against apps : https://app.printfriendly.com/print?source=homepage&url_s=uG...

Re: A history of branch prediction

#30
post #10

The use of previous branch history and branch address as a "context" for prediction reminds me of the very similar technique used for prediction in arithmetic compression as used in e.g. JBIG2, JPEG2000, etc. --- the goal being that, if an event X happens several times in context C, then whenever context C occurs, the probability of X is more likely. Also, since modern CPUs internally have many functional units to wh…

You could do it, but 'work' produces heat. From that point of view a branch predictor /saves/ you from spending the heat of the cases you /don't/ need to have processed. The performance per watt of such a design would probably leave it on the back of a napkin as an educated guess of how costly that would be.

Pie-in-the-sky idea here, but only irreversible computations produce heat. Maybe in the distant future we can make chips that do many parallel computations of all branches reversibly, and only make the results irreversible once the correct branch is known?
Post reply on HN