swapped mutex for rwmutex

similiar single-goroutine performance, slightly better concurrent performance.
pull/343/head
Russ Egan 7 years ago
parent 15217303f9
commit 2e9adc3329
  1. 24
      baked_in.go
  2. 24
      benchmarks_test.go

@ -141,18 +141,24 @@ var (
} }
) )
var oneofValCache = map[string][]string{} var oneofValsCache = map[string][]string{}
var oneofValCacheLock = sync.Mutex{} var oneofValsCacheRWLock = sync.RWMutex{}
func isOneOf(fl FieldLevel) bool { func parseOneOfParam2(s string) []string {
param := fl.Param() oneofValsCacheRWLock.RLock()
oneofValCacheLock.Lock() vals, ok := oneofValsCache[s]
vals, ok := oneofValCache[param] oneofValsCacheRWLock.RUnlock()
if !ok { if !ok {
vals = strings.Fields(param) oneofValsCacheRWLock.Lock()
oneofValCache[param] = vals vals = strings.Fields(s)
oneofValsCache[s] = vals
oneofValsCacheRWLock.Unlock()
} }
oneofValCacheLock.Unlock() return vals
}
func isOneOf(fl FieldLevel) bool {
vals := parseOneOfParam2(fl.Param())
field := fl.Field() field := fl.Field()

@ -1184,3 +1184,27 @@ func BenchmarkStructComplexFailureParallel(b *testing.B) {
} }
}) })
} }
type TestOneof struct {
Color string `validate:"oneof=red green"`
}
func BenchmarkOneof(b *testing.B) {
w := &TestOneof{Color: "green"}
val := New()
for i := 0; i < b.N; i++ {
val.Struct(w)
}
}
func BenchmarkOneofParallel(b *testing.B) {
w := &TestOneof{Color: "green"}
val := New()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
val.Struct(w)
}
})
}

Loading…
Cancel
Save