Skip to content

Commit

Permalink
Merge pull request #187 from Mido-sys/optimize_compiler_output
Browse files Browse the repository at this point in the history
Replace bytes.buffer with strings.builder to avoid extra memory allocation
  • Loading branch information
paganotoni authored Aug 6, 2024
2 parents 36946a4 + 79fe842 commit 7f2c245
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plush

import (
"bytes"
"fmt"

"github.com/gobuffalo/plush/v4/token"
Expand Down Expand Up @@ -36,7 +35,7 @@ type compiler struct {
}

func (c *compiler) compile() (string, error) {
bb := &bytes.Buffer{}
bb := &strings.Builder{}

for _, stmt := range c.program.Statements {
var res interface{}
Expand Down Expand Up @@ -70,7 +69,7 @@ func (c *compiler) compile() (string, error) {
return bb.String(), nil
}

func (c *compiler) write(bb *bytes.Buffer, i interface{}) {
func (c *compiler) write(bb *strings.Builder, i interface{}) {
switch t := i.(type) {
case time.Time:
if dtf, ok := c.ctx.Value("TIME_FORMAT").(string); ok {
Expand Down
4 changes: 2 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package plush

import (
"bytes"
"fmt"
"strings"
"sync"

"github.com/gobuffalo/helpers"
Expand Down Expand Up @@ -80,7 +80,7 @@ func (h HelperContext) BlockWith(hc hctx.Context) (string, error) {
return "", err
}

bb := &bytes.Buffer{}
bb := &strings.Builder{}
h.compiler.write(bb, i)

return bb.String(), nil
Expand Down

0 comments on commit 7f2c245

Please sign in to comment.