Skip to content

Commit ab32699

Browse files
use Unix Nano timestamp for "now" in poolCommon
The pool’s ticktock goroutine was storing time.Now() in an atomic.Value, which incurs interface boxing and shows up as runtime.convT in profiles. Switch to storing Unix nanos in an int64 with atomic.Load/StoreInt64, and reconstruct time.Time in nowTime(). This preserves behavior while removing the per‑tick allocation and conversion overhead, especially when many pools are created.
1 parent 76ddb18 commit ab32699

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

ants.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ type poolCommon struct {
191191
ticktockCtx context.Context
192192
stopTicktock context.CancelFunc
193193

194-
now atomic.Value
194+
nowNano int64
195195

196196
options *Options
197197
}
@@ -303,7 +303,7 @@ func (p *poolCommon) ticktock() {
303303
break
304304
}
305305

306-
p.now.Store(time.Now())
306+
atomic.StoreInt64(&p.nowNano, time.Now().UnixNano())
307307
}
308308
}
309309

@@ -318,13 +318,13 @@ func (p *poolCommon) goPurge() {
318318
}
319319

320320
func (p *poolCommon) goTicktock() {
321-
p.now.Store(time.Now())
321+
atomic.StoreInt64(&p.nowNano, time.Now().UnixNano())
322322
p.ticktockCtx, p.stopTicktock = context.WithCancel(context.Background())
323323
go p.ticktock()
324324
}
325325

326326
func (p *poolCommon) nowTime() time.Time {
327-
return p.now.Load().(time.Time)
327+
return time.Unix(0, atomic.LoadInt64(&p.nowNano))
328328
}
329329

330330
// Running returns the number of workers currently running.

0 commit comments

Comments
 (0)