Will tons of go timer dramatically decrease performance.

1,194 views
Skip to first unread message

benz...@gmail.com

unread,
Jan 2, 2017, 12:23:07 AM1/2/17
to golang-nuts
Will tons of go timer dramatically decrease performance? and why?

Thanks.

Dave Cheney

unread,
Jan 2, 2017, 1:59:42 AM1/2/17
to golang-nuts
Yes and no. Timers are stored in sorted data structure in the runtime. If you start many timers in quick succession and do not stop them then the size of this structure can become a concern, however as the runtime only sleeps until the most recent wake-up event, there is little affect on performance.

Sokolov Yura

unread,
Jan 3, 2017, 2:46:35 AM1/3/17
to golang-nuts
Also it is single structure with single global mutex-lock.

Михаил Стойков

unread,
Jan 3, 2017, 8:30:27 AM1/3/17
to golang-nuts

I have to add that I had a problem with multiple sleeps (which creates a runtime.timer struct), because of the amount of newly generated structures to be collected on the next GC.

Which is why https://github.com/ironsmile/nedomi/blob/master/utils/throttle/timers.go was wrote.  Before this pool, pprof was being topped by GC in cpu usage and runtime.timer in allocated objects count.

It's important to note that my use case was throttling 3-5+ thousand connections which a sleep every 10ms+. And there are always thousand of timers running.

So my advice is if the tons of go timers can be reused ... to reuse them :D. If you do  https://github.com/golang/go/issues/11513  is a mandatory read.

# go test -bench BenchmarkParallelSlee*  -benchmem -benchtime 2m
BenchmarkParallelSleepWithSTD-4                   3000      59055109 ns/op     1280003 B/op       20000 allocs/op
BenchmarkParallelSleepWithPooledTimer-4           3000      61768205 ns/op        5113 B/op          40 allocs/op
Reply all
Reply to author
Forward
0 new messages