prefetch

From: Linus Torvalds <torvalds@linux-foundation.org>
Newsgroups: fa.linux.kernel
Subject: Re: [patch] epoll use a single inode ...
Date: Wed, 07 Mar 2007 22:58:57 UTC
Message-ID: <fa.xioENCdH8/npFPrkKx9GGXIztBs@ifi.uio.no>

On Wed, 7 Mar 2007, Anton Blanchard wrote:
>
> Funny you mention this. We found some noticeable ppc64 regressions when
> moving the dcache to standard list macros and had to do this to fix it
> up:
>
> static inline void prefetch(const void *x)
> {
>         if (unlikely(!x))
>                 return;
>
>         __asm__ __volatile__ ("dcbt 0,%0" : : "r" (x));
> }
>
> Urgh :)

Yeah, I'm not at all surprised. Any implementation of "prefetch" that
doesn't just turn into a no-op if the TLB entry doesn't exist (which makes
them weaker for *actual* prefetching) will generally have a hard time with
a NULL pointer. Exactly because it will try to do a totally unnecessary
TLB fill - and since most CPU's will not cache negative TLB entries, that
unnecessary TLB fill will be done over and over and over again..

In general, using software prefetching is just a stupid idea, unless

 - the prefetch really is very strict (ie for a linked list you do exactly
   the above kinds of things to make sure that you don't try to prefetch
   the non-existent end entry)
AND
 - the CPU is stupid (in-order in particular).

I think Intel even suggests in their optimization manuals to *not* do
software prefetching, because hw can usually simply do better without it.

		Linus

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在语言中,prefetch是一种优化技术,用于预先加载数据到CPU缓存中,以减少访存延迟和提高程序性能。它可以在循环迭代或函数调用之前,先将未来循环迭代或函数调用中可能需要的数据加载到CPU缓存中。 在C语言中,使用预处理器指`#pragma`来指定prefetch的相关操作。具体的实现方式可能因为编译器和硬件平台的不同而有所差异。 下面是一个简单的示例代码,展示了如何使用prefetch来提高数据访问效率: ```c #include <stdio.h> #define PREFETCH_DISTANCE 4 void prefetch_example(int* array, int size) { int i; // Prefetch the data for future iterations for (i = 0; i < size; i += PREFETCH_DISTANCE) { __builtin_prefetch(&array[i + PREFETCH_DISTANCE]); } // Process the data for (i = 0; i < size; i++) { // Access array[i] here printf("%d ", array[i]); } } int main() { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int size = sizeof(array) / sizeof(array[0]); prefetch_example(array, size); return 0; } ``` 在上面的代码中,我们使用`__builtin_prefetch`函数来进行数据预取。在循环中,我们通过指定一个距离`PREFETCH_DISTANCE`来控制预取的间隔。这样,循环中的每个迭代都会预先加载未来迭代可能需要的数据到CPU缓存中,以提高访问效率。 需要注意的是,prefetch的效果可能因为硬件平台和具体的应用场景而有所不同。因此,在使用prefetch时,需要进行实际测试和性能评估,以确保它能够带来实际的性能提升。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值