commercialhaskell / rio

A standard library for Haskell
Other
838 stars 54 forks source link

Custom `Monoid` instance for `Utf8Builder` #166

Closed lehins closed 5 years ago

lehins commented 5 years ago

Because of derived instance with GeneralizedNewtypeDeriving, mconcat breaks list fusion and causes extra allocations when used with Utf8Builder. Here is a way to reproduce:

  runSimpleApp $ do
    logInfo $ foldr (<>) mempty $ fuseThis ["foo", "bar", "-", "<>"]
    logInfo $ foldr mappend mempty $ fuseThis ["foo", "bar", "-", "mappend"]
    logInfo $ mconcat $ fuseThis ["foo", "bar", "-", "mconcat"]

fuseThis comes from Data.List.Fusion.Probe

Code above results in:

foobar-<>
foobar-mappend
fuse-builder-exe: fuseThis: List did not fuse
CallStack (from HasCallStack):
  error, called at ./Data/List/Fusion/Probe.hs:52:16

But with the fix in this PR:

foobar-<>
foobar-mappend
foobar-mconcat