---
title: "Moving a vector scan 48 bytes"
description: "On Intel C4, moving a vector scan 48 bytes in the executable cut its median time from 119 to 60 milliseconds."
source: "https://hew.sh/blog/2026/11/when-identical-instructions-run-at-different-speeds"
---

On November 22, one of our vector benchmarks took nearly twice as long after a Hew compiler update. The instruction counts agreed to four significant figures. We eventually got most of the time back by relinking the slower executable with 48 bytes of padding.

We first saw the slowdown on an Intel Emerald Rapids C4 VM. We built the same program with [the earlier compiler](https://github.com/hew-lang/hew/commit/928ff44dacc946e88c800fad6cf15cbaee7e90d9) and [the newer compiler](https://github.com/hew-lang/hew/commit/5fb9f4118edc78afc117b5393283de5aeb6b668c). The intervening changes included coroutine cleanup and parallel object emission. Both builds used the same scan from our [calibration program](https://github.com/hew-lang/hew/blob/5fb9f4118edc78afc117b5393283de5aeb6b668c/examples/benchmarks/calibration/primitives.hew#L13-L21):

```hew
fn vector_scan(iterations: i64, size: i64, values: Vec<i64>) -> i64 {
    var checksum = 0;
    for r in 0..iterations {
        for i in 0..size {
            checksum += values[i];
        }
    }
    checksum
}
```

The driver filled the vector with `i % 251` before starting the timer. Each pass read the same elements. At 4,096 elements, the vector held 32 KiB of integer data. A second benchmark, `vector_build`, constructed a fresh vector on every pass, filled it one element at a time, then summed the contents.

<figure class="benchmark-figure">
  <a href="/images/blog/cloud-benchmarks/scan-workloads.svg" title="Open full-size figure"><img src="/images/blog/cloud-benchmarks/scan-workloads.svg" alt="Timing change in the newer build across 11 paired rounds. For 64, 4,096 and 65,536 elements, scanning took 82.7%, 79.5% and 93.5% longer respectively. Building and scanning took 2.6%, 6.9% and 7.5% less time." /></a>
  <figcaption>Eleven pairs per size. The scan reused one vector. The build workload allocated and filled a fresh vector on each pass.</figcaption>
</figure>

All three sizes showed the same split. Building the vector and reading it took a little less time in the newer build. Repeatedly scanning an existing vector took substantially longer. Another scan in the suite overwrote one element between passes, at index `r % size`. Its whole-process timings at 4,096 elements had medians of 146.7 and 166.1 ms across 21 paired trials. That program included the write and process startup in its measurement. We kept the read-only version for the placement experiment because we could reproduce its larger difference in a fraction of a second.

## Inside the scan

We reproduced six LLVM functions associated with the scan and compared the optimized IR from each toolchain. All six matched byte for byte. One of those functions fetched a vector element in three instructions.

```llvm
%vector.data = load ptr, ptr %loop.output.value, align 8
%vector.element = getelementptr inbounds [8 x i8], ptr %vector.data, i64 %loop.output.value6
%vector.scalar = load i64, ptr %vector.element, align 8
```

The first load reads the address of the vector's storage. The next instruction computes the address of an eight-byte element at the requested index. The last load reads that integer. The code that follows adds it to the checksum and checks for signed overflow.

We then disassembled the outlined loop function in the two executables. Each contained 168 machine instructions. Lining them up relative to the start of the function left three differences, all in address displacements. The corresponding instructions referenced the file table, source-site table and symbol table used to report a bounds fault. The addresses of the *functions themselves* had also changed. The older loop function started 32 bytes past a 64-byte boundary. The newer one started 48 bytes in.

## Relinking the same object

We saved each build's object file, its runtime archive and the arguments used for the final link. This let us change the executable layout while keeping the compiler output fixed. For example, this command made a small x86-64 object containing 48 bytes of padding.

```bash
printf '%s\n' '.section .text.bench_layout,"axR",@progbits' \
              '.space 48,0x90' '.section .note.GNU-stack,"",@progbits' |
  cc -x assembler -c -o pad48.o -
```

We put that object immediately before the generated program object in the link command. The padding occupied space in the executable and execution never entered it. We repeated the link with 16 and 32 bytes, and kept the original layout as a fourth case. For each binary we used `nm -an` to read the loop function's address after linking. The figure groups the results by that address modulo 64. Adding 48 bytes to a function that already starts at offset 48 puts it at offset 32 in the next block.

<figure class="benchmark-figure">
  <a href="/images/blog/cloud-benchmarks/placement.svg" title="Open full-size figure"><img src="/images/blog/cloud-benchmarks/placement.svg" alt="Median user cycles at four function addresses modulo 64. At offset 32 both builds took about 200 million cycles. At offset 48 the older and newer builds took 317 and 392 million cycles respectively." /></a>
  <figcaption>The offset is the function address modulo 64, measured in bytes. Each point is the median of 16 runs.</figcaption>
</figure>

We pinned one worker to one CPU. Each execution made 16,592 passes over the vector, visited 67,960,832 elements and returned the same checksum, 8,381,614,720. After a warmup, we rotated the order of the eight binaries over 16 rounds so each layout ran at different points in the sequence. The program timed the scan itself. Linux `perf stat` counted user-mode instructions and cycles over the process.

With the newer build at its original address, the scan took 118.5 ms and about 392 million cycles. With the extra 48 bytes it took 60.5 ms and about 200 million cycles. Both retired 1.297 billion instructions. Adding 16 bytes to the older build moved it to offset 48 and raised its median time from 61.6 to 96.1 ms. At offset 32, the two builds finished within about a millisecond of each other.

## The same binaries on Ryzen

We ran the same eight executables on an AMD Ryzen 9 7950X, with 12 measured rounds per layout. The newer build's medians ranged from 41.2 to 44.1 ms on Ryzen. On the Xeon, they ranged from 60.5 to 118.5 ms. The individual runs show a fair amount of scatter on the desktop, including a 71 ms run for the layout with a 44.1 ms median.

<figure class="benchmark-figure">
  <a href="/images/blog/cloud-benchmarks/scan-processors.svg" title="Open full-size figure"><img src="/images/blog/cloud-benchmarks/scan-processors.svg" alt="Individual elapsed times of the four layouts in the newer build. The Intel runs at offset 48 took 109 to 123 ms. All other Intel layouts finished in under 74 ms. On AMD the four medians were between 41 and 45 ms." /></a>
  <figcaption>Each dot is one run of the newer build. Both CPUs ran the same four executables.</figcaption>
</figure>

The linker shifted other code along with the scan. On the Xeon, even with both functions at offset 48, their medians still differed by 22 ms. A follow-up could hold the surrounding functions in place and move just this loop. That would separate the cost of its own address from the cost of changes elsewhere in the executable. The [measurements for all 224 runs (CSV)](/images/blog/cloud-benchmarks/scan-layout-runs.csv) include the padding and function offset for each executable, along with elapsed time and the process counters.

[Part 3: Sharing byte buffers and reusing coroutine frames](/blog/2026/11/making-proven-lifetimes-cheaper/)
