php-parser/pkg/position/pool.go
Laytan Laats 86c10ca380 perf: reduce memory usage by reducing amt of position's created
The positions created were taking GB's of memory and were not being
GC'ed.
2022-09-09 00:40:11 +02:00

30 lines
392 B
Go

package position
const DefaultBlockSize = 50
type Pool struct {
block []Position
off int
}
func NewPool(blockSize int) *Pool {
return &Pool{
block: make([]Position, blockSize),
}
}
func (p *Pool) Get() *Position {
if len(p.block) == 0 {
return nil
}
if len(p.block) == p.off {
p.block = make([]Position, len(p.block))
p.off = 0
}
p.off++
return &p.block[p.off-1]
}