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

Email regex optimization #1353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nodivbyzero
Copy link
Contributor

Fixes Or Enhances

This PR simplifies the email regular expression, making it more efficient and easier to understand.
Here are benchmarks:

old regex:

Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -bench ^BenchmarkEmailValidation$ github.com/go-playground/validator/v10

goos: darwin
goarch: arm64
pkg: github.com/go-playground/validator/v10
cpu: Apple M1
BenchmarkEmailValidation/Valid_Emails-8         	  219237	      5463 ns/op	     112 B/op	       7 allocs/op
BenchmarkEmailValidation/Invalid_Emails-8       	  397093	      3025 ns/op	    1193 B/op	      23 allocs/op
BenchmarkEmailValidation/Mixed_Types-8          	 1730408	       693.1 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Valid_test@mail.com-8  	 2442416	       492.8 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_Dörte@Sörensen.example.com-8           	 1768521	       683.2 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_θσερ@εχαμπλε.ψομ-8                     	 1998978	       605.8 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_юзер@екзампл.ком-8                     	 2004871	       600.8 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_उपयोगकर्ता@उदाहरण.कॉम-8                                               	 1836072	       659.0 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_用户@例子.广告-8                                                            	 2668478	       450.8 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_"test_test"@email.com-8                                               	  658518	      1869 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Invalid_mail@domain_with_underscores.org-8                                  	 2313256	       505.2 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_Empty-8                                                             	 9073566	       131.2 ns/op	     184 B/op	       3 allocs/op
BenchmarkEmailValidation/Invalid_test@email-8                                                        	 2601570	       491.4 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_test@email.-8                                                       	 2248792	       524.7 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_@email.com-8                                                        	 5813140	       204.2 ns/op	     202 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_"@email.com-8                                                       	  996787	      1127 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Single_Validator-8                                                          	 2536804	       482.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkEmailValidation/New_Validator_Each_Time-8                                                   	   66536	     17918 ns/op	   15404 B/op	     191 allocs/op
PASS
ok  	github.com/go-playground/validator/v10	29.567s

vs. new regex

Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -bench ^BenchmarkEmailValidation$ github.com/go-playground/validator/v10

goos: darwin
goarch: arm64
pkg: github.com/go-playground/validator/v10
cpu: Apple M1
BenchmarkEmailValidation/Valid_Emails-8         	  325342	      4316 ns/op	     112 B/op	       7 allocs/op
BenchmarkEmailValidation/Invalid_Emails-8       	  545132	      2178 ns/op	    1194 B/op	      23 allocs/op
BenchmarkEmailValidation/Mixed_Types-8          	 1951177	       616.4 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Valid_test@mail.com-8  	 2884880	       417.1 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_Dörte@Sörensen.example.com-8           	 1963050	       608.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_θσερ@εχαμπλε.ψομ-8                     	 2448519	       492.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_юзер@екзампл.ком-8                     	 2276559	       527.4 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_उपयोगकर्ता@उदाहरण.कॉम-8                                               	 1928568	       643.4 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_用户@例子.广告-8                                                            	 3261272	       374.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Valid_"test_test"@email.com-8                                               	 2384032	       508.5 ns/op	      16 B/op	       1 allocs/op
BenchmarkEmailValidation/Invalid_mail@domain_with_underscores.org-8                                  	 2690359	       463.2 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_Empty-8                                                             	 9082252	       134.2 ns/op	     184 B/op	       3 allocs/op
BenchmarkEmailValidation/Invalid_test@email-8                                                        	 2825318	       419.1 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_test@email.-8                                                       	 2621067	       459.6 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_@email.com-8                                                        	 5833818	       205.0 ns/op	     202 B/op	       4 allocs/op
BenchmarkEmailValidation/Invalid_"@email.com-8                                                       	 2549175	       472.6 ns/op	     201 B/op	       4 allocs/op
BenchmarkEmailValidation/Single_Validator-8                                                          	 3027820	       395.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkEmailValidation/New_Validator_Each_Time-8                                                   	   67820	     17648 ns/op	   15404 B/op	     191 allocs/op
PASS
ok  	github.com/go-playground/validator/v10	29.909s

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

@nodivbyzero nodivbyzero requested a review from a team as a code owner December 26, 2024 21:10
@coveralls
Copy link

Coverage Status

coverage: 74.318%. remained the same
when pulling 2bf221f on nodivbyzero:email-regexp-optimization
into 6c3307e on go-playground:master.

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