Email regex optimization
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/[email protected] 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/[email protected] 2248792 524.7 ns/op 201 B/op 4 allocs/op
BenchmarkEmailValidation/[email protected] 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/[email protected] 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/[email protected] 2621067 459.6 ns/op 201 B/op 4 allocs/op
BenchmarkEmailValidation/[email protected] 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:
- [x] Tests exist or have been written that cover this particular change.
@go-playground/validator-maintainers
coverage: 74.823% (+0.5%) from 74.318% when pulling 75e6318a83b28ee6503e8266532dca87e3b2ddf0 on nodivbyzero:email-regexp-optimization into 6c3307e6c64040ebc0efffe9366927c68146ffba on go-playground:master.
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
@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.