From bc85e2b572889922b1d5387b4bc4f8a3896412f8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 26 Nov 2015 12:38:18 +0200 Subject: [PATCH] workerpool: immediately switch to connection processing if GOMAXPROCS=1. This improves single-threaded server performance by 1-2% --- workerpool.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/workerpool.go b/workerpool.go index 2a57c53..cbab951 100644 --- a/workerpool.go +++ b/workerpool.go @@ -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)