Skip to content

Commit 21f7f01

Browse files
committed
runtime: avoid runtimeNano call on a common netpoll path
runtimeNano is slower than nanotime, so pass the duration to runtime_pollSetDeadline as is. netpoll can add nanotime itself. Arguably a bit simpler because, say, a negative duration clearly represents already expired timer, no need to compare to nanotime again. This may also fix an obscure corner case when a deadline in past which happens to be nanotime 0 is confused with no deadline at all, which are radically different things. Also don't compute any durations and times if Time is zero (currently we first compute everything and then reset d back to 0, which is wasteful). name old time/op new time/op delta TCP4OneShotTimeout-6 17.1µs ± 0% 17.0µs ± 0% ~ (p=0.421 n=5+5) SetReadDeadline-6 230ns ± 0% 205ns ± 1% -10.63% (p=0.008 n=5+5) Change-Id: I2aad699270289a5b9ead68f5e44ec4ec6d96baa0 Reviewed-on: https://go-review.googlesource.com/c/146344 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Dmitry Vyukov <[email protected]>
1 parent 31e7842 commit 21f7f01

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/internal/poll/fd_poll_runtime.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,12 @@ func (fd *FD) SetWriteDeadline(t time.Time) error {
136136
}
137137

138138
func setDeadlineImpl(fd *FD, t time.Time, mode int) error {
139-
diff := int64(time.Until(t))
140-
d := runtimeNano() + diff
141-
if d <= 0 && diff > 0 {
142-
// If the user has a deadline in the future, but the delay calculation
143-
// overflows, then set the deadline to the maximum possible value.
144-
d = 1<<63 - 1
145-
}
146-
if t.IsZero() {
147-
d = 0
139+
var d int64
140+
if !t.IsZero() {
141+
d = int64(time.Until(t))
142+
if d == 0 {
143+
d = -1 // don't confuse deadline right now with no deadline
144+
}
148145
}
149146
if err := fd.incref(); err != nil {
150147
return err

src/runtime/netpoll.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,13 @@ func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
201201
}
202202
rd0, wd0 := pd.rd, pd.wd
203203
combo0 := rd0 > 0 && rd0 == wd0
204-
if d != 0 && d <= nanotime() {
205-
d = -1
204+
if d > 0 {
205+
d += nanotime()
206+
if d <= 0 {
207+
// If the user has a deadline in the future, but the delay calculation
208+
// overflows, then set the deadline to the maximum possible value.
209+
d = 1<<63 - 1
210+
}
206211
}
207212
if mode == 'r' || mode == 'r'+'w' {
208213
pd.rd = d

0 commit comments

Comments
 (0)