簡體   English   中英

最奇怪的優化? 在`libuv`中。 請解釋

[英]Strange optimization? in `libuv`. Please explain

libuvcore.c:uv_run()包含下一個代碼

/* The if statement lets the compiler compile it to a conditional store.
 * Avoids dirtying a cache line.
 */
if (loop->stop_flag != 0)
    loop->stop_flag = 0;

這是什么意思? 是某種優化嗎? 為什么他們不簡單地分配0?

是的,就像評論中所說的那樣。 如果該標志已經為0,則無需將任何數據寫入內存,從而避免了可能將當前數據移出高速緩存並將該標志替換為0。 僅在時間緊迫的應用中,這將提供附加值。

我認為這種優化是不好的。 例如,在帶有-O3的gcc上,它給出以下代碼:

foo():
        movl    stop_flag(%rip), %eax
        testl   %eax, %eax
        je      .L3
        movl    $0, stop_flag(%rip)
.L3:
        ret
stop_flag:
        .zero   4

如您所見,沒有條件移動,而是分支。 而且我敢肯定,分支錯誤預測遠比弄臟緩存行還糟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM