Skip to content

Commit

Permalink
Convert numLoops to the least power of two integer value
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Sep 23, 2019
1 parent dd00ef7 commit 7e1c5cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
36 changes: 17 additions & 19 deletions gnet_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"time"

"github.com/panjf2000/gnet/internal"
"github.com/panjf2000/gnet/netpoll"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -117,9 +118,9 @@ func activateLoops(svr *server, numLoops int) {
// Create loops locally and bind the listeners.
for i := 0; i < numLoops; i++ {
loop := &loop{
idx: i,
poller: netpoll.OpenPoller(),
packet: make([]byte, 0xFFFF),
idx: i,
poller: netpoll.OpenPoller(),
packet: make([]byte, 0xFFFF),
}
for _, ln := range svr.lns {
loop.poller.AddRead(ln.fd)
Expand All @@ -135,25 +136,22 @@ func activateLoops(svr *server, numLoops int) {
}

func activateReactors(svr *server, numLoops int) {
// Convert numLoops to the least power of two integer value greater than or equal to n,
// e.g. 2, 4, 8, 16, 32, 64, etc, which will make a higher performance when dispatching messages later.
//powerOfTwoNumLoops := internal.CeilToPowerOfTwo(numLoops)
//numLoops = powerOfTwoNumLoops
//if numCPU := runtime.NumCPU(); numCPU < powerOfTwoNumLoops {
// numLoops = internal.FloorToPowerOfTwo(numCPU)
//if numLoops < 3 {
// numLoops = 3
//}
//
//svr.wg.Add(1 + (numLoops-1)/2)

if numLoops < 3 {
numLoops = 3
}

svr.wg.Add(1 + (numLoops-1)/2)

for i := 0; i < (numLoops-1)/2; i++ {
// Convert numLoops to the least power of two integer value greater than or equal to n,
// e.g. 2, 4, 8, 16, 32, 64, etc, which will make a higher performance when dispatching messages later.
powerOfTwoNumLoops := internal.CeilToPowerOfTwo(numLoops)
svr.wg.Add(1 + powerOfTwoNumLoops)
//for i := 0; i < (numLoops-1)/2; i++ {
for i := 0; i < powerOfTwoNumLoops; i++ {
loop := &loop{
idx: i,
poller: netpoll.OpenPoller(),
packet: make([]byte, 0xFFFF),
idx: i,
poller: netpoll.OpenPoller(),
packet: make([]byte, 0xFFFF),
}
svr.loops = append(svr.loops, loop)
}
Expand Down
11 changes: 5 additions & 6 deletions reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const (
//}

type eventConsumer struct {
numLoops int
// numLoopsMask int
numLoops int
numLoopsMask int
loop *loop
connRingBuffer *[connRingBufferSize]*conn
onOpened func(*conn)
Expand All @@ -41,8 +41,8 @@ func (ec *eventConsumer) Consume(lower, upper int64) {
// Connections load balance under round-robin algorithm.
if ec.numLoops > 1 {
// Leverage "and" operator instead of "modulo" operator to speed up round-robin algorithm.
// idx := int(lower) & ec.numLoopsMask
idx := int(lower) % ec.numLoops
idx := int(lower) & ec.numLoopsMask
//idx := int(lower) % ec.numLoops
if idx != ec.loop.idx {
// Don't match the round-robin rule, ignore this connection.
continue
Expand Down Expand Up @@ -72,11 +72,10 @@ func activateMainReactor(svr *server) {

eventConsumers := make([]disruptor.Consumer, 0, svr.numLoops)
for _, loop := range svr.loops {
// ec := &eventConsumer{svr.numLoops, svr.numLoops - 1, loop, connRingBuffer}
onOpened := func(conn *conn) {
_ = loop.loopOpened(svr, conn)
}
ec := &eventConsumer{svr.numLoops, loop, connRingBuffer, onOpened}
ec := &eventConsumer{svr.numLoops, svr.numLoops - 1, loop, connRingBuffer, onOpened}
eventConsumers = append(eventConsumers, ec)
}

Expand Down

0 comments on commit 7e1c5cd

Please sign in to comment.