Skip to content

Commit

Permalink
refactor: use unsafe to avoid alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
wazazaby committed Mar 21, 2024
1 parent 4712c80 commit 009bff0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion vimebu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"strconv"
"strings"
"unsafe"
)

const (
Expand Down Expand Up @@ -251,5 +252,13 @@ func (b *Builder) String() string {
if b.flLabel {
b.underlying.WriteByte(rightBracketByte)
}
return b.underlying.String()

return b.unsafeString()
}

// unsafeString returns the accumulated string using
// [strings.Builder]'s unsafe code to reduce allocations.
func (b *Builder) unsafeString() string {
buf := b.underlying
return unsafe.String(unsafe.SliceData(buf.Bytes()), buf.Len())
}

0 comments on commit 009bff0

Please sign in to comment.