星期六, 5月 25, 2019

prefetch

在 Linux Kernel,為了減少 cache miss 來增進效能,可將預期會用到的資料用 prefetch() 事先讀取,但有些只是想像效能會較好,沒有實證反而可能有反效果。

例如 <linux/list.h> 的 list_for_each() macro:
    #define list_for_each(pos, head) \
 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
            pos = pos->next)
經過實證反而效能較差 (短 list、null prefetch),硬體自己做的不會較差。

其它:
  • reordering structures that commonly accessed together fields are found in the same cache line
  • linked-list => cache-unfriendly
  • singly-linked hlist hash table list 
  • likely()
參考:
The problem with prefetch