> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Performance optimization

> Measure one firmware budget, make a focused change, and repeat the same measurement to confirm the improvement without hiding a regression.

Use this workflow when firmware misses a budget for size, latency, throughput, or power. Choose one primary metric and a repeatable workload before you change anything. Keep correctness and secondary limits visible, so a speed gain doesn't hide a memory or power regression.

## Choose a metric

| Metric | Measure it with |
| - | - |
| Flash and RAM | Build output and the linker map. |
| Latency | Timer or GPIO markers on the target, captured by a [logic analyzer](/docs/debug-mode/logic-analyzer.md) or [oscilloscope](/docs/debug-mode/oscilloscope.md). |
| Throughput | An operation count over a defined interval. |
| Current and energy | A [PPK2](/docs/debug-mode/ppk2.md) or [Joulescope](/docs/debug-mode/joulescope.md) during a defined workload. |
| Executed paths | Instruction trace and source coverage. See [Coverage and trace](/docs/debug-mode/coverage-and-trace.md). |

Define the workload and conditions in the request:

```text theme={"theme":"css-variables"}
Measure wake-to-first-sample latency over 100 cycles using the release build
at 3.3 V. Keep the sensor at 100 Hz and logging disabled during measurement.
```

## Use the right observation

Each tool answers a different question. None of them proves a bottleneck on its own.

| Tool | Shows | Limitation |
| - | - | - |
| GDB | Program state at a point in time. | Halting and stepping change timing. |
| Bounded J-Trace capture | Instruction order for the most recent window. | Resets and runs the target, and interrupts GDB or RTT use of the probe. Instruction order isn't elapsed time. |
| Streamed ETM | Instruction order over a longer run. | Halts the target repeatedly, so it can't demonstrate real-time deadlines. |
| Source coverage | Which code executed. | Doesn't show how long it took. |
| PPK2 or Joulescope | Current and energy. | Results depend on the supply setup and settling time. |

For power, use the same supply, instrument mode, and settling time for every run. A Joulescope always needs an external supply. A PPK2 can supply the target in **Source meter** mode or measure an external supply in **Ampere meter** mode. For a long test, ask for streaming or a Joulescope JLS recording instead of one-shot captures. In VS Code, captures open in the Monitor's **Power** tab.

## Record a baseline

Save the source revision, toolchain, compiler flags, board revision, supply, workload, and measurement method. Repeat the measurement enough times to show its variation, and report more than a single favorable sample.

For size:

```text theme={"theme":"css-variables"}
Build the recorded release configuration. Report flash, initialized RAM,
zeroed RAM, and the largest application symbols from the linker map.
```

For power, with a PPK2 as the board's only supply:

```text theme={"theme":"css-variables"}
Use the PPK2 in source-meter mode at 3300 mV. Wait 100 ms for startup, then
measure five seconds of the idle workload. Repeat three times and report
average, minimum, and maximum current for each run.
```

For timing, save the trigger, channel assignments, and achieved sample rate. To watch a trend over time, use [live plots](/docs/core-concepts/plot.md), as long as the firmware's logging overhead doesn't distort the metric.

## Find a candidate

Ask Inlay to inspect the code path that accounts for the measured cost and propose one specific change. For changes to scheduling, clocks, memory layout, or architecture, run `/plan` first and review the plan in **Plan** mode before implementation.

```text theme={"theme":"css-variables"}
Inspect the packet lookup path and propose one change that can reduce the
measured latency. Preserve ordering and the public API. Include tests and
repeat the existing benchmark after implementation.
```

## Repeat the unchanged measurement

After the focused change:

1. Run the relevant correctness tests.
2. Build with the same toolchain and options.
3. Restore the same target state and workload.
4. Repeat the original measurement.
5. Compare baseline, candidate, absolute change, and run-to-run variation.
6. Check secondary limits such as image size, current, or deadline margin.

```text theme={"theme":"css-variables"}
Rerun the recorded benchmark without changing its setup. Report the before
and after distributions and whether the improvement exceeds measurement
variation. Include correctness and memory checks.
```

If the change doesn't beat the measurement's variation, treat it as no improvement.

Keep the measurement protocol and results in the project. Save a repeated benchmark as a [hardware script](/docs/debug-mode/hardware-scripts.md) so later changes use the same procedure. See [Combined workflows](/docs/debug-mode/combined-workflows.md) for relating logs, target state, and instrument captures.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.