forked from Terry-Mao/goim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bufio, bytes.Buffer, bytes.Pool improve GC performance
- Loading branch information
Showing
18 changed files
with
2,201 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
log "code.google.com/p/log4go" | ||
"sync" | ||
"github.com/Terry-Mao/goim/libs/bytes" | ||
"github.com/Terry-Mao/goim/libs/time" | ||
) | ||
|
||
type RoundOptions struct { | ||
Timer int | ||
TimerSize int | ||
Reader int | ||
ReadBuf int | ||
ReadBufSize int | ||
Writer int | ||
WriteBuf int | ||
WriteBufSize int | ||
} | ||
|
||
// Ronnd userd for connection round-robin get a reader/writer/timer for split big lock. | ||
type Round struct { | ||
readers []sync.Pool | ||
writers []sync.Pool | ||
timers []Timer | ||
readers []bytes.Pool | ||
writers []bytes.Pool | ||
timers []time.Timer | ||
options RoundOptions | ||
readerIdx int | ||
writerIdx int | ||
timerIdx int | ||
} | ||
|
||
func NewRound(readBuf, writeBuf, timer, timerSize int) *Round { | ||
r := new(Round) | ||
log.Debug("create %d reader buffer pool", readBuf) | ||
log.Debug("create %d writer buffer pool", writeBuf) | ||
r.readerIdx = readBuf | ||
r.writerIdx = writeBuf | ||
r.readers = make([]sync.Pool, readBuf) | ||
r.writers = make([]sync.Pool, writeBuf) | ||
log.Debug("create %d timer", timer) | ||
r.timerIdx = timer | ||
r.timers = make([]Timer, timer) | ||
for i := 0; i < timer; i++ { | ||
r.timers[i].Init(timerSize) | ||
// NewRound new a round struct. | ||
func NewRound(options RoundOptions) (r *Round) { | ||
var i int | ||
r = new(Round) | ||
r.options = options | ||
// reader | ||
r.readers = make([]bytes.Pool, options.Reader) | ||
for i = 0; i < options.Reader; i++ { | ||
r.readers[i].Init(options.ReadBuf, options.ReadBufSize) | ||
} | ||
// writer | ||
r.writers = make([]bytes.Pool, options.Writer) | ||
for i = 0; i < options.Writer; i++ { | ||
r.writers[i].Init(options.WriteBuf, options.WriteBufSize) | ||
} | ||
// timer | ||
r.timers = make([]time.Timer, options.Timer) | ||
for i = 0; i < options.Timer; i++ { | ||
r.timers[i].Init(options.TimerSize) | ||
} | ||
return r | ||
return | ||
} | ||
|
||
func (r *Round) Timer(rn int) *Timer { | ||
return &(r.timers[rn%r.timerIdx]) | ||
// Timer get a timer. | ||
func (r *Round) Timer(rn int) *time.Timer { | ||
return &(r.timers[rn%r.options.Timer]) | ||
} | ||
|
||
func (r *Round) Reader(rn int) *sync.Pool { | ||
return &(r.readers[rn%r.readerIdx]) | ||
// Reader get a reader memory buffer. | ||
func (r *Round) Reader(rn int) *bytes.Pool { | ||
return &(r.readers[rn%r.options.Reader]) | ||
} | ||
|
||
func (r *Round) Writer(rn int) *sync.Pool { | ||
return &(r.writers[rn%r.writerIdx]) | ||
// Writer get a writer memory buffer pool. | ||
func (r *Round) Writer(rn int) *bytes.Pool { | ||
return &(r.writers[rn%r.options.Writer]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.