Skip to content

Commit

Permalink
NOINLINE string8 and stringUtf8
Browse files Browse the repository at this point in the history
If they inline too early, our RULEs may be ineffective.  Better
performance was observed with `INLINE [0]` than `INLINE [1]`,
but `NOINLINE` works at least as well.
  • Loading branch information
hs-viktor committed Jan 15, 2023
1 parent 052affb commit 44fdcbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Data/ByteString/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ char8 :: Char -> Builder
char8 = P.primFixed P.char8

-- | Char8 encode a 'String'.
{-# INLINE [1] string8 #-} -- phased to allow literal cstring rewrites
{-# NOINLINE string8 #-}
string8 :: String -> Builder
string8 = P.primMapListFixed P.char8

Expand Down Expand Up @@ -471,7 +471,7 @@ charUtf8 = P.primBounded P.charUtf8
--
-- Note that 'stringUtf8' performs no codepoint validation and consequently may
-- emit invalid UTF-8 if asked (e.g. single surrogates).
{-# INLINE [1] stringUtf8 #-} -- phased to allow literal cstring rewrites
{-# NOINLINE stringUtf8 #-}
stringUtf8 :: String -> Builder
stringUtf8 = P.primMapListBounded P.charUtf8

Expand Down
8 changes: 8 additions & 0 deletions bench/BenchAll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ asclit, utflit :: Ptr Word8 -> Builder
asclit str@(Ptr addr) = BI.ascLiteralCopy str (byteCountLiteral addr)
utflit str@(Ptr addr) = BI.modUtf8LitCopy str (byteCountLiteral addr)

ascStr, utfStr :: String
ascStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
utfStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

main :: IO ()
main = do
defaultMain
Expand All @@ -273,6 +277,10 @@ main = do
, benchB' "String" () $ \() -> asclit (Ptr "hello world!"#)
, benchB' "AsciiLit" () $ \() -> asclit ascBuf
, benchB' "Utf8Lit" () $ \() -> utflit utfBuf
, benchB' "strLit" () $ \() -> string8 ascStr
, benchB' "utfLit" () $ \() -> stringUtf8 utfStr
, benchB' "strLitInline" () $ \() -> string8 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
, benchB' "utfLitInline" () $ \() -> stringUtf8 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
]

, bgroup "Encoding wrappers"
Expand Down

0 comments on commit 44fdcbc

Please sign in to comment.