c++functioncachingreference

How can I document if variables passed by reference to a function need to be cached again when the function returns?


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:


Solution

  • 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