[{"data":1,"prerenderedAt":196},["ShallowReactive",2],{"term-b\u002Fbranch-prediction":3,"related-b\u002Fbranch-prediction":180},{"id":4,"title":5,"acronym":6,"body":7,"category":161,"description":162,"difficulty":163,"extension":164,"letter":165,"meta":166,"navigation":167,"path":168,"related":169,"seo":174,"sitemap":175,"stem":178,"subcategory":6,"__hash__":179},"terms\u002Fterms\u002Fb\u002Fbranch-prediction.md","Branch Prediction",null,{"type":8,"value":9,"toc":155},"minimark",[10,15,19,23,26,30,144,148,151],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 - The Vibe Check",[16,17,18],"p",{},"Modern CPUs don't wait around to find out if an if statement is true, they guess the answer and start executing down that path before they even know for sure, because waiting would waste dozens of idle cycles. Guess right and you got a free head start, guess wrong and the CPU has to throw away all that work and start over, which is why sorting an array before a loop full of conditionals can genuinely make it faster.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"Branch prediction is a CPU technique that guesses the outcome of a conditional branch, like an if statement, before it is evaluated, allowing the processor to speculatively execute instructions down the predicted path instead of stalling the pipeline. A misprediction forces the CPU to discard the speculative work and flush the pipeline, which can cost 10 to 20 cycles on modern hardware, making predictable branch patterns meaningfully faster than data-dependent ones.",[11,27,29],{"id":28},"show-me-the-code","Show Me The Code",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-javascript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u002F\u002F Sorting data first can speed up a branch-heavy loop\n\u002F\u002F because the predictor learns the pattern\ndata.sort();\nfor (const x of data) {\n  if (x > threshold) sum += x; \u002F\u002F now highly predictable\n}\n","javascript","",[38,39,40,49,55,76,102,138],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,48],{"class":47},"sHwdD","\u002F\u002F Sorting data first can speed up a branch-heavy loop\n",[41,50,52],{"class":43,"line":51},2,[41,53,54],{"class":47},"\u002F\u002F because the predictor learns the pattern\n",[41,56,58,62,66,70,73],{"class":43,"line":57},3,[41,59,61],{"class":60},"sTEyZ","data",[41,63,65],{"class":64},"sMK4o",".",[41,67,69],{"class":68},"s2Zo4","sort",[41,71,72],{"class":60},"()",[41,74,75],{"class":64},";\n",[41,77,79,83,86,90,93,96,99],{"class":43,"line":78},4,[41,80,82],{"class":81},"s7zQu","for",[41,84,85],{"class":60}," (",[41,87,89],{"class":88},"spNyl","const",[41,91,92],{"class":60}," x ",[41,94,95],{"class":64},"of",[41,97,98],{"class":60}," data) ",[41,100,101],{"class":64},"{\n",[41,103,105,108,111,114,117,120,123,126,129,132,135],{"class":43,"line":104},5,[41,106,107],{"class":81},"  if",[41,109,85],{"class":110},"swJcz",[41,112,113],{"class":60},"x",[41,115,116],{"class":64}," >",[41,118,119],{"class":60}," threshold",[41,121,122],{"class":110},") ",[41,124,125],{"class":60},"sum",[41,127,128],{"class":64}," +=",[41,130,131],{"class":60}," x",[41,133,134],{"class":64},";",[41,136,137],{"class":47}," \u002F\u002F now highly predictable\n",[41,139,141],{"class":43,"line":140},6,[41,142,143],{"class":64},"}\n",[11,145,147],{"id":146},"when-youll-hear-this","When You'll Hear This",[16,149,150],{},"\"Sorting the array first made the branch predictor's job trivial and doubled our speed.\" \u002F \"That famous benchmark where sorted data runs faster is all branch prediction.\"",[152,153,154],"style",{},"html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":51,"depth":51,"links":156},[157,158,159,160],{"id":13,"depth":51,"text":14},{"id":21,"depth":51,"text":22},{"id":28,"depth":51,"text":29},{"id":146,"depth":51,"text":147},"backend","Modern CPUs don't wait around to find out if an if statement is true, they guess the answer and start executing down that path before they even know for su...","advanced","md","b",{},true,"\u002Fterms\u002Fb\u002Fbranch-prediction",[170,171,172,173],"Cache Line","Perf Counters","SIMD","Latency",{"title":5,"description":162},{"changefreq":176,"priority":177},"weekly",0.7,"terms\u002Fb\u002Fbranch-prediction","UXs-HTUwXNFv4UoopZA5fkUxHBj6KJ3aPAwm5_bCT5E",[181,184,189,192],{"title":170,"path":182,"acronym":6,"category":161,"difficulty":163,"description":183},"\u002Fterms\u002Fc\u002Fcache-line","Your CPU never fetches just the one byte you asked for, it grabs a whole chunk around it, usually 64 bytes, called a cache line, on the theory that you'll...",{"title":173,"path":185,"acronym":6,"category":186,"difficulty":187,"description":188},"\u002Fterms\u002Fl\u002Flatency","networking","beginner","Latency is the delay before data starts moving — the time it takes for a request to go from your device to the server and back.",{"title":171,"path":190,"acronym":6,"category":161,"difficulty":163,"description":191},"\u002Fterms\u002Fp\u002Fperf-counters","Perf counters are the CPU's own built-in scorekeepers, tiny hardware registers that silently count things like cache misses, branch mispredictions, and ins...",{"title":172,"path":193,"acronym":194,"category":161,"difficulty":163,"description":195},"\u002Fterms\u002Fs\u002Fsimd","Single Instruction, Multiple Data","SIMD is a CPU trick where instead of adding two numbers at a time, the processor adds eight, or sixteen, or thirty-two pairs of numbers in the exact same i...",1792528066362]