This commit is contained in:
gobwas 2016-02-02 22:20:26 +03:00
parent 57a5246fac
commit 61a66d485f
3 changed files with 49 additions and 109 deletions

View File

@ -168,6 +168,15 @@ func BenchmarkAllGlobMatch(b *testing.B) {
_ = m.Match(fixture_all_match)
}
}
func BenchmarkAllGlobMatchParallel(b *testing.B) {
m, _ := Compile(pattern_all)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = m.Match(fixture_all_match)
}
})
}
func BenchmarkAllRegexpMatch(b *testing.B) {
m := regexp.MustCompile(regexp_all)
f := []byte(fixture_all_match)

View File

@ -42,8 +42,15 @@ func toPowerOfTwo(v int) int {
return v
}
const (
minSegment = 32
minSegmentMinusOne = 31
maxSegment = 1024
maxSegmentMinusOne = 1023
)
func init() {
for i := 1024; i >= 1; i >>= 1 {
for i := maxSegment; i >= minSegment; i >>= 1 {
func(i int) {
segmentsPools[i-1] = sync.Pool{
New: func() interface{} {
@ -54,41 +61,29 @@ func init() {
}
}
var segmentsPool = sync.Pool{
New: func() interface{} {
return make([]int, 0, 64)
},
}
func getIdx(c int) int {
p := toPowerOfTwo(c)
switch {
case p >= 1024:
return 1023
case p < 1:
return 0
case p >= maxSegment:
return maxSegmentMinusOne
case p <= minSegment:
return minSegmentMinusOne
default:
return p - 1
}
}
func acquireSegments(c int) []int {
// fmt.Println("GET", getIdx(c))
return segmentsPools[getIdx(c)].Get().([]int)[:0]
}
func releaseSegments(s []int) {
// fmt.Println("PUT", getIdx(cap(s)))
segmentsPools[getIdx(cap(s))].Put(s)
}
func appendIfNotAsPrevious(target []int, val int) []int {
l := len(target)
if l != 0 && target[l-1] == val {
return target
}
return append(target, val)
}
// appendMerge merges and sorts given already SORTED and UNIQUE segments.
func appendMerge(target, sub []int) []int {
lt, ls := len(target), len(sub)
out := acquireSegments(lt + ls)
@ -131,64 +126,6 @@ func appendMerge(target, sub []int) []int {
return target
}
// mergeSegments merges and sorts given already SORTED and UNIQUE segments.
func mergeSegments(list [][]int, out []int) []int {
var current []int
switch len(list) {
case 0:
return out
case 1:
return list[0]
default:
current = acquireSegments(len(list[0]))
current = append(current, list[0]...)
// releaseSegments(list[0])
}
for _, s := range list[1:] {
next := acquireSegments(len(current) + len(s))
for x, y := 0, 0; x < len(current) || y < len(s); {
if x >= len(current) {
next = append(next, s[y:]...)
break
}
if y >= len(s) {
next = append(next, current[x:]...)
break
}
xValue := current[x]
yValue := s[y]
switch {
case xValue == yValue:
x++
y++
next = appendIfNotAsPrevious(next, xValue)
case xValue < yValue:
next = appendIfNotAsPrevious(next, xValue)
x++
case yValue < xValue:
next = appendIfNotAsPrevious(next, yValue)
y++
}
}
releaseSegments(current)
current = next
}
out = append(out, current...)
releaseSegments(current)
return out
}
func reverseSegments(input []int) {
l := len(input)
m := l / 2

View File

@ -1,32 +1,26 @@
benchmark | old ns/op | new ns/op | delta
-----------------------------------------------|-----------|-------------|-----------
BenchmarkAllGlobMatch-4 512 711 +38.87%
BenchmarkMultipleGlobMatch-4 121 417 +244.63%
BenchmarkAlternativesGlobMatch-4 166 300 +80.72%
BenchmarkAlternativesSuffixFirstGlobMatch-4 23.5 292 +1142.55%
BenchmarkAlternativesSuffixSecondGlobMatch-4 29.8 355 +1091.28%
BenchmarkAlternativesCombineLiteGlobMatch-4 161 250 +55.28%
BenchmarkAlternativesCombineHardGlobMatch-4 325 334 +2.77%
BenchmarkPlainGlobMatch-4 7.20 154 +2038.89%
BenchmarkPrefixGlobMatch-4 8.75 113 +1191.43%
BenchmarkSuffixGlobMatch-4 9.07 115 +1167.92%
BenchmarkPrefixSuffixGlobMatch-4 15.1 125 +727.81%
benchmark old ns/op new ns/op delta
BenchmarkIndexPrefix-4 85.1 55.9 -34.31%
BenchmarkIndexRange-4 170(143) 60.6 -64.35%
BenchmarkRowIndex-4 172(128) 94.0 -45.35%
BenchmarkIndexSingle-4 61.0(16) 35.8 -41.31%
BenchmarkIndexSuffix-4 84.8 55.7 -34.32%
BenchmarkIndexSuper-4 461(180) 192 -58.35%
BenchmarkIndexText-4 84.6 54.4 -35.70%
BenchmarkIndexPrefixSuffix-4 84.3 57.2 -32.15%
BenchmarkIndexNothing-4 452(3.31) 92.8 -79.47% XXX
BenchmarkIndexMin-4 516(274) 161 -68.80%
BenchmarkIndexMax-4 442(88) 92.4 -79.10%
BenchmarkIndexList-4 151(41) 51.1 -66.16%
BenchmarkIndexContains-4 492(220) 247 -49.80%
BenchmarkIndexAny-4 887(222) 255 -71.25%
BenchmarkAllGlobMatch-4 519 1024 +97.30%
BenchmarkMultipleGlobMatch-4 123 218 +77.24%
BenchmarkAlternativesGlobMatch-4 164 283 +72.56%
BenchmarkAlternativesSuffixFirstGlobMatch-4 23.6 23.5 -0.42%
BenchmarkAlternativesSuffixSecondGlobMatch-4 29.7 30.1 +1.35%
BenchmarkAlternativesCombineLiteGlobMatch-4 161 352 +118.63%
BenchmarkAlternativesCombineHardGlobMatch-4 321 649 +102.18%
BenchmarkPlainGlobMatch-4 7.17 7.09 -1.12%
BenchmarkPrefixGlobMatch-4 8.74 8.64 -1.14%
BenchmarkSuffixGlobMatch-4 10.3 9.06 -12.04%
BenchmarkPrefixSuffixGlobMatch-4 31.0 15.1 -51.29%
BenchmarkIndexAny-4 1414 232 -83.59%
BenchmarkIndexContains-4 557 250 -55.12%
BenchmarkIndexList-4 207 42.6 -79.42%
BenchmarkIndexMax-4 630 111 -82.38%
BenchmarkIndexMin-4 515 328 -36.31%
BenchmarkIndexPrefixSuffix-4 97.9 86.2 -11.95%
BenchmarkIndexPrefix-4 86.1 84.0 -2.44%
BenchmarkIndexRange-4 181 144 -20.44%
BenchmarkRowIndex-4 185 127 -31.35%
BenchmarkIndexSingle-4 82.6 16.0 -80.63%
BenchmarkIndexSuffix-4 85.5 84.9 -0.70%
BenchmarkIndexSuper-4 450 196 -56.44%
BenchmarkIndexText-4 85.3 85.9 +0.70%