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 2 commits 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

coveralls commented Dec 26, 2024

Coverage Status

coverage: 74.823% (+0.5%) from 74.318%
when pulling 75e6318 on nodivbyzero:email-regexp-optimization
into 6c3307e on go-playground:master.

@nodivbyzero nodivbyzero requested a review from deankarn January 18, 2025 02:05
@deankarn
Copy link
Contributor

I like the simplification and it appears correct, my only FUD is, how confident are we that it functions the same as the previous?

I mean the test still all pass etc.. but wanted to gauge you confidence level as well @nodivbyzero

@nodivbyzero
Copy link
Contributor Author

@deankarn
I'm pretty confident in this regex. Honestly, I used ChatGPT to optimize the original regex and then tested it with different email addresses.
All tests still pass, so I believe it functions the same as before.

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.

3 participants