gpt4 book ai didi

c++ - new/malloc 或 delete/free 是否占用或使缓存行无效?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:43 27 4
gpt4 key购买 nike

我对缓存行为很好奇。下面是一些与缓存相关的问题:

  1. 写操作是否将数据带入缓存?考虑像 A[i] = B[i] 这样的赋值,A[i] 会被加载到缓存中吗?因为我只是将一些东西写入 A[i] 而不是读取它的值。

  2. 分配大内存时,内存可能来自操作系统。出于安全原因,操作系统会将数据初始化为零(Reference)。如果赋值会把数据带入缓存(问题1),这种机制会占用缓存吗?

  3. 假设有一个已分配的数组 B,并且整个 B 现在都在缓存中。释放数组 B 后,B 占用的缓存行是否会立即失效(可用)?

有人可以给我提示吗?

最佳答案

从这里https://people.freebsd.org/~lstewart/articles/cpumemory.pdf

--

  1. Does write operation bring the data into the cache?

来自文章:

By default all data read or written by the CPU cores is stored in the cache. There are memory regions which cannot be cached but this is something only the OS implementers have to be concerned about; it is not visible to the application programmer. There are also instructions which allow the programmer to deliberately bypass certain caches. This will be discussed in section 6.

--

  1. When allocating large memory, the memory may come from the OS. will this mechanism occupy the cache?

可能不是。只有在读取或写入数据后才会占用缓存。来自文章:

On operating systems like Linux with demand-paging support, an mmap call only modifies the page tables ... No actual memory is allocated at the time of the mmap call.

The allocation part happens when a memory page is first accessed, either by reading or writing data, or by executing code. In response to the ensuing page fault, the kernel takes control and determines, using the page table tree, the data which has to be present on the page. This resolution of the page fault is not cheap, but it happens for every single page which is used by a process.

--

3 .Assume that there is an allocated array B, and the entire B is now in the cache. Will the cache lines occupied by B become invalid(available) right after I free array B?

只有当另一个 CPU 上有写操作时缓存行才会失效

What developed over the years is the MESI cache coherency protocol (Modified, Exclusive, Shared, Invalid). The protocol is named after the four states a cache line can be in when using the MESI protocol. ... If the second processor wants to write to the cache line the first processor sends the cache line content and marks the cache line locally as Invalid.

缓存行也可以被驱逐:

Another detail of the caches which is rather uninteresting to programmers is the cache replacement strategy. Most caches evict the Least Recently Used (LRU) element first.

根据我使用 TCMalloc 的经验,free() 并不是从缓存中逐出内存的令人信服的理由。相反,它可能对性能有害。在 free() 上,TCMalloc 只是将一个释放的内存块放入其缓存中。而这 block 内存将在下一次应用程序请求一 block 内存时由malloc()返回。这就是像 TCMalloc 这样的缓存分配器的本质。如果这 block 内存仍在缓存中,那么它的性能会更好!

关于c++ - new/malloc 或 delete/free 是否占用或使缓存行无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com