如何将某些内存范围从C ++标记为不可缓存? [英] How to mark some memory ranges as non-cacheable from C++?

查看:107
本文介绍了如何将某些内存范围从C ++标记为不可缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处阅读有关CPU缓存的维基百科: http://en.wikipedia.org/wiki/CPU_cache# Replacement_Policies

I was reading the wikipedia on the CPU cache here: http://en.wikipedia.org/wiki/CPU_cache#Replacement_Policies


将某些内存范围标记为不可缓存可以提高性能,因为可以避免缓存很少重新存储的内存区域访问。这样可以避免在不进行任何重用的情况下将某些内容加载到缓存中的开销。

Marking some memory ranges as non-cacheable can improve performance, by avoiding caching of memory regions that are rarely re-accessed. This avoids the overhead of loading something into the cache, without having any reuse.

现在,我一直在阅读和学习如何编写具有更好的缓存性能的程序(一般考虑,通常不特定于C ++),但是我不知道高级代码可以显式地与CPU缓存行为进行交互。所以我的问题是,有没有办法用C ++来完成我在那篇文章中引用的内容?

Now, I've been reading and learning about how to write programs with better cache performance (general considerations, usually not specific to C++), but I did not know that high-level code can interact with CPU caching behavior explicitly. So my question, is there a way to do what I quoted from that article, in C++?

此外,我将非常感谢您提供有关如何特别在C ++中提高缓存性能的资源。 ,即使它们不使用直接处理CPU缓存的任何功能。例如,我想知道是否使用过多级别的间接寻址(例如,指向指针容器的指针容器)会损害缓存性能。

Also, I would appreciate resources on how to improve cache performance specifically in C++, even if they do not use any functions that deal directly with the CPU caches. For example, I'm wondering if using excessive levels of indirection (eg., a container of pointers to containers of pointers) can damage cache performance.

推荐答案

在Windows上,您可以使用 VirtualProtect( ptr,length,PAGE_NOCACHE和& oldFlags) 设置内存的缓存行为以避免缓存。

On Windows, you can use VirtualProtect(ptr, length, PAGE_NOCACHE, &oldFlags) to set the caching behavior for memory to avoid caching.

关于太多间接寻址:是的,如果您经常访问不同内存(通常会发生这种情况),它们可能会损害缓存性能。不过,请务必注意,如果您一致取消引用相同集,例如8个内存块,只有第9个块有所不同,因此通常不会有什么区别,因为这8个块将在第一次访问后被缓存。

Regarding too many indirections: Yes, they can damage cache performance, if you access different pieces of memory very often (which is what happens usually). It's important to note, though, that if you consistently dereference the same set of e.g. 8 blocks of memory, and only the 9th block differs, then it generally won't make a difference, because the 8 blocks would be cached after the first access.

这篇关于如何将某些内存范围从C ++标记为不可缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆