简体   繁体   中英

const int& performance issue

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM