Skip to content

Commit

Permalink
buf pool for MarshalHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxinx committed Sep 14, 2023
1 parent 3879865 commit 8f523db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/theplant/htmlgo

go 1.17
go 1.18

require github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61

Expand Down
12 changes: 11 additions & 1 deletion tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"html"
"strings"
"sync"
)

type tagAttr struct {
Expand Down Expand Up @@ -267,6 +268,12 @@ func (b *HTMLTagBuilder) PrependChildren(c ...HTMLComponent) (r *HTMLTagBuilder)
return b
}

var bufPool = sync.Pool{
New: func() any {
return &bytes.Buffer{}
},
}

func (b *HTMLTagBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
class := strings.TrimSpace(strings.Join(b.classNames, " "))
if len(class) > 0 {
Expand Down Expand Up @@ -331,7 +338,10 @@ func (b *HTMLTagBuilder) MarshalHTML(ctx context.Context) (r []byte, err error)
attrStr = " " + strings.Join(attrSegs, " ")
}

buf := bytes.NewBuffer(nil)
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
buf.Reset()

newline := ""

if b.omitEndTag {
Expand Down

0 comments on commit 8f523db

Please sign in to comment.