go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.98k stars 1.33k forks source link

Lazily initialize regexes to speed up `init()` #1246

Closed kylecarbs closed 5 months ago

kylecarbs commented 7 months ago

Using GODEBUG=inittrace=1 I found a relatively constant 10ms init caused by this function.

@go-playground/validator-maintainers

coveralls commented 7 months ago

Coverage Status

coverage: 74.228% (+0.02%) from 74.208% when pulling cebbc77c0ece34053127ee5adcafd819343143a0 on kylecarbs:master into a0f74b0fb2a7ae1750c0f0b0a49550d8b6e2e708 on go-playground:master.

deankarn commented 7 months ago

@kylecarbs out of curiosity, what is your use case where 10ms is not tolerable?

kylecarbs commented 7 months ago

@deankarn this package is imported in our CLI. Adding that overhead to every invocation (even help) isn't great.

kylecarbs commented 6 months ago

@deankarn any changes needed for me to push this through?

deankarn commented 5 months ago

@kylecarbs sorry for the lateness of my reply, been busy.

My only concern is the sync.Mutex overhead for longer running applications, can you change the implementation to use sync.Once instead which uses lower level atomics?

kylecarbs commented 5 months ago

@deankarn good idea. Will change in a bit!

deankarn commented 5 months ago

I will check back later tonight/tomorrow :)