skip to content

In C++, why is passing a small trivially copyable Vec3 by value often faster than by const reference, and when is it not?

level: middleimportance: should knowfreq 50%

answer

  1. registers versus an address
  2. the calling convention decides
  3. a reference can alias the output
  4. cheap to copy, small, trivial
  5. inlining removes the convention, not the alias

basics

~20 s

Passed by value, a 12-byte trivially copyable Vec3 usually travels in registers as a private copy the callee never has to reload. By const reference it is an address that might alias other writes. Large or costly-to-copy types still go by reference.

solid answer

~50 s

By const reference the callee receives an **address** and loads every field from memory. By value, a small trivially copyable struct is passed in **registers** on common 64-bit calling conventions: on x86-64 System V a `Vec3 {float x, y, z;}` arrives in two SSE registers, and on arm64 each float gets its own register. The second gain is **aliasing**: a `const Vec3&` may refer to memory the function writes, so after each such store the compiler must reload it; a by-value copy can stay in registers across a loop. It is not always a win: Windows x64 passes a 12-byte struct as a pointer to a caller-made copy, large types cost a real copy, types with a non-trivial copy constructor or destructor go through memory, and inlining removes the register difference, though not the aliasing one. Rule of thumb: small and trivially copyable by value, everything else by `const&`.

code

cpp · 24 lines
cpp
#include <cstdio>
#include <vector>

struct Vec3 { float x, y, z; };  // 12 bytes, trivially copyable

// By reference: s may alias an element of out, so the compiler
// must reload s.x, s.y and s.z after every store into out.
void scaleRef(std::vector<Vec3>& out, const Vec3& s) {
  for (Vec3& v : out) { v.x *= s.x; v.y *= s.y; v.z *= s.z; }
}

// By value: s is a private copy, so it can stay in registers.
void scaleVal(std::vector<Vec3>& out, Vec3 s) {
  for (Vec3& v : out) { v.x *= s.x; v.y *= s.y; v.z *= s.z; }
}

int main() {
  std::vector<Vec3> a{{2, 2, 2}, {3, 3, 3}};
  std::vector<Vec3> b = a;
  scaleRef(a, a[0]);  // a[0] becomes {4,4,4}; a[1] is then scaled by 4
  scaleVal(b, b[0]);  // the copy stays {2,2,2}
  std::printf("by reference: a[1].x = %g\n", a[1].x);
  std::printf("by value:     b[1].x = %g\n", b[1].x);
}

go deeper

for a junior

Know that small cheap types such as int, string_view and a small Vec3 are normally passed by value and big ones by const reference.

for a middle

Explain registers versus an address under the calling convention, and the aliasing reloads a const reference forces.

for a senior

Name where the rule breaks: Windows x64 hidden references, non-trivially copyable types, large structs; know that inlining removes only the register difference, and confirm in the assembly.

for a principal

Standardise a parameter-passing guideline for the math library and enforce it in review, with trivially-copyable static_asserts on value types.

## What the two signatures compile to Take `struct Vec3 { float x, y, z; };`, 12 bytes and trivially copyable, and two functions that differ only in the parameter type: ```cpp float dotV(Vec3 a, Vec3 b); float dotR(const Vec3& a, const Vec3& b); ``` A reference parameter is, at the machine level, an address. A by-value parameter is whatever the platform's **calling convention** says for that type, and that is an ABI rule, not a C++ one: | Platform convention | `Vec3` by value | `const Vec3&` | |---|---|---| | x86-64 System V (Linux, macOS) | two SSE registers: x and y packed in one, z in the next | address in a general register, fields loaded from memory | | arm64 (as compiled here, Apple's AArch64 ABI) | three float registers, one per field | address, fields loaded from memory | | Windows x64 | a pointer to a copy the caller makes, because 12 is not 1, 2, 4 or 8 bytes | address | The x86-64 listing below shows it: `dotV` multiplies registers directly, `dotR` starts with loads through `rdi` and `rsi`. ## Aliasing: the bigger reason The register saving is small. The aliasing effect can be large. Consider scaling every element of a vector: - `void scale(std::vector<Vec3>& out, const Vec3& s)` cannot assume `s` is unrelated to `out`. After storing into `out[i]`, the compiler must assume `s` might have changed, so it **reloads** `s.x`, `s.y` and `s.z` every iteration, or emits a runtime overlap check before any vectorised path. - `void scale(std::vector<Vec3>& out, Vec3 s)` gets a private copy. Nothing the loop writes can change it, so the three factors live in registers for the whole loop. The difference is not only speed. If a caller passes an element of the same vector, `scale(v, v[0])`, the reference version changes `v[0]` first and then scales later elements by the new value. The runnable example prints 12 for the reference version and 6 for the value version. ## When by value loses 1. **Large types.** Copying a 4 KB matrix costs more than any register saving; pass it by `const&`. 2. **Types with a non-trivial copy constructor or destructor.** The Itanium C++ ABI used on Linux and macOS passes such types by invisible reference to a temporary, and making the copy runs user code. `std::string` and `std::vector` belong here. 3. **Conventions that pass by hidden reference.** On Windows x64 a 12-byte `Vec3` by value becomes a pointer to a temporary copy, so the register benefit disappears while the aliasing benefit remains. 4. **The call is inlined.** Once the body is pasted into the caller there is no calling convention, so the register difference disappears. The aliasing difference does not: a reference still names the caller's object, and unless the optimiser can prove the objects are distinct it must still reload. ## A rule of thumb that survives review - Pass by value: built-in types, small trivially copyable structs (two or three machine words), `std::string_view`, `std::span`, iterators and small function objects. - Pass by `const&`: anything large, anything that allocates on copy, anything with a non-trivial copy. - Pass by value and move when the function stores its own copy, such as a constructor taking a `std::string` it keeps. The general by-value versus by-reference rules and reference binding are covered with references; this question is about what the choice costs in a hot math library. ## Return values follow the same rules Returning a small trivially copyable struct by value is just as cheap. On x86-64 System V, `Vec3 add(Vec3 a, Vec3 b)` compiles to two instructions and a return: `addps` adds x and y in one register, `addss` adds z in the next, and the result goes back in those same two registers. There is no reason to write `void add(const Vec3& a, const Vec3& b, Vec3& out)` for speed; the output parameter reintroduces exactly the aliasing question by-value avoids, because `out` might be `a`. ## How to check a specific case Compile the function with `-O2 -S` for each target you ship, or use a compiler explorer with several compilers side by side, and look for memory loads at the start of the function and inside its loops. A `static_assert(std::is_trivially_copyable_v<Vec3>)` next to the type documents the assumption that makes by-value cheap.

  • Why is std::string_view conventionally passed by value but std::string by const reference?
    A `std::string_view` is a pointer and a length, trivially copyable and two words wide, so on mainstream 64-bit conventions it travels in two registers and has no aliasing questions about its own fields. A `std::string` copy may allocate and must run a non-trivial copy constructor and destructor, so passing it by value costs real work unless the function keeps the copy.
  • Does adding __restrict to the reference parameter give the same benefit as passing by value?
    It can tell GCC and Clang that the reference does not alias other pointers, which removes the reloads, but `__restrict` is a compiler extension, not standard C++, and it is a promise the caller can break silently, with undefined behaviour as the result. Passing a small type by value gets the same freedom with no promise to keep.

  • Passing by const reference is always cheaper because nothing is copied.
  • A const Vec3& parameter cannot change while the function runs.
  • Every 64-bit platform passes a 12-byte struct in registers.
  • By-value parameters are copied onto the heap.
  • Windows x64 passes a 12-byte struct by value in registers, just like System V.