0

The following statement from the (wonderful) book c++ templates (p 109) suggests to me that passing arguments to a function by reference might compel the processor to cache the associated variables again:

Under the hood, passing an argument by reference is implemented by passing the address of the argument. Addresses are encoded compactly, and therefore transferring an address from the caller to the callee is efficient in itself. However, passing an address can create uncertainties for the compiler when it compiles the caller’s code: What is the callee doing with that address? In theory, the callee can change all the values that are “reachable” through that address. That means, that the compiler has to assume that all the values it may have cached (usually, in machine registers) are invalid after the call. Reloading all those values can be quite expensive.

The discussion surrounding the quote mentions that const-reference might also be cached again after the function call returns. After reading this statement I the following two questions came to my mind:

  • Is it possible to verify (maybe with a tool) when variables might be cached again after a function call? I know that it is possible to profile the average caching performance of a program, but I wonder whether I can associate cache performance with particular function lines?
  • Can somebody construct an example documenting that calling a function with a reference parameter invalidates the processor's cache?
fabian
  • 1,413
  • 1
  • 13
  • 24
  • 2
    Take note of this part of the quoted paragraph: "cached (usually, in machine registers)". So it's talking about keeping values in registers, not about the CPU cache. The CPU cache doesn't care if you pass around addresses - it only cares when you actually write something to memory. – sepp2k Apr 18 '21 at 17:54
  • 2
    `void foo(int &x) { x=42; }` -- any function that calls `x` and passes a reference to an `int` has no means of determining whether `foo()` modified it, and `x` has to be recached. The End. – Sam Varshavchik Apr 18 '21 at 17:58
  • 1
    Something simple: https://gcc.godbolt.org/z/rh6eqa3TK – chris Apr 18 '21 at 18:40

1 Answers1

0

You cannot in general write portable C++ code related to the CPU cache. Some microcontrollers don't have any cache. Some high-end processors have several levels of them.

Notice the GCC builtin __builtin_prefetch.

I believe some other compilers have similar things

But see also this answer. In many cases, you should not use __builtin_prefetch

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547