Skip to content
  • koz_'s avatar
    Implement list `fold` and `foldMap` via mconcat · 6b1d0b9c
    koz_ authored and Marge Bot's avatar Marge Bot committed
    - This allows specialized mconcat implementations an opportunity to combine
      elements efficiently in a single pass.
    
    - Inline the default implementation of `mconcat`, this
      may result in list fusion.
    
    - In Monoids with strict `mappend`, implement `mconcat` as a strict left fold:
    
      * And (FiniteBits)
      * Ior (FiniteBits)
      * Xor (FiniteBits)
      * Iff (FiniteBits)
      * Max (Ord)
      * Min (Ord)
      * Sum (Num)
      * Product (Num)
      * (a -> m) (Monoid m)
    
    - Delegate mconcat for WrappedMonoid to the underlying monoid.
    
    Resolves: #17123
    
    Per the discussion in !4890, we expect some stat changes:
    
        * T17123(normal) run/alloc   403143160.0     4954736.0 -98.8% GOOD
    
          This is the expected improvement in `fold` for a long list of
          `Text` elements.
    
        * T13056(optasm) ghc/alloc   381013328.0   447700520.0 +17.5% BAD
    
          Here there's an extra simplifier run as a result of the new methods
          of the Foldable instance for List.  It looks benign.  The test is
          a micro benchmark that compiles just the derived foldable instances
          for a pair of structures, a cost of this magnitude is not expected
          to extend to more realistic programs.
    
        * T9198(normal) ghc/alloc   504661992.0   541334168.0  +7.3% BAD
    
          This test regressed from 8.10 and 9.0 back to exponential blowup.
          This metric also fluctuates, for reasons not yet clear.  The issue
          here is the exponetial blowup, not this MR.
    
    Metric Decrease:
        T17123
    Metric Increase:
        T9198
        T13056
    6b1d0b9c