Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 3754a85

Browse files
committed
avoid possibly allocating a too small, garbage pointslice
1 parent c2cc3fb commit 3754a85

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

pointslicepool/pointslicepool.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ type PointSlicePool struct {
1818
func New(defaultSize int) *PointSlicePool {
1919
return &PointSlicePool{
2020
defaultSize: defaultSize,
21-
p: sync.Pool{
22-
New: func() interface{} {
23-
return make([]schema.Point, 0, defaultSize)
24-
},
25-
},
21+
p: sync.Pool{},
2622
}
2723
}
2824

@@ -31,16 +27,18 @@ func (p *PointSlicePool) Put(s []schema.Point) {
3127
}
3228

3329
func (p *PointSlicePool) Get() []schema.Point {
34-
return p.p.Get().([]schema.Point)
30+
return p.GetMin(p.defaultSize)
3531
}
3632

3733
// GetMin returns a pointslice that has at least minCap capacity
3834
func (p *PointSlicePool) GetMin(minCap int) []schema.Point {
39-
candidate := p.Get()
40-
if cap(candidate) >= minCap {
41-
return candidate
35+
candidate, ok := p.p.Get().([]schema.Point)
36+
if ok {
37+
if cap(candidate) >= minCap {
38+
return candidate
39+
}
40+
p.p.Put(candidate)
4241
}
43-
p.p.Put(candidate)
4442
if minCap > p.defaultSize {
4543
return make([]schema.Point, 0, minCap)
4644
}

0 commit comments

Comments
 (0)