---
title: "Instruction-Level Optimization: #[inline(always)]"
description: "Strategic application of Rust's #[inline(always)] attribute for instruction-level optimization, covering effective usage patterns and risks of overuse"
url: "https://mayorana.ch/en/blog/instruction-level-optimization-inline-always"
language: "en"
date: "2025-11-04"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "optimization"
  - "advanced"
fr_version: "https://mayorana.ch/fr/blog/instruction-level-optimization-inline-always.md"
---

# Instruction-Level Optimization: #[inline(always)]

Rust's `#[inline(always)]` attribute forces the compiler to inline a function's body at every call site, optimizing instruction-level performance by eliminating call overhead and exposing more optimization opportunities. I'd use it strategically in performance-critical code, but overuse carries risks to code size, compile time, and even runtime efficiency. Here's how I'd approach it.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo8-fig" viewBox="0 0 800 230" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="A normal call jumps to a separate function and returns, while inline(always) copies the function body directly into the hot loop">
<!-- style -->
<style>
.lo8-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo8-fig,[data-theme="dark"] .lo8-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo8-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo8-fig .boxac{fill:var(--box);stroke:var(--ac);stroke-width:2}
.lo8-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif}
.lo8-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif}
.lo8-fig .mut{fill:var(--mut);font:11px ui-sans-serif,system-ui,sans-serif}
.lo8-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- defs -->
<defs>
<marker id="lo8arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
</defs>
<!-- top: normal call -->
<text x="40" y="30" class="ti">Normal call, in a loop</text>
<rect x="40" y="42" width="200" height="46" rx="6" class="box"/>
<text x="140" y="70" text-anchor="middle" class="tx">parse_stream loop</text>
<path d="M240,65 L268,65" class="ln" marker-end="url(#lo8arrow)"/>
<rect x="270" y="42" width="180" height="46" rx="6" class="box"/>
<text x="360" y="70" text-anchor="middle" class="tx">jump + return</text>
<path d="M450,65 L478,65" class="ln" marker-end="url(#lo8arrow)"/>
<rect x="480" y="42" width="220" height="46" rx="6" class="box"/>
<text x="590" y="70" text-anchor="middle" class="tx">extract_bits body</text>
<text x="620" y="105" class="mut">~5–10 cycles of overhead per call</text>
<!-- bottom: inline(always) -->
<text x="40" y="150" class="ti">#[inline(always)] extract_bits</text>
<rect x="40" y="162" width="200" height="46" rx="6" class="box"/>
<text x="140" y="190" text-anchor="middle" class="tx">parse_stream loop</text>
<path d="M240,185 L268,185" class="ln" marker-end="url(#lo8arrow)"/>
<rect x="270" y="162" width="220" height="46" rx="6" class="boxac"/>
<text x="380" y="184" text-anchor="middle" class="tx">shr + and</text>
<text x="380" y="200" text-anchor="middle" class="mut">body copied inline</text>
<text x="620" y="190" class="mut">no jump — but duplicated at every call site</text>
</svg>
</div>

## Strategic Application

I'd apply `#[inline(always)]` in scenarios where:

- **Small, Hot Functions**: A tiny function called in a tight loop, where call overhead (stack setup, jumps) is significant relative to its work.
- **Optimization Opportunities**: Inlining enables the compiler to fuse the function with its caller, simplifying branches or constants.

### Example: A bit-manipulation utility in a real-time parser:

```rust
#[inline(always)]
fn extract_bits(value: u32, shift: u32, mask: u32) -> u32 {
    (value >> shift) & mask
}

fn parse_stream(data: &[u32]) -> u32 {
    let mut result = 0;
    for &val in data {
        result += extract_bits(val, 8, 0xFF); // Hot loop
    }
    result
}
```

**Why `#[inline(always)]`?**: Without inlining, each call incurs a jump and return (5-10 cycles on x86_64). Inlining reduces this to a single `shr` and `and`, and LLVM can further optimize the loop (e.g., unroll or vectorize).

**Effectiveness**: The function's simplicity ensures inlining cuts overhead, and constant propagation (if shift and mask are fixed) might eliminate redundant ops.

### Considerations:

- **Size**: `extract_bits` is small (2-3 instructions), so inlining doesn't bloat much.
- **Frequency**: Used in a hot loop, justifying the force.
- **Profile First**: I'd confirm with perf that call overhead is a bottleneck before forcing inlining.

## Downsides of Overuse

### Code Size Increase
- Inlining [duplicates](https://mayorana.ch/en/blog/efficient-duplicate-removal-vec) the function body everywhere it's called. For a larger function (e.g., 20 instructions) called 100 times, the binary grows by 2,000 instructions, bloating the instruction cache (I-cache).
- **Impact**: More I-cache misses, slowing execution despite fewer calls.

### Compile Time
- LLVM must optimize each inlined instance, increasing compilation time. For a large codebase with many `#[inline(always)]` annotations, builds could slow from seconds to minutes.
- **Impact**: Slower iteration, frustrating for development.

### Runtime Performance Risks
- Over-inlining large functions can disrupt I-cache locality, outweighing call savings. For example, inlining a 50-instruction function into a loop might evict other hot code.
- The compiler's heuristics (e.g., with plain `#[inline]`) often balance this better than forced inlining.

<div class="svg-container" style="margin:2rem 0;">
<svg class="lo8b-fig" viewBox="0 0 800 295" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Side-by-side view of the L1 instruction cache: a single small inlined helper leaves room for other hot code, while a large function duplicated at every call site fills the cache and evicts it">
<style>
.lo8b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .lo8b-fig,[data-theme="dark"] .lo8b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.lo8b-fig .frame{fill:none;stroke:var(--mut);stroke-width:2}
.lo8b-fig .blk{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo8b-fig .blkac{fill:var(--box);stroke:var(--ac);stroke-width:2.5}
.lo8b-fig .free{fill:none;stroke:var(--ln);stroke-width:1.5;stroke-dasharray:4 4}
.lo8b-fig .gone{fill:none;stroke:var(--ac);stroke-width:2;stroke-dasharray:4 4}
.lo8b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.lo8b-fig .ti{fill:var(--tx);font:700 13px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo8b-fig .hd{fill:var(--mut);font:700 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo8b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo8b-fig .mut{fill:var(--mut);font:500 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.lo8b-fig .ac{fill:var(--ac);font:700 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
</style>
<!-- left panel: judicious inlining -->
<text x="205" y="26" class="ti">Tiny helper, forced inline</text>
<rect x="30" y="40" width="350" height="170" rx="6" class="frame"/>
<text x="205" y="60" class="hd">L1 instruction cache — 32 KB</text>
<rect x="50" y="70" width="310" height="30" rx="4" class="blk"/>
<text x="205" y="90" class="tx">parse_stream loop</text>
<rect x="50" y="104" width="310" height="30" rx="4" class="blkac"/>
<text x="205" y="124" class="ac">extract_bits — 2 instructions inlined</text>
<rect x="50" y="138" width="310" height="30" rx="4" class="blk"/>
<text x="205" y="158" class="tx">other hot code — still resident</text>
<rect x="50" y="172" width="310" height="32" rx="4" class="free"/>
<text x="205" y="192" class="mut">headroom</text>
<!-- right panel: over-inlining -->
<text x="595" y="26" class="ti">50-instruction fn, 100 call sites</text>
<rect x="420" y="40" width="350" height="170" rx="6" class="frame"/>
<text x="595" y="60" class="hd">L1 instruction cache — 32 KB</text>
<rect x="440" y="70" width="310" height="24" rx="4" class="blk"/>
<text x="595" y="87" class="tx">inlined copy #1</text>
<rect x="440" y="98" width="310" height="24" rx="4" class="blk"/>
<text x="595" y="115" class="tx">inlined copy #2</text>
<rect x="440" y="126" width="310" height="24" rx="4" class="blk"/>
<text x="595" y="143" class="tx">inlined copy #3</text>
<rect x="440" y="154" width="310" height="24" rx="4" class="blk"/>
<text x="595" y="171" class="tx">inlined copy #4 … #100</text>
<rect x="440" y="182" width="310" height="24" rx="4" class="gone"/>
<text x="595" y="199" class="ac">other hot code — evicted</text>
<!-- footer -->
<rect x="90" y="228" width="620" height="60" rx="6" class="box"/>
<text x="400" y="252" class="tx">The cache size is fixed — every duplicated body costs someone else their lines</text>
<text x="400" y="272" class="mut">Watch both numbers: size target/release/app for .text, perf stat -e iTLB-load-misses for the cost</text>
</svg>
</div>

## Mitigation Strategies

### Selective Use
- Reserve `#[inline(always)]` for tiny, frequently called functions in hot paths. Use `#[inline]` (a hint) for larger ones, trusting LLVM's judgment.
- **Example**: Don't inline a complex parser, but do inline a 2-line accessor.

### Profiling
- Use `perf stat -e instructions,cycles` or `cargo flamegraph` to identify call overhead. Only apply `#[inline(always)]` where data shows a win (e.g., 10%+ cycle reduction).
- Post-optimization, check I-cache misses (`perf stat -e iTLB-load-misses`) to ensure no regression.

### Measure Code Size
- Run `size target/release/myapp` before and after. If the `.text` section balloons (e.g., 10KB to 100KB), reconsider inlining larger functions.

### Alternatives
- Loop unrolling or iterator fusion (Rust's [zero-cost abstractions](https://mayorana.ch/en/blog/zero-cost-abstractions-rust)) can achieve similar gains without forced inlining.
- **Example**: Rewrite `parse_stream` with `fold` to let the compiler inline implicitly.

## Checking the result
### Benchmark
With criterion:

```rust
use criterion::{black_box, Criterion};
fn bench(c: &mut Criterion) {
    let data = vec![0x1234_5678; 1000];
    c.bench_function("inline_parse", |b| b.iter(|| parse_stream(black_box(&data))));
}
```

Compare with and without `#[inline(always)]`—expect tighter latency.

### Assembly
`cargo rustc --release -- --emit asm` shows `shr` and `and` in the loop, no call instructions.

### Size Check
`ls -lh` on the binary confirms minimal growth.

## When to use it, and when not to
I'd use `#[inline(always)]` for small, hot functions like `extract_bits` in tight loops, ensuring call overhead vanishes and optimizations kick in. Overuse risks bloated binaries and slow compiles, so I'd profile to justify it, fallback to `#[inline]` elsewhere, and monitor I-cache effects. This balances performance gains with maintainability and scalability in a Rust codebase.
