workerpool: immediately switch to connection processing if GOMAXPROCS=1. This improves single-threaded server performance by 1-2%

This commit is contained in:
Aliaksandr Valialkin
2015-11-26 12:38:18 +02:00
parent 8b00288e0a
commit bc85e2b572
+16 -1
View File
@@ -2,6 +2,7 @@ package fasthttp
import (
"net"
"runtime"
"runtime/debug"
"strings"
"sync"
@@ -98,6 +99,20 @@ func (wp *workerPool) Serve(c net.Conn) bool {
return true
}
var workerChanCap = func() int {
// Use blocking workerChan if GOMAXPROCS=1.
// This immediately switches Serve to WorkerFunc, which results
// in higher performance (under go1.5 at least).
if runtime.GOMAXPROCS(0) == 1 {
return 0
}
// Use non-blocking workerChan if GOMAXPROCS>1,
// since otherwise the Serve caller (Acceptor) may lag accepting
// new connections if WorkerFunc is CPU-bound.
return 1
}()
func (wp *workerPool) getCh() *workerChan {
var ch *workerChan
createWorker := false
@@ -123,7 +138,7 @@ func (wp *workerPool) getCh() *workerChan {
vch := workerChanPool.Get()
if vch == nil {
vch = &workerChan{
ch: make(chan net.Conn, 1),
ch: make(chan net.Conn, workerChanCap),
}
}
ch = vch.(*workerChan)