purebred-mua / purebred-email

A fast email parsing library implemented in Haskell
https://hackage.haskell.org/package/purebred-email
GNU Affero General Public License v3.0
23 stars 4 forks source link

refactor foldUnstructured #58

Closed frasertweedale closed 4 years ago

frasertweedale commented 4 years ago

Refactor foldUnstructured and buildField to use a Builder instead of building an intermediate strict ByteString.

This is in anticipation of further changes to resolve https://github.com/purebred-mua/purebred-email/issues/56.

The new implementation was benchmarked against the old It performs similarly to the old on all input sizes, when building just the fields. Performance should improve a bit when rendering whole messages because the Builder will fuse into the larger message builder; no intermediate ByteStrings or chunks will be allocated.

At first it surprised me that performance was so similar, even for large inputs, given the large number of small strict ByteStrings being appended. But on further consideration, 'unwords' and 'intercalate' are O(n). These are two separate "layers" in the implementation but the overall complexity is O(n).