Use MapItem object in Mapslice directly

This commit is contained in:
Masaaki Goshima 2021-12-27 12:18:30 +09:00
parent 657973a17e
commit c220d90e4c
No known key found for this signature in database
GPG Key ID: 6A53785055537153
1 changed files with 5 additions and 11 deletions

View File

@ -207,7 +207,7 @@ type MapItem struct {
}
type Mapslice struct {
Items []*MapItem
Items []MapItem
}
func (m *Mapslice) Len() int {
@ -253,22 +253,16 @@ type MapContext struct {
var mapContextPool = sync.Pool{
New: func() interface{} {
return &MapContext{}
return &MapContext{
Slice: &Mapslice{},
}
},
}
func NewMapContext(mapLen int) *MapContext {
ctx := mapContextPool.Get().(*MapContext)
if ctx.Slice == nil {
ctx.Slice = &Mapslice{
Items: make([]*MapItem, 0, mapLen),
}
}
if len(ctx.Slice.Items) < mapLen {
ctx.Slice.Items = make([]*MapItem, mapLen)
for i := 0; i < mapLen; i++ {
ctx.Slice.Items[i] = &MapItem{}
}
ctx.Slice.Items = make([]MapItem, mapLen)
} else {
ctx.Slice.Items = ctx.Slice.Items[:mapLen]
}