emersion / go-msgauth

🔏 A Go library and tools for DKIM, DMARC and Authentication-Results
MIT License
162 stars 51 forks source link

dkim: Only compile regex once #60

Closed 56KBs closed 7 months ago

56KBs commented 7 months ago

Only compile the regex for removing the b= value once during verification, reducing allocations & improving performance.

goos: windows
goarch: amd64
pkg: github.com/emersion/go-msgauth/dkim
cpu: AMD Ryzen 5 5600X 6-Core Processor
        │ original.txt │          regex-static.txt          │
        │    sec/op    │   sec/op     vs base               │
Name-12    47.95µ ± 0%   45.78µ ± 1%  -4.53% (p=0.000 n=10)

        │ original.txt │           regex-static.txt           │
        │     B/op     │     B/op      vs base                │
Name-12   18.95Ki ± 0%   16.14Ki ± 0%  -14.81% (p=0.000 n=10)

        │ original.txt │          regex-static.txt          │
        │  allocs/op   │ allocs/op   vs base                │
Name-12     186.0 ± 0%   157.0 ± 0%  -15.59% (p=0.000 n=10)
56KBs commented 7 months ago

I've been reading through the codebase some more and have spotted some other areas of code that could be modified to reduce CPU time/allocations/memory use - Would you be interested in me raising PRs for these @emersion? If so would you prefer one PR with multiple enhancements committed or one PR per enhancement?

emersion commented 7 months ago

Feel free to send PR(s). Note that I would rather keep the codebase simple: as long as the optimized versions don't decrease readability too much I'd welcome such changes. Regarding the PR(s), I don't mind as long as the changes are properly split logically per commit, so as you prefer: either one PR and you craft the logical commits, either multiple PRs and I can squash when merging.