Skip to content

Commit

Permalink
增加内存池机制,尝试优化性能
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomM committed Jun 15, 2018
1 parent ca134b4 commit 70fae3c
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,58 @@ func textSliceToString(text []Text) string {
return Join(text)
}

var globalMemPool *MemPool

type memPoolOption struct {
bufLen int
}

type MemPool struct {
buffer []byte
bufCurent int
option *memPoolOption
}

func (p *MemPool) GetBytes(n int) []byte {
if n > p.option.bufLen {
bigbyte := make([]byte, n)
return bigbyte
}
if p.bufCurent+n >= p.option.bufLen {
p.buffer = make([]byte, p.option.bufLen)
p.bufCurent = 0
}
startOps := p.bufCurent
p.bufCurent += n
return p.buffer[startOps:p.bufCurent]
}

func WithMemPool(bufLen int) {
option := &memPoolOption{bufLen: bufLen}
globalMemPool = &MemPool{
buffer: make([]byte, option.bufLen),
option: option,
}
}

func Join(a []Text) string {
switch len(a) {
case 0:
return ""
case 1:
return string(a[0])
case 2:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
return string(a[0]) + string(a[1])
case 3:
// Special case for common small values.
// Remove if golang.org/issue/6714 is fixed
return string(a[0]) + string(a[1]) + string(a[2])
}
n := 0
for i := 0; i < len(a); i++ {
n += len(a[i])
}

b := make([]byte, n)
var b []byte
if globalMemPool != nil {
b = globalMemPool.GetBytes(n)
} else {
b = make([]byte, n)
}
bp := copy(b, a[0])
for _, s := range a[1:] {
bp += copy(b[bp:], s)
Expand Down

0 comments on commit 70fae3c

Please sign in to comment.