Skip to content

Commit 31e7842

Browse files
committed
runtime: execute memory barrier conditionally when changing netpoll timers
We only need the memory barrier in poll_runtime_pollSetDeadline only when one of the timers has fired, which is not the expected case. Memory barrier can be somewhat expensive on some archs, so execute it only if one of the timers has in fact fired. name old time/op new time/op delta TCP4OneShotTimeout-6 17.0µs ± 0% 17.1µs ± 0% +0.35% (p=0.032 n=5+5) SetReadDeadline-6 232ns ± 0% 230ns ± 0% -1.03% (p=0.000 n=4+5) Update #25729 Change-Id: Ifce6f505b9e7ba3717bad8f454077a2e94ea6e75 Reviewed-on: https://go-review.googlesource.com/c/146343 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ff51353 commit 31e7842

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/runtime/netpoll.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,14 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
254254
}
255255
// If we set the new deadline in the past, unblock currently pending IO if any.
256256
var rg, wg *g
257-
atomicstorep(unsafe.Pointer(&wg), nil) // full memory barrier between stores to rd/wd and load of rg/wg in netpollunblock
258-
if pd.rd < 0 {
259-
rg = netpollunblock(pd, 'r', false)
260-
}
261-
if pd.wd < 0 {
262-
wg = netpollunblock(pd, 'w', false)
257+
if pd.rd < 0 || pd.wd < 0 {
258+
atomicstorep(unsafe.Pointer(&wg), nil) // full memory barrier between stores to rd/wd and load of rg/wg in netpollunblock
259+
if pd.rd < 0 {
260+
rg = netpollunblock(pd, 'r', false)
261+
}
262+
if pd.wd < 0 {
263+
wg = netpollunblock(pd, 'w', false)
264+
}
263265
}
264266
unlock(&pd.lock)
265267
if rg != nil {

0 commit comments

Comments
 (0)