kolmodin / binary

Efficient, pure binary serialisation using ByteStrings in Haskell.
Other
105 stars 67 forks source link

Ineffective INLINE pragmas on `many` method for `Alternative` `Get` instance. #196

Open AndreasPK opened 2 years ago

AndreasPK commented 2 years ago

Binary defines this instance:

-- | @since 0.7.0.0
instance Alternative Get where
  empty = C $ \inp _ks -> Fail inp "Data.Binary.Get(Alternative).empty"
  {-# INLINE empty #-}
  (<|>) f g = do
    (decoder, bs) <- runAndKeepTrack f
    case decoder of
      Done inp x -> C $ \_ ks -> ks inp x
      Fail _ _ -> pushBack bs >> g
      _ -> error "Binary: impossible"
  {-# INLINE (<|>) #-}
  some p = (:) <$> p <*> many p
  {-# INLINE some #-}
  many p = do
    v <- (Just <$> p) <|> pure Nothing
    case v of
      Nothing -> pure []
      Just x -> (:) x <$> many p
  {-# INLINE many #-}

But since many is recursive GHC will never inline it making the INLINE pragma ineffective.

If that's fine it could be made INLINEABLE instead.