简体   繁体   English

const int&performance issue

[英]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? ) 我曾经问过一个关于stackoverflow的问题(如果你对此感兴趣的话是链接: 通过引用传递“高级”概念?

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). 如果40恰好是CPU缓存中的值(rvalue)。 Then would you, by writing that line, just reserved cache memory to hold the number 40 for the lifetime of your process? 那么,通过编写该行,您是否只需保留缓存以在流程的生命周期内保存数字40? 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). 文字40几乎肯定存在于一些只读存储器中,可能存在于汇编程序中(对于较小的值,通常有指令可以设置寄存器或地址;对于较大的值,它将生活在某个地方作为常量)。 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). 当你创建一个const引用时,在编译器认为适合的任何地方构造一个临时值来保存临时值(可能在堆栈上)。 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. 如果从未采用此临时地址,则实际上甚至可能不会创建:C ++标准中的所有规则都由“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. 如果采用const引用的地址,编译器需要决定放置对象的位置,实际上,您可能会看到一个小的性能影响。

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. 缓存管理等低级操作不受此级别决策的影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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