Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sync.OnceValue for lazy regexp initialization #1279

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kke
Copy link

@kke kke commented Jun 13, 2024

Fixes Or Enhances

Replaces the local lazy initialization implementation with the stdlib sync.OnceValue.

Using sync.OnceValue instead of the local implementation brings some benefits in allocations, panic handling and mutex usage after initialization.

sync.OnceValue was introduced in go1.21 so backwards compatibility is provided via lazy_compat.go and build tags.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@kke kke requested a review from a team as a code owner June 13, 2024 06:53
@kke
Copy link
Author

kke commented Jun 13, 2024

sync.OnceValue was introduced in go1.21, so there has to be two files with buildtags as validator supports older go versions.

@coveralls
Copy link

Coverage Status

coverage: 74.285% (-0.006%) from 74.291%
when pulling 2c1e665 on kke:sync-oncevalue-regex
into a947377 on go-playground:master.

@coveralls
Copy link

Coverage Status

coverage: 74.285% (-0.006%) from 74.291%
when pulling 61e16c9 on kke:sync-oncevalue-regex
into a947377 on go-playground:master.

kke and others added 3 commits June 13, 2024 10:39
Using sync.OnceValue instead of the local implementation brings some
benefits in allocations, panic handling and mutex use / concurrent
access after initialization.

Signed-off-by: Kimmo Lehto <[email protected]>
sync.OnceValue was introduced in go1.21. Currently validator's go.mod
states minimum go version to be go1.18.

This commit moves the lazy regex initialization into its own file
lazy.go and provides backwards compatibility using buildtags and the
file lazy_compat.go which contains a backported (non-generic) version of
sync.OnceValue.

Signed-off-by: Kimmo Lehto <[email protected]>
@coveralls
Copy link

Coverage Status

coverage: 74.285% (-0.006%) from 74.291%
when pulling dac5fe6 on kke:sync-oncevalue-regex
into a947377 on go-playground:master.

@kke
Copy link
Author

kke commented Jun 13, 2024

coverage: 74.285% (-0.006%) from 74.291%

The coverage check does not seem to calculate correctly when files with build tags are involved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants