2

I had asked previously a question on stackoverflow (if you are interested here's the link: Passing by reference "advanced" concept? )

Interestingly, one of the answers intrigued me and I felt it deserves a separate question.

const int& x = 40;

If 40 happens to be a value in the CPU cache (rvalue). Then would you, by writing that line, just reserved cache memory to hold the number 40 for the lifetime of your process? And isn't that a bad thing?

Thank you

Community
  • 1
  • 1
Kam
  • 5,878
  • 10
  • 53
  • 97
  • It seems like this was answered in the other question: "const int& x = 40; has the same semantics as int = 40;const int& x = ; But that has no bearing on what the compiler actually implements because of the as is rule that allows the compiler to remove all junk." @loki-astari In the other question, they were just saying you could think of the value being held in cache, not that it actually was –  Nov 11 '12 at 15:14
  • 2
    There is no "cache" in C++... – Kerrek SB Nov 11 '12 at 15:20
  • possible duplicate of [Passing by reference "advanced" concept?](http://stackoverflow.com/questions/13332101/passing-by-reference-advanced-concept) – RivieraKid Nov 11 '12 at 20:47

2 Answers2

7

The literal 40 almost certainly lives in some read-only memory, possibly in the assembler (for small values there are typically instructions which can set a register or address; for bigger values it would be living somewhere as constant). It doesn't live "in the cache". When you create a const reference to it, a temporary is constructed wherever the compiler sees fit to keep temporaries (probably on the stack). Whether this lives in any cache is up to the system.

If the address of this temporary is never taken, it may actually not even be created: All rules in the C++ standard are governed by the "as if"-rule. As a result, the reference and the literal would be identical. If the address of the const reference is ever taken, the compiler needs to decide where to put the object and you may, indeed, see a small performance impact.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
5

You can't reserve space on the cache from your program

It isn't really in your control. The cache-control decisions are made by its own controller which studies temporal and spatial locality, among other things to decide which cache-lines to replace and which to keep.

There are usually multiple copies of your data, on different caches and the virtual-memory address-space (maps to physical memory + swap).


The way memory is managed is far more complex than that. The system generates a virtual address every-time, when dealing with memory.

This virtual address is translated into a physical address. This translation could yield an address on the cache, physical memory, etc. It does not necessarily map to one piece of memory. If it has been swapped out, it causes a page-fault and that page is loaded into memory (multiple levels).

The low level operations like cache management are not affected by your decisions at this level.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • I agree with your comments but should note that since data alignment can be chosen in C++11 high-level choices can actually influence how the lower-most management is done. – edA-qa mort-ora-y Nov 11 '12 at 18:09