Skip to content

Commit 307a077

Browse files
committed
feat(ansi): add parser pool to get/put parsers in a thread-safe manner
1 parent a3aed41 commit 307a077

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

ansi/parser_sync.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ansi
2+
3+
import (
4+
"sync"
5+
6+
"github.com/charmbracelet/x/ansi/parser"
7+
)
8+
9+
var parserPool = sync.Pool{
10+
New: func() any {
11+
return NewParser(parser.MaxParamsSize, 1024*4) // 4MB data buffer
12+
},
13+
}
14+
15+
// GetParser returns a parser from a sync pool.
16+
func GetParser() *Parser {
17+
return parserPool.Get().(*Parser)
18+
}
19+
20+
// PutParser returns a parser to a sync pool. The parser is reset
21+
// automatically.
22+
func PutParser(p *Parser) {
23+
p.Reset()
24+
p.DataLen = 0
25+
parserPool.Put(p)
26+
}

0 commit comments

Comments
 (0)