Reuse the BlackFriday instance when possible

This is in heavy use in rendering, so this makes a difference:

```bash
benchmark                                                                                    old ns/op     new ns/op     delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4     124551144     107743429     -13.49%

benchmark                                                                                    old allocs     new allocs     delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4     528684         435118         -17.70%

benchmark                                                                                    old bytes     new bytes     delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_pages=500,tags_per_page=5,shortcodes,render-4     53306848      45147832      -15.31%
```
This commit is contained in:
Bjørn Erik Pedersen
2017-12-16 18:56:58 +01:00
parent 2511498608
commit db4b7a5c67
6 changed files with 36 additions and 33 deletions

View File

@@ -168,7 +168,7 @@ type Page struct {
plainWords []string
// rendering configuration
renderingConfig *helpers.Blackfriday
renderingConfig *helpers.BlackFriday
// menus
pageMenus PageMenus
@@ -700,19 +700,21 @@ func (p *Page) renderContent(content []byte) []byte {
Config: p.getRenderingConfig()})
}
func (p *Page) getRenderingConfig() *helpers.Blackfriday {
func (p *Page) getRenderingConfig() *helpers.BlackFriday {
p.renderingConfigInit.Do(func() {
p.renderingConfig = p.s.ContentSpec.NewBlackfriday()
bfParam := p.GetParam("blackfriday")
if bfParam == nil {
p.renderingConfig = p.s.ContentSpec.BlackFriday
return
}
// Create a copy so we can modify it.
bf := *p.s.ContentSpec.BlackFriday
p.renderingConfig = &bf
if p.Language() == nil {
panic(fmt.Sprintf("nil language for %s with source lang %s", p.BaseFileName(), p.lang))
}
bfParam := p.GetParam("blackfriday")
if bfParam == nil {
return
}
pageParam := cast.ToStringMap(bfParam)
if err := mapstructure.Decode(pageParam, &p.renderingConfig); err != nil {
p.s.Log.FATAL.Printf("Failed to get rendering config for %s:\n%s", p.BaseFileName(), err.Error())