mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
a9c5133a77
The custom sort functions used from the templates had some subtle data race- and related issues, especially when used in the single page template. This commit fixes this by making copies and protect the read and writes with a RWMutex. The results are cached (it will typically be invoked *number of pages* times with exactly the same data). This is, not surprisingly, also faster: ``` benchmark old ns/op new ns/op delta BenchmarkSortByWeightAndReverse 14228 491 -96.55% benchmark old allocs new allocs delta BenchmarkSortByWeightAndReverse 1 0 -100.00% benchmark old bytes new bytes delta BenchmarkSortByWeightAndReverse 32 0 -100.00% ``` Fixes #1293
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package hugolib
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/spf13/hugo/source"
|
|
)
|
|
|
|
func TestPageSortReverse(t *testing.T) {
|
|
p1 := createSortTestPages(10)
|
|
assert.Equal(t, 0, p1[0].FuzzyWordCount)
|
|
assert.Equal(t, 9, p1[9].FuzzyWordCount)
|
|
p2 := p1.Reverse()
|
|
assert.Equal(t, 9, p2[0].FuzzyWordCount)
|
|
assert.Equal(t, 0, p2[9].FuzzyWordCount)
|
|
// cached
|
|
assert.True(t, probablyEqualPages(p2, p1.Reverse()))
|
|
}
|
|
|
|
func BenchmarkSortByWeightAndReverse(b *testing.B) {
|
|
|
|
p := createSortTestPages(300)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
p = p.ByWeight().Reverse()
|
|
}
|
|
|
|
}
|
|
|
|
func createSortTestPages(num int) Pages {
|
|
pages := make(Pages, num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
pages[i] = &Page{
|
|
Node: Node{
|
|
URLPath: URLPath{
|
|
Section: "z",
|
|
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
|
|
},
|
|
Site: &SiteInfo{
|
|
BaseURL: "http://base/",
|
|
},
|
|
},
|
|
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
|
|
}
|
|
w := 5
|
|
if i%2 == 0 {
|
|
w = 10
|
|
}
|
|
pages[i].FuzzyWordCount = i
|
|
pages[i].Weight = w
|
|
pages[i].Description = "initial"
|
|
}
|
|
|
|
return pages
|
|
}
|