MCU XIP Performance Gap: Analysis and Real-World Benchmark Results

    This article explains why MCU benchmark scores often fail to match real firmware performance, focusing on the gap between zero-wait-state SRAM execution and XIP (eXecute-In-Place) from external Flash. It breaks down where XIP latency comes from, outlines a reproducible test setup that changes only code placement, and presents measured results showing a small single-digit gap when cache and prefetch are properly configured. Readers will find a practical optimization checklist and selection guidance for deciding when to keep main code in external Flash.

    1. Background: Benchmark Environment vs. Real Workloads

    A common frustration during embedded MCU selection is that the compute performance numbers in datasheets and marketing materials can never be reproduced in a real project. The cause is usually not inflated specs, but a mismatch between where the benchmark code runs and where production firmware code runs.

    Most microcontrollers integrate an on-chip SRAM. Because it sits on the same die as the core with fully controlled timing, it is typically treated as a “zero-wait-state region” where instruction fetch adds almost no extra latency. Externally extended storage (usually serial or parallel Flash) has inherent delays in physical interface speed, command protocol overhead, and bus arbitration, so it is classified as a “non-zero-wait-state region.” To make their numbers look good, vendors habitually move entire benchmarks such as Dhrystone and CoreMark into SRAM. The advertised figures therefore reflect a theoretical upper bound. Once user firmware exceeds the internal SRAM capacity and most code is forced to run from external Flash via XIP (eXecute-In-Place), the experience degrades noticeably.

    It is worth stressing that XIP itself is a mature and necessary technique: it lets the core fetch instructions directly from external storage without copying code into RAM first, saving boot time and precious on-chip memory. The real question is not “can it XIP,” but how large the XIP efficiency loss is, whether it is controllable, and how to reduce it to an acceptable range at the architecture level.

    2. Mechanism: Where XIP Slows Down

    To judge whether a gap is reasonable, first understand where the latency comes from. The instruction fetch path for external Flash XIP typically includes:

    1. Interface and protocol overhead: Serial interfaces transfer commands, addresses, and data frame by frame on a bit clock; even parallel interfaces have setup time and bus turnaround.
    2. Wait-state configuration: The bus controller inserts wait states for slower peripherals, and poor configuration directly amplifies latency.
    3. Repeated fetches without on-chip cache: Without a cache, the core traverses the external bus for every instruction, and loops repeatedly cross the interface.
    4. Bus contention: Arbitration waits occur when DMA, peripheral access, and instruction fetch share the bus.
    5. Pipeline flushes from branches and jumps: When latency cannot be hidden by sequential prefetch, the loss is further amplified.

    The countermeasure in modern MCUs is instruction prefetch buffers and cache: sequential fetches are moved in batches ahead of time, and hot code stays resident in cache so most fetches hit on-chip storage. This is important to understand—actual XIP performance depends heavily on whether cache/prefetch is enabled, whether capacity is sufficient, and whether code locality is good, rather than being a fixed coefficient.

    3. Verification Method: A Reproducible Comparison Design

    Credible conclusions require controlled variables. A recommended process:

    • The same benchmark program, the same compiler optimization level (e.g., -O2), the same clock tree configuration, changing only the code section placement address in the linker script.
    • Two layouts: Group A links all code into the zero-wait-state region of internal SRAM; Group B links code into the XIP region of external Flash.
    • Warm-up and multi-round averaging: Run several warm-up rounds first, then take the arithmetic mean over multiple rounds to avoid contaminating data with first-access and cold-cache effects.
    • Record the environment triple: Core frequency, cache/prefetch on/off state, and external storage interface mode with wait states.

    An illustrative linker script snippet (showing only the “section placement” idea):

    1. /* Group A: critical code placed in internal SRAM */
    2. SECTIONS {
    3. .fast_text 0x20000000 : { *(.fast_text) } > SRAM
    4. }
    5. /* Group B: code stays in external Flash, executed in place */
    6. SECTIONS {
    7. .xip_text 0x90000000 : { *(.text) } > EXT_FLASH
    8. }

    Pair this with a minimal cycle counter sample to cross-check benchmark results:

    1. uint32_t t0 = DWT->CYCCNT;
    2. run_benchmark();
    3. uint32_t t1 = DWT->CYCCNT;
    4. printf("cycles = %u\n", (unsigned)(t1 - t0));

    4. Measured Observations and Interpretation

    In a typical measurement, running from the SRAM zero-wait-state region produced about 4702 points, even higher than the officially advertised 3965. Switching to external Flash XIP produced about 4570 points. The two differ by about 132 points, roughly a 2.8% gap.

    These numbers convey three conclusions:

    1. The gap is far smaller than intuition suggests. When cache/prefetch is effective and code access has good locality, the XIP loss is a single-digit percentage, not “cut in half.”
    2. Conservative advertised values are not unusual. Differences in the test environment (frequency, optimization level, cache configuration) are enough to make measured results exceed the nominal figure, so the nominal value is better treated as a lower-bound reference.
    3. Configuration, not the medium, decides success. With cache disabled and wait states misconfigured, XIP can be several times slower; configured properly, in-place execution from external Flash can support fairly demanding real-time workloads.

    Note that the figures above come from a specific platform and a specific benchmark and should not be treated as universal constants. Pipeline depth, cache capacity, and interface width differ greatly across cores, so always re-measure on your own hardware.

    5. Engineering Optimization Checklist

    If measured XIP loss is too large, work through the following from lowest to highest cost:

    • Enable and tune cache/prefetch: Confirm that enable bits, line size, and replacement policy match the current medium’s characteristics.
    • Fix wait states and clock dividers: Configure per the datasheet timing diagrams, avoiding excessive conservatism.
    • Layer code placement: Put interrupt vectors, real-time ISRs, and hard-real-time paths such as cryptography and motor control in the zero-wait-state region, leaving the rest in the XIP region.
    • Inline and align hot functions: Reduce branches and align hot loops to cache lines to improve hit rate.
    • Place critical data in internal RAM: Avoid data access contending with instruction fetch for the external bus.
    • Use a “copy + XIP” hybrid when necessary: Copy a small set of critical sections to RAM during boot, while the main body still executes in place.
    • Use the cycle counter for regression: Fold the benchmark into continuous integration to prevent configuration regressions.

    6. Conclusion and Selection Guidance

    There is no insurmountable gap between “zero-wait-state benchmark scores” and “XIP measured scores.” What matters more in engineering is not any nominal number, but a reproducible comparison process: fix compiler options and clock configuration, change only code placement, and cross-validate with multi-round averages and cycle counts. When the gap is held to a single-digit percentage, it is perfectly safe to keep the main code in external Flash for in-place execution, reserving the precious on-chip SRAM for data and hard-real-time paths.

    On the cloud side, the same methodology applies: whether using container platforms to host build pipelines, or using object storage to archive firmware versions and tracking performance regressions through logging and monitoring alerts, the general capabilities offered by Baidu AI Cloud can help teams turn “performance verification” into a sustainable, traceable engineering step rather than a one-off marketing number.