@@ -1,9 +1,7 @@
1−{-# LANGUAGE OverloadedStrings, RankNTypes #-}
1+{-# LANGUAGE OverloadedStrings, RankNTypes, PackageImports, FlexibleContexts #-}
3import Control.DeepSeq
3import Control.DeepSeq
4import Criterion.Main
4import Criterion.Main
6−import Data.Aeson.Types (fromJSONKeyCoerce)
7import Data.Hashable
5import Data.Hashable
8import Data.Proxy (Proxy (..))
6import Data.Proxy (Proxy (..))
9import Data.Tagged (Tagged (..))
7import Data.Tagged (Tagged (..))
@@ -12,6 +10,11 @@ import qualified Data.Text as T
12import qualified Data.Map as M
10import qualified Data.Map as M
13import qualified Data.HashMap.Strict as HM
11import qualified Data.HashMap.Strict as HM
13+import qualified "aeson" Data.Aeson as A
15+import qualified "aeson-benchmarks" Data.Aeson as B
16+import qualified "aeson-benchmarks" Data.Aeson.Types as B (fromJSONKeyCoerce)
15value :: Int -> HM.HashMap T.Text T.Text
18value :: Int -> HM.HashMap T.Text T.Text
16value n = HM.fromList $ map f [1..n]
19value n = HM.fromList $ map f [1..n]
@@ -35,10 +38,11 @@ instance NFData T1 where
35 rnf (T1 t) = rnf t
38 rnf (T1 t) = rnf t
36instance Hashable T1 where
39instance Hashable T1 where
37 hashWithSalt salt (T1 t) = hashWithSalt salt t
40 hashWithSalt salt (T1 t) = hashWithSalt salt t
38−instance FromJSON T1 where
42+instance B.FromJSON T1 where
39− parseJSON = withText "T1" $ pure . T1
43+ parseJSON = B.withText "T1" $ pure . T1
40−instance FromJSONKey T1 where
44+instance B.FromJSONKey T1 where
41− fromJSONKey = FromJSONKeyText T1
45+ fromJSONKey = B.FromJSONKeyText T1
43-------------------------------------------------------------------------------
47-------------------------------------------------------------------------------
@@ -51,10 +55,11 @@ instance NFData T2 where
51 rnf (T2 t) = rnf t
55 rnf (T2 t) = rnf t
52instance Hashable T2 where
56instance Hashable T2 where
53 hashWithSalt salt (T2 t) = hashWithSalt salt t
57 hashWithSalt salt (T2 t) = hashWithSalt salt t
54−instance FromJSON T2 where
59+instance B.FromJSON T2 where
55− parseJSON = withText "T2" $ pure . T2
60+ parseJSON = B.withText "T2" $ pure . T2
56−instance FromJSONKey T2 where
61+instance B.FromJSONKey T2 where
57− fromJSONKey = fromJSONKeyCoerce
62+ fromJSONKey = B.fromJSONKeyCoerce
59-------------------------------------------------------------------------------
64-------------------------------------------------------------------------------
60-- TextParser
65-- TextParser
@@ -67,10 +72,11 @@ instance NFData T3 where
67 rnf (T3 t) = rnf t
72 rnf (T3 t) = rnf t
68instance Hashable T3 where
73instance Hashable T3 where
69 hashWithSalt salt (T3 t) = hashWithSalt salt t
74 hashWithSalt salt (T3 t) = hashWithSalt salt t
70−instance FromJSON T3 where
76+instance B.FromJSON T3 where
71− parseJSON = withText "T3" $ pure . T3
77+ parseJSON = B.withText "T3" $ pure . T3
72−instance FromJSONKey T3 where
78+instance B.FromJSONKey T3 where
73− fromJSONKey = FromJSONKeyTextParser (pure . T3)
79+ fromJSONKey = B.FromJSONKeyTextParser (pure . T3)
75-------------------------------------------------------------------------------
81-------------------------------------------------------------------------------
@@ -83,30 +89,40 @@ value1000 = value 1000
83value10000 = value 10000
89value10000 = value 10000
85encodedValue10 :: LBS.ByteString
91encodedValue10 :: LBS.ByteString
86−encodedValue10 = encode $ value10
92+encodedValue10 = B.encode $ value10
88encodedValue100 :: LBS.ByteString
94encodedValue100 :: LBS.ByteString
89−encodedValue100 = encode $ value100
95+encodedValue100 = B.encode $ value100
91encodedValue1000 :: LBS.ByteString
97encodedValue1000 :: LBS.ByteString
92−encodedValue1000 = encode $ value1000
98+encodedValue1000 = B.encode $ value1000
94encodedValue10000 :: LBS.ByteString
100encodedValue10000 :: LBS.ByteString
95−encodedValue10000 = encode $ value10000
101+encodedValue10000 = B.encode $ value10000
97-------------------------------------------------------------------------------
103-------------------------------------------------------------------------------
98-- Helpers
104-- Helpers
99-------------------------------------------------------------------------------
105-------------------------------------------------------------------------------
101−decodeHM
107+decodeHMB
102− :: (FromJSONKey k, Eq k, Hashable k)
108+ :: (B.FromJSONKey k, Eq k, Hashable k)
103 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
109 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
104−decodeHM _ = decode
110+decodeHMB _ = B.decode
113+ :: (A.FromJSON (HM.HashMap k T.Text), Eq k, Hashable k)
114+ => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
115+decodeHMA _ = A.decode
118+ :: (B.FromJSONKey k, Ord k)
119+ => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
120+decodeMapB _ = B.decode
106−decodeMap
122+decodeMapA
107− :: (FromJSONKey k, Ord k)
123+ :: (A.FromJSON (M.Map k T.Text), Ord k)
108 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
124 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
109−decodeMap _ = decode
125+decodeMapA _ = A.decode
111proxyText :: Proxy T.Text
127proxyText :: Proxy T.Text
112proxyText = Proxy
128proxyText = Proxy
@@ -132,14 +148,15 @@ benchDecodeHM
132 -> LBS.ByteString
148 -> LBS.ByteString
133 -> Benchmark
149 -> Benchmark
134benchDecodeHM name val = bgroup name
150benchDecodeHM name val = bgroup name
135− [ bench "Text" $ nf (decodeHM proxyText) val
151+ [ bench "Text" $ nf (decodeHMB proxyText) val
136− , bench "Identity" $ nf (decodeHM proxyT1) val
152+ , bench "Identity" $ nf (decodeHMB proxyT1) val
137− , bench "Coerce" $ nf (decodeHM proxyT2) val
153+ , bench "Coerce" $ nf (decodeHMB proxyT2) val
138− , bench "Parser" $ nf (decodeHM proxyT3) val
154+ , bench "Parser" $ nf (decodeHMB proxyT3) val
155+ , bench "aeson-0.11" $ nf (decodeHMA proxyText) val
139− , bench "Tagged Text" $ nf (decodeHM $ proxyTagged proxyText) val
156+ , bench "Tagged Text" $ nf (decodeHMB $ proxyTagged proxyText) val
140− , bench "Tagged Identity" $ nf (decodeHM $ proxyTagged proxyT1) val
157+ , bench "Tagged Identity" $ nf (decodeHMB $ proxyTagged proxyT1) val
141− , bench "Tagged Coerce" $ nf (decodeHM $ proxyTagged proxyT2) val
158+ , bench "Tagged Coerce" $ nf (decodeHMB $ proxyTagged proxyT2) val
142− , bench "Tagged Parser" $ nf (decodeHM $ proxyTagged proxyT3) val
159+ , bench "Tagged Parser" $ nf (decodeHMB $ proxyTagged proxyT3) val
145benchDecodeMap
162benchDecodeMap
@@ -147,10 +164,11 @@ benchDecodeMap
147 -> LBS.ByteString
164 -> LBS.ByteString
148 -> Benchmark
165 -> Benchmark
149benchDecodeMap name val = bgroup name
166benchDecodeMap name val = bgroup name
150− [ bench "Text" $ nf (decodeMap proxyText) val
167+ [ bench "Text" $ nf (decodeMapB proxyText) val
151− , bench "Identity" $ nf (decodeMap proxyT1) val
168+ , bench "Identity" $ nf (decodeMapB proxyT1) val
152− , bench "Coerce" $ nf (decodeMap proxyT2) val
169+ , bench "Coerce" $ nf (decodeMapB proxyT2) val
153− , bench "Parser" $ nf (decodeMap proxyT3) val
170+ , bench "Parser" $ nf (decodeMapB proxyT3) val
171+ , bench "aeson-0.11" $ nf (decodeMapA proxyText) val
156benchEncodeHM
174benchEncodeHM
@@ -158,7 +176,8 @@ benchEncodeHM
158 -> HM.HashMap T.Text T.Text
176 -> HM.HashMap T.Text T.Text
159 -> Benchmark
177 -> Benchmark
160benchEncodeHM name val = bgroup name
178benchEncodeHM name val = bgroup name
179+ [ bench "Text" $ nf B.encode val
161− [ bench "Text" $ nf encode val
180+ , bench "aeson-0.11" $ nf A.encode val
164benchEncodeMap
183benchEncodeMap
@@ -166,7 +185,8 @@ benchEncodeMap
166 -> HM.HashMap T.Text T.Text
185 -> HM.HashMap T.Text T.Text
167 -> Benchmark
186 -> Benchmark
168benchEncodeMap name val = bgroup name
187benchEncodeMap name val = bgroup name
188+ [ bench "Text" $ nf B.encode val'
169− [ bench "Text" $ nf encode val'
189+ , bench "aeson-0.11" $ nf A.encode val'
172 val' :: M.Map T.Text T.Text
192 val' :: M.Map T.Text T.Text