mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Use pooled buffer in replaceShortcodes
Even as a copy at the end is needed, this consumes way less memory on Go 1.4.2: ```benchmark old ns/op new ns/op delta BenchmarkParsePage 145979 139964 -4.12% BenchmarkReplaceShortcodeTokens 633574 631946 -0.26% BenchmarkShortcodeLexer 195842 187938 -4.04% benchmark old allocs new allocs delta BenchmarkParsePage 87 87 +0.00% BenchmarkReplaceShortcodeTokens 9424 9415 -0.10% BenchmarkShortcodeLexer 274 274 +0.00% benchmark old bytes new bytes delta BenchmarkParsePage 141830 141830 +0.00% BenchmarkReplaceShortcodeTokens 35219 25385 -27.92% BenchmarkShortcodeLexer 30178 30177 -0.00% ``` See #1148
This commit is contained in:
parent
67209dbfb3
commit
e764a6e638
1 changed files with 7 additions and 2 deletions
|
@ -458,7 +458,8 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
|
|||
return source, nil
|
||||
}
|
||||
|
||||
var buff bytes.Buffer
|
||||
buff := bp.GetBuffer()
|
||||
defer bp.PutBuffer(buff)
|
||||
|
||||
sourceLen := len(source)
|
||||
start := 0
|
||||
|
@ -507,7 +508,11 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
|
|||
if err != nil {
|
||||
return nil, errors.New("buff write failed")
|
||||
}
|
||||
return buff.Bytes(), nil
|
||||
|
||||
bc := make([]byte, buff.Len(), buff.Len())
|
||||
copy(bc, buff.Bytes())
|
||||
|
||||
return bc, nil
|
||||
}
|
||||
|
||||
func getShortcodeTemplate(name string, t tpl.Template) *template.Template {
|
||||
|
|
Loading…
Reference in a new issue