优化内存池

This commit is contained in:
boyce
2021-01-07 18:34:32 +08:00
parent e712e24de5
commit e6c01c9071
3 changed files with 118 additions and 25 deletions

View File

@@ -2,7 +2,7 @@ package rpc
import ( import (
"reflect" "reflect"
"sync" "github.com/duanhf2012/origin/util/sync"
"time" "time"
) )
@@ -29,8 +29,13 @@ func (r *Responder) IsInvalid() bool {
} }
//var rpcResponsePool sync.Pool //var rpcResponsePool sync.Pool
var rpcRequestPool sync.Pool var rpcRequestPool = sync.NewPoolEx(make(chan sync.IPoolData,10240),func()sync.IPoolData{
var rpcCallPool sync.Pool return &RpcRequest{}
})
var rpcCallPool = sync.NewPoolEx(make(chan sync.IPoolData,10240),func()sync.IPoolData{
return &Call{done:make(chan *Call,1)}
})
type IRpcRequestData interface { type IRpcRequestData interface {
@@ -72,16 +77,6 @@ type Call struct {
callTime time.Time callTime time.Time
} }
func init(){
rpcRequestPool.New = func() interface{} {
return &RpcRequest{}
}
rpcCallPool.New = func() interface{} {
return &Call{done:make(chan *Call,1)}
}
}
func (slf *RpcRequest) Clear() *RpcRequest{ func (slf *RpcRequest) Clear() *RpcRequest{
slf.RpcRequestData = nil slf.RpcRequestData = nil
slf.localReply = nil slf.localReply = nil
@@ -92,6 +87,22 @@ func (slf *RpcRequest) Clear() *RpcRequest{
return slf return slf
} }
func (slf *RpcRequest) Reset() {
slf.Clear()
}
func (slf *RpcRequest) IsRef()bool{
return slf.ref
}
func (slf *RpcRequest) Ref(){
slf.ref = true
}
func (slf *RpcRequest) UnRef(){
slf.ref = false
}
func (rpcResponse *RpcResponse) Clear() *RpcResponse{ func (rpcResponse *RpcResponse) Clear() *RpcResponse{
rpcResponse.RpcResponseData = nil rpcResponse.RpcResponseData = nil
return rpcResponse return rpcResponse
@@ -113,6 +124,22 @@ func (call *Call) Clear() *Call{
return call return call
} }
func (call *Call) Reset() {
call.Clear()
}
func (call *Call) IsRef()bool{
return call.ref
}
func (call *Call) Ref(){
call.ref = true
}
func (call *Call) UnRef(){
call.ref = false
}
func (call *Call) Done() *Call{ func (call *Call) Done() *Call{
return <-call.done return <-call.done
} }

View File

@@ -1,5 +1,7 @@
package sync package sync
import sysSync "sync"
type Pool struct { type Pool struct {
New func()interface{} //构建对象函数 New func()interface{} //构建对象函数
C chan interface{} //最大缓存的数量 C chan interface{} //最大缓存的数量
@@ -12,11 +14,12 @@ type IPoolData interface {
UnRef() UnRef()
} }
type PoolEx struct{ type poolEx struct{
New func()IPoolData //构建对象函数
C chan IPoolData //最大缓存的数量 C chan IPoolData //最大缓存的数量
syncPool sysSync.Pool
} }
func (pool *Pool) Get() interface{}{ func (pool *Pool) Get() interface{}{
select { select {
case d := <-pool.C: case d := <-pool.C:
@@ -35,22 +38,33 @@ func (pool *Pool) Put(data interface{}){
} }
} }
func (pool *PoolEx) Get() IPoolData{ func NewPoolEx(C chan IPoolData,New func()IPoolData) *poolEx{
var pool poolEx
pool.C = C
//pool.New = New
pool.syncPool.New = func() interface{} {
return New()
}
return &pool
}
func (pool *poolEx) Get() IPoolData{
select { select {
case d := <-pool.C: case d := <-pool.C:
d.Ref() d.Ref()
d.Reset() d.Reset()
return d return d
default: default:
data := pool.New() data := pool.syncPool.Get().(IPoolData)
data.Reset() data.Reset()
data.Ref() data.Ref()
return data
} }
return nil return nil
} }
func (pool *PoolEx) Put(data IPoolData){ func (pool *poolEx) Put(data IPoolData){
if data.IsRef() == false { if data.IsRef() == false {
panic("Repeatedly freeing memory") panic("Repeatedly freeing memory")
} }
@@ -59,6 +73,8 @@ func (pool *PoolEx) Put(data IPoolData){
select { select {
case pool.C <- data: case pool.C <- data:
default: default:
pool.syncPool.Put(data)
} }
} }

View File

@@ -2,9 +2,10 @@ package timer
import ( import (
"fmt" "fmt"
"github.com/duanhf2012/origin/util/sync"
"reflect" "reflect"
"runtime" "runtime"
"sync"
"time" "time"
) )
@@ -18,6 +19,8 @@ type Timer struct {
cb func() cb func()
AdditionData interface{} //定时器附加数据 AdditionData interface{} //定时器附加数据
rOpen bool //是否重新打开 rOpen bool //是否重新打开
ref bool
} }
// Ticker // Ticker
@@ -30,17 +33,17 @@ type Cron struct {
Timer Timer
} }
var timerPool = sync.Pool{New: func() interface{}{ var timerPool = sync.NewPoolEx(make(chan sync.IPoolData,1000),func() sync.IPoolData{
return &Timer{} return &Timer{}
}} })
var cronPool = sync.Pool{New: func() interface{}{ var cronPool = sync.NewPoolEx(make(chan sync.IPoolData,1000),func() sync.IPoolData{
return &Cron{} return &Cron{}
}} })
var tickerPool = sync.Pool{New: func() interface{}{ var tickerPool =sync.NewPoolEx(make(chan sync.IPoolData,1000),func() sync.IPoolData{
return &Ticker{} return &Ticker{}
}} })
func newTimer(d time.Duration,c chan *Timer,cb func(),name string,additionData interface{}) *Timer{ func newTimer(d time.Duration,c chan *Timer,cb func(),name string,additionData interface{}) *Timer{
if c == nil { if c == nil {
@@ -116,6 +119,53 @@ func (t *Timer) GetName() string{
return t.name return t.name
} }
func (t *Timer) Reset(){
}
func (t *Timer) IsRef()bool{
return t.ref
}
func (t *Timer) Ref(){
t.ref = true
}
func (t *Timer) UnRef(){
t.ref = false
}
func (c *Cron) Reset(){
}
func (c *Cron) IsRef()bool{
return c.ref
}
func (c *Cron) Ref(){
c.ref = true
}
func (c *Cron) UnRef(){
c.ref = false
}
func (c *Ticker) Reset(){
}
func (c *Ticker) IsRef()bool{
return c.ref
}
func (c *Ticker) Ref(){
c.ref = true
}
func (c *Ticker) UnRef(){
c.ref = false
}
func NewDispatcher(l int) *Dispatcher { func NewDispatcher(l int) *Dispatcher {
disp := new(Dispatcher) disp := new(Dispatcher)
disp.ChanTimer = make(chan *Timer, l) disp.ChanTimer = make(chan *Timer, l)