Refactor templateSpec less method (#5840)

This commit is contained in:
Charlie Vieth 2019-05-15 19:35:07 -04:00 committed by Daniel Nelson
parent 1b3ca655c6
commit 43c6d13c33
2 changed files with 20 additions and 11 deletions

View File

@ -124,21 +124,16 @@ type templateSpecs []templateSpec
// Less reports whether the element with // Less reports whether the element with
// index j should sort before the element with index k. // index j should sort before the element with index k.
func (e templateSpecs) Less(j, k int) bool { func (e templateSpecs) Less(j, k int) bool {
if len(e[j].filter) == 0 && len(e[k].filter) == 0 { jlen := len(e[j].filter)
jlength := len(strings.Split(e[j].template, e[j].separator)) klen := len(e[k].filter)
klength := len(strings.Split(e[k].template, e[k].separator)) if jlen == 0 && klen != 0 {
return jlength < klength
}
if len(e[j].filter) == 0 {
return true return true
} }
if len(e[k].filter) == 0 { if klen == 0 && jlen != 0 {
return false return false
} }
return strings.Count(e[j].template, e[j].separator) <
jlength := len(strings.Split(e[j].template, e[j].separator)) strings.Count(e[k].template, e[k].separator)
klength := len(strings.Split(e[k].template, e[k].separator))
return jlength < klength
} }
// Swap swaps the elements with indexes i and j. // Swap swaps the elements with indexes i and j.

View File

@ -0,0 +1,14 @@
package templating
import "testing"
func BenchmarkTemplateLess(b *testing.B) {
a := templateSpec{
template: "aa|bb|cc|dd|ee|ff",
separator: "|",
}
specs := templateSpecs{a, a}
for i := 0; i < b.N; i++ {
specs.Less(0, 1)
}
}