Skip to content

Commit

Permalink
Reducing allocations on AlertData using buffer pooling
Browse files Browse the repository at this point in the history
benchmark                            old ns/op       new ns/op       delta
Benchmark_T10_P500_AlertTask-4       210263431       159024737       -24.37%
Benchmark_T10_P50000_AlertTask-4     18718435131     15589249575     -16.72%
Benchmark_T1000_P500_AlertTask-4     21833771619     20820186700     -4.64%

benchmark                            old allocs     new allocs     delta
Benchmark_T10_P500_AlertTask-4       490648         467352         -4.75%
Benchmark_T10_P50000_AlertTask-4     50044467       47560324       -4.96%
Benchmark_T1000_P500_AlertTask-4     48492207       46626729       -3.85%

benchmark                            old bytes      new bytes      delta
Benchmark_T10_P500_AlertTask-4       36155296       33162568       -8.28%
Benchmark_T10_P50000_AlertTask-4     3686308856     3347320896     -9.20%
Benchmark_T1000_P500_AlertTask-4     3604462496     3463221064     -3.92%
  • Loading branch information
yosiat committed Apr 4, 2016
1 parent 4245f21 commit 30619b1
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sync"
text "text/template"
"time"

Expand Down Expand Up @@ -100,6 +101,8 @@ type AlertNode struct {
idTmpl *text.Template
messageTmpl *text.Template
detailsTmpl *html.Template

bufPool sync.Pool
}

// Create a new AlertNode which caches the most recent item and exposes it over the HTTP API.
Expand All @@ -110,6 +113,13 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, l *log.Logger) (an *
}
an.node.runF = an.runAlert

// Create buffer pool for the templates
an.bufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}

// Parse templates
an.idTmpl, err = text.New("id").Parse(n.Id)
if err != nil {
Expand Down Expand Up @@ -520,8 +530,10 @@ func (a *AlertNode) renderID(name string, group models.GroupID, tags models.Tags
Group: g,
Tags: tags,
}
var id bytes.Buffer
err := a.idTmpl.Execute(&id, info)
id := a.bufPool.Get().(*bytes.Buffer)
defer a.bufPool.Put(id)
id.Reset()
err := a.idTmpl.Execute(id, info)
if err != nil {
return "", err
}
Expand All @@ -545,21 +557,32 @@ func (a *AlertNode) renderMessageAndDetails(id, name string, t time.Time, group
Level: level.String(),
Time: t,
}
var msg bytes.Buffer
err := a.messageTmpl.Execute(&msg, minfo)

// Grab a buffer for the message template and the details template
tmpBuffer := a.bufPool.Get().(*bytes.Buffer)
defer a.bufPool.Put(tmpBuffer)
tmpBuffer.Reset()

err := a.messageTmpl.Execute(tmpBuffer, minfo)
if err != nil {
return "", "", detailsInfo{}, err
}

msg := tmpBuffer.String()
dinfo := detailsInfo{
messageInfo: minfo,
Message: msg.String(),
Message: msg,
}
var details bytes.Buffer
err = a.detailsTmpl.Execute(&details, dinfo)

// Reuse the buffer, for the details template
tmpBuffer.Reset()
err = a.detailsTmpl.Execute(tmpBuffer, dinfo)
if err != nil {
return "", "", dinfo, err
}
return msg.String(), details.String(), dinfo, nil

details := tmpBuffer.String()
return msg, details, dinfo, nil
}

//--------------------------------
Expand Down

0 comments on commit 30619b1

Please sign in to comment.