Avoid reading from Viper for path and URL funcs

The gain, given the "real sites benchmark" below, is obvious:

```
benchmark           old ns/op       new ns/op       delta
BenchmarkHugo-4     14497594101     13084156335     -9.75%

benchmark           old allocs     new allocs     delta
BenchmarkHugo-4     57404335       48282002       -15.89%

benchmark           old bytes       new bytes      delta
BenchmarkHugo-4     9933505624      9721984424     -2.13%
```

Fixes #2495
This commit is contained in:
Bjørn Erik Pedersen 2016-10-24 13:45:30 +02:00 committed by GitHub
parent dffd7da07c
commit a10b2cd372
26 changed files with 348 additions and 90 deletions

View file

@ -75,16 +75,16 @@ var fpb filepathBridge
// It does so by creating a Unicode-sanitized string, with the spaces replaced,
// whilst preserving the original casing of the string.
// E.g. Social Media -> Social-Media
func MakePath(s string) string {
return UnicodeSanitize(strings.Replace(strings.TrimSpace(s), " ", "-", -1))
func (p *PathSpec) MakePath(s string) string {
return p.UnicodeSanitize(strings.Replace(strings.TrimSpace(s), " ", "-", -1))
}
// MakePathSanitized creates a Unicode-sanitized string, with the spaces replaced
func MakePathSanitized(s string) string {
if viper.GetBool("DisablePathToLower") {
return MakePath(s)
func (p *PathSpec) MakePathSanitized(s string) string {
if p.disablePathToLower {
return p.MakePath(s)
}
return strings.ToLower(MakePath(s))
return strings.ToLower(p.MakePath(s))
}
// MakeTitle converts the path given to a suitable title, trimming whitespace
@ -110,7 +110,7 @@ func ishex(c rune) bool {
// a predefined set of special Unicode characters.
// If RemovePathAccents configuration flag is enabled, Uniccode accents
// are also removed.
func UnicodeSanitize(s string) string {
func (p *PathSpec) UnicodeSanitize(s string) string {
source := []rune(s)
target := make([]rune, 0, len(source))
@ -124,7 +124,7 @@ func UnicodeSanitize(s string) string {
var result string
if viper.GetBool("RemovePathAccents") {
if p.removePathAccents {
// remove accents - see https://blog.golang.org/normalization
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
result, _, _ = transform.String(t, string(target))