mirror of https://github.com/anoshenko/rui.git
Optimisation
This commit is contained in:
parent
00d6e2379d
commit
b2b9befa14
34
utils.go
34
utils.go
|
@ -6,35 +6,33 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var stringBuilders []*strings.Builder = make([]*strings.Builder, 4096)
|
const stringBuilderCap = 4096
|
||||||
var stringBuilderCount = 0
|
|
||||||
|
var stringBuilderPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
result := new(strings.Builder)
|
||||||
|
result.Grow(stringBuilderCap)
|
||||||
|
return result
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func allocStringBuilder() *strings.Builder {
|
func allocStringBuilder() *strings.Builder {
|
||||||
for stringBuilderCount > 0 {
|
if builder := stringBuilderPool.Get(); builder != nil {
|
||||||
stringBuilderCount--
|
return builder.(*strings.Builder)
|
||||||
result := stringBuilders[stringBuilderCount]
|
|
||||||
if result != nil {
|
|
||||||
stringBuilders[stringBuilderCount] = nil
|
|
||||||
result.Reset()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result := new(strings.Builder)
|
result := new(strings.Builder)
|
||||||
result.Grow(4096)
|
result.Grow(stringBuilderCap)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func freeStringBuilder(builder *strings.Builder) {
|
func freeStringBuilder(builder *strings.Builder) {
|
||||||
if builder != nil {
|
if builder != nil && builder.Cap() == stringBuilderCap {
|
||||||
if stringBuilderCount == len(stringBuilders) {
|
builder.Reset()
|
||||||
stringBuilders = append(stringBuilders, builder)
|
stringBuilderPool.Put(builder)
|
||||||
} else {
|
|
||||||
stringBuilders[stringBuilderCount] = builder
|
|
||||||
}
|
|
||||||
stringBuilderCount++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue