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
return source, nil
}
- var buff bytes.Buffer
+ buff := bp.GetBuffer()
+ defer bp.PutBuffer(buff)
sourceLen := len(source)
start := 0
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 {