[Haskell-cafe] Why is my mappend so slow?

martin martin.drautzburg at web.de
Fri Nov 20 11:14:34 UTC 2015

Am 11/20/2015 um 11:33 AM schrieb Roman Cheplyaka:
> On 11/20/2015 12:19 PM, martin wrote:
>> Am 11/20/2015 um 10:31 AM schrieb Tom Ellis:
>>> On Fri, Nov 20, 2015 at 10:18:46AM +0100, martin wrote:
>>>> (++) is only used in the Wtrs themselves, but these are only called when I am actually logging something,
>>>> which is only a handful of lines. Adding an mempty Logger does not change the number of lines written but
>>>> does change the execution time. So I don't think this is it.
>>> Surely every call logs *something* even if it's []?
>> Not really. If nothing gets logged, then my loggers return the original log. There is no (++) involved, not even
>> a ([] ++ log). The cost should only be the cost of checking the condition.
> Even for mempty, you're still recursively mappending the "tails" of the loggers. How about introducing a designated
> constructor for mempty to avoid that recursive mappend? (The same constructor will be used when a logger is "done"
> and is known not to produce any further output.)

This is probably the root cause, and your sugestion would probably solve the slowness of (<> mempty) and (mempty <>).

But for a Logger which just produces no output "for now" it still wouldn't help, would it? E.g. if I want to log every
n invocations, then a Logger will produce output at invocation n and then no output for invocations n+1 .. 2n -1, and
produce output at 2n again. It can never become an mempty.

This is actually the original problem which triggered it all. But my current design is overly costly. I cannnot seem
to express "if the condition does not hold, do nothing" propery.

More information about the Haskell-Cafe mailing list