emersion / go-msgauth

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

reduce relaxedBodyCanonicalizer allocations #46

Closed pierrre closed 3 years ago

pierrre commented 3 years ago

In my application, nearly 50% of my memory allocations (in count) are caused by relaxedBodyCanonicalizer. https://github.com/emersion/go-msgauth/blob/6dd5b6c922fa55520e0bf188c91156624b737cc7/dkim/canonical.go#L130-L158 Especially lines 146 and 149.

I think this code could be optimized. Currently each call to append causes a new memory allocation and requires to copy the memory. Because the slices are reset to nil. It doesn't reuse the memory previously allocated. The solution: do c.wspBuf = c.wspBuf[:0] instead of c.wspBuf = nil. Same for crlfBuf.

WDYT ?

emersion commented 3 years ago

wspBuf could be replaced with a wsp bool field, since we never use the actual value. Your suggestion looks like a good idea for crlfBuf.

Patches welcome!

pierrre commented 3 years ago

wspBuf could be replaced with a wsp bool field

Yes I noticed that too :sweat_smile:

I'll work on it tomorrow.

pierrre commented 3 years ago

Here is the PR https://github.com/emersion/go-msgauth/pull/47