on master help sign in
Microbenchmark time! Let's compare direct string encoding vs via text

Turns out that going via text is actually *faster* -- unless you inline the direct string function, in which case it wins. Inlining the text function has no effect on performance.

commitb2ceb12849ee8cf904efdbf6d52a7c4160aa73b6 authorBryan O'Sullivan <bos@serpentine.com> dateMon 6 Jul 2015, 19:54 (11 years ago) parent7c3a89c
2 files changed+18−5
Data/Aeson/Encode/Builder.hs+1 benchmarks/aeson-benchmarks.cabal+17−5
Data/Aeson/Encode/Builder.hs+1
@@ -96,6 +96,7 @@ string :: String -> Builder
9696string t = B.char8 '"' <> foldMap go t <> B.char8 '"'
9797 where go c | c > '\x7f' = B.charUtf8 c
9898 | otherwise = BP.primBounded escapeAscii (c2w c)
99+{-# INLINE string #-}
99100
100101escapeAscii :: BP.BoundedPrim Word8
101102escapeAscii =
benchmarks/aeson-benchmarks.cabal+17−5
@@ -16,15 +16,13 @@ library
1616 exposed-modules:
1717 Data.Aeson
1818 Data.Aeson.Encode
19− Data.Aeson.Parser
20− Data.Aeson.Types
21− Data.Aeson.TH
22−
23− other-modules:
2419 Data.Aeson.Encode.Builder
2520 Data.Aeson.Encode.Functions
2621 Data.Aeson.Functions
22+ Data.Aeson.Parser
2723 Data.Aeson.Parser.Internal
24+ Data.Aeson.TH
25+ Data.Aeson.Types
2826 Data.Aeson.Types.Class
2927 Data.Aeson.Types.Generic
3028 Data.Aeson.Types.Instances
@@ -57,6 +55,20 @@ library
5755 ghc-options: -O2 -Wall
5856 cpp-options: -DGENERICS
5957
58+executable aeson-benchmark-micro
59+ main-is: Micro.hs
60+ hs-source-dirs: ../examples .
61+ ghc-options: -Wall -O2 -rtsopts
62+ build-depends:
63+ aeson-benchmarks,
64+ base,
65+ bytestring,
66+ criterion >= 1.0,
67+ deepseq,
68+ ghc-prim,
69+ text,
70+ time
71+
6072executable aeson-benchmark-typed
6173 main-is: Typed.hs
6274 hs-source-dirs: ../examples .