TGTGInsighttelegram intelligenceLIVE / telegram public index
Post content
Post content
学会了 spinlock 的正确写法,加一个 while (*xp == 1) 的内部 spin 原地提速 30%👍 --- a/bad.c +++ b/good.c @@ -34,8 +34,10 @@ static inline int read_once(const xchglock_t *p) static inline void xchg_lock(xchglock_t *xp) { - while (xchg(xp, 1) == 1) - ; + while (xchg(xp, 1) == 1) { + while (read_once(xp) == 1) + ; + } } 原因是 cacheline bouncing,perf c2c 检查可知好版本的 HITM 只有坏版本的 1/5。太高级了。 ref: perfbook 7.3.1