# branch-prediction

Published articles for branch-prediction.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Branch Prediction: Why an if Inside a Hot Loop Costs Milliseconds

DevFeed: [Branch Prediction: Why an if Inside a Hot Loop Costs Milliseconds](<https://devfeed.tech/articles/branch-prediction-why-an-if-inside-a-hot-loop-costs-milliseconds-39571.md>)

Original publisher: [Read original article](<https://ankit-rana.com/logs/19-branch-prediction-loop-unrolling/>)

Author: hello@ankit-rana.com

Published: 2026-03-21T00:00:00Z

Content type: tutorial

Language: en

Sources: [Ankit Rana | Mechanical Sympathy](<https://devfeed.tech/sources/ankit-rana-mechanical-sympathy.md>)

Topics: [cpu](<https://devfeed.tech/topics/cpu.md>), [Hardware](<https://devfeed.tech/topics/hardware.md>)

Tags: [branch](<https://devfeed.tech/tags/branch.md>), [branch-prediction](<https://devfeed.tech/tags/branch-prediction.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [cycles](<https://devfeed.tech/tags/cycles.md>), [loops](<https://devfeed.tech/tags/loops.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [performance](<https://devfeed.tech/tags/performance.md>), [pipeline](<https://devfeed.tech/tags/pipeline.md>), [pipelining](<https://devfeed.tech/tags/pipelining.md>), [sorting](<https://devfeed.tech/tags/sorting.md>), [systems](<https://devfeed.tech/tags/systems.md>), [wall-clock-time](<https://devfeed.tech/tags/wall-clock-time.md>)

### AI overview

This tutorial explains how CPU pipelining and branch prediction affect performance in hot loops. It describes the cost of mispredictions and suggests sorting data to create predictable branch patterns or using branchless techniques and loop unrolling, while recommending measurement on the target workload.

### Source excerpt

CPUs pipeline instructions and speculate on branch outcomes. A misprediction discards the speculative work and flushes the pipeline at roughly 10 to 20 cycles. In a million-iteration loop with random branch outcomes, those flushes dominate runtime. Sorting the data so the branch resolves the same way for long runs, or removing the branch entirely with a mask or 0/1 multiplier, is what recovers the time.

## Timing-safe memcmp and API parity

DevFeed: [Timing-safe memcmp and API parity](<https://devfeed.tech/articles/timing-safe-memcmp-and-api-parity-36633.md>)

Original publisher: [Read original article](<https://rdist.root.org/2014/06/24/timing-safe-memcmp-and-api-parity/>)

Author: Nate Lawson

Published: 2014-06-24T12:03:33Z

Content type: opinion

Language: en

Sources: [rdist](<https://devfeed.tech/sources/rdist.md>)

Topics: [API](<https://devfeed.tech/topics/api.md>), [C](<https://devfeed.tech/topics/c.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [vulnerability](<https://devfeed.tech/topics/vulnerability.md>), [ordering](<https://devfeed.tech/topics/ordering.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [assembly](<https://devfeed.tech/tags/assembly.md>), [branch-prediction](<https://devfeed.tech/tags/branch-prediction.md>), [c](<https://devfeed.tech/tags/c.md>), [cache](<https://devfeed.tech/tags/cache.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [developers](<https://devfeed.tech/tags/developers.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [ordering](<https://devfeed.tech/tags/ordering.md>), [performance](<https://devfeed.tech/tags/performance.md>), [security](<https://devfeed.tech/tags/security.md>), [timing](<https://devfeed.tech/tags/timing.md>)

### AI overview

The article discusses OpenBSD's timing-safe bcmp and memcmp APIs. It supports simpler constant-time equality comparison but questions timing-safe ordered comparison because compiler and CPU behavior may undermine its guarantees and because ordering secret data can indicate larger architectural problems.

### Source excerpt

OpenBSD released a new API with a timing-safe bcmp and memcmp. I strongly agree with their strategy of encouraging developers to adopt "safe" APIs, even at a slight performance loss. The strlcpy/strlcat family of functions they pioneered have been immensely helpful against overflows. Data-independent timing routines are extremely hard to get right, and the farther you are from ... Continue reading Timing-safe memcmp and API parity