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 oneofValCacheLock = sync.Mutex{}
var oneofValsCache = map[string][]string{}
var oneofValsCacheRWLock = sync.RWMutex{}
func isOneOf(fl FieldLevel) bool {
param := fl.Param()
oneofValCacheLock.Lock()
vals, ok := oneofValCache[param]
func parseOneOfParam2(s string) []string {
oneofValsCacheRWLock.RLock()
vals, ok := oneofValsCache[s]
oneofValsCacheRWLock.RUnlock()
if !ok {
vals = strings.Fields(param)
oneofValCache[param] = vals
oneofValsCacheRWLock.Lock()
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()

@ -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