@@ -1,5 +1,4 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2−{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE PackageImports #-}
2{-# LANGUAGE PackageImports #-}
4{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE RankNTypes #-}
@@ -15,9 +14,8 @@ import Criterion.Main
15import Data.Hashable
14import Data.Hashable
16import Data.Proxy (Proxy (..))
15import Data.Proxy (Proxy (..))
17import Data.Tagged (Tagged (..))
16import Data.Tagged (Tagged (..))
18−import qualified "aeson" Data.Aeson as A
17+import Data.Aeson
19−import qualified "aeson-benchmarks" Data.Aeson as B
20−import qualified "aeson-benchmarks" Data.Aeson.Types as B (fromJSONKeyCoerce)
18+import Data.Aeson.Types (fromJSONKeyCoerce)
21import qualified Data.ByteString.Lazy as LBS
19import qualified Data.ByteString.Lazy as LBS
22import qualified Data.HashMap.Strict as HM
20import qualified Data.HashMap.Strict as HM
23import qualified Data.Map as M
21import qualified Data.Map as M
@@ -47,10 +45,10 @@ instance NFData T1 where
47instance Hashable T1 where
45instance Hashable T1 where
48 hashWithSalt salt (T1 t) = hashWithSalt salt t
46 hashWithSalt salt (T1 t) = hashWithSalt salt t
50−instance B.FromJSON T1 where
48+instance FromJSON T1 where
51− parseJSON = B.withText "T1" $ pure . T1
49+ parseJSON = withText "T1" $ pure . T1
52−instance B.FromJSONKey T1 where
50+instance FromJSONKey T1 where
53− fromJSONKey = B.FromJSONKeyText T1
51+ fromJSONKey = FromJSONKeyText T1
55-------------------------------------------------------------------------------
53-------------------------------------------------------------------------------
@@ -64,10 +62,10 @@ instance NFData T2 where
64instance Hashable T2 where
62instance Hashable T2 where
65 hashWithSalt salt (T2 t) = hashWithSalt salt t
63 hashWithSalt salt (T2 t) = hashWithSalt salt t
67−instance B.FromJSON T2 where
65+instance FromJSON T2 where
68− parseJSON = B.withText "T2" $ pure . T2
66+ parseJSON = withText "T2" $ pure . T2
69−instance B.FromJSONKey T2 where
67+instance FromJSONKey T2 where
70− fromJSONKey = B.fromJSONKeyCoerce
68+ fromJSONKey = fromJSONKeyCoerce
72-------------------------------------------------------------------------------
70-------------------------------------------------------------------------------
73-- TextParser
71-- TextParser
@@ -81,10 +79,10 @@ instance NFData T3 where
81instance Hashable T3 where
79instance Hashable T3 where
82 hashWithSalt salt (T3 t) = hashWithSalt salt t
80 hashWithSalt salt (T3 t) = hashWithSalt salt t
84−instance B.FromJSON T3 where
82+instance FromJSON T3 where
85− parseJSON = B.withText "T3" $ pure . T3
83+ parseJSON = withText "T3" $ pure . T3
86−instance B.FromJSONKey T3 where
84+instance FromJSONKey T3 where
87− fromJSONKey = B.FromJSONKeyTextParser (pure . T3)
85+ fromJSONKey = FromJSONKeyTextParser (pure . T3)
89-------------------------------------------------------------------------------
87-------------------------------------------------------------------------------
@@ -97,40 +95,30 @@ value1000 = value 1000
97value10000 = value 10000
95value10000 = value 10000
99encodedValue10 :: LBS.ByteString
97encodedValue10 :: LBS.ByteString
100−encodedValue10 = B.encode value10
98+encodedValue10 = encode value10
102encodedValue100 :: LBS.ByteString
100encodedValue100 :: LBS.ByteString
103−encodedValue100 = B.encode value100
101+encodedValue100 = encode value100
105encodedValue1000 :: LBS.ByteString
103encodedValue1000 :: LBS.ByteString
106−encodedValue1000 = B.encode value1000
104+encodedValue1000 = encode value1000
108encodedValue10000 :: LBS.ByteString
106encodedValue10000 :: LBS.ByteString
109−encodedValue10000 = B.encode value10000
107+encodedValue10000 = encode value10000
111-------------------------------------------------------------------------------
109-------------------------------------------------------------------------------
112-- Helpers
110-- Helpers
113-------------------------------------------------------------------------------
111-------------------------------------------------------------------------------
115−decodeHMB
113+decodeHM
116− :: (B.FromJSONKey k, Eq k, Hashable k)
114+ :: (FromJSON (HM.HashMap k T.Text), Eq k, Hashable k)
117 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
115 => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
118−decodeHMB _ = B.decode
116+decodeHM _ = decode
120−decodeHMA
118+decodeMap
121− :: (A.FromJSON (HM.HashMap k T.Text), Eq k, Hashable k)
122− => Proxy k -> LBS.ByteString -> Maybe (HM.HashMap k T.Text)
123−decodeHMA _ = A.decode
126− :: (B.FromJSONKey k, Ord k)
127− => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
128−decodeMapB _ = B.decode
131− :: (A.FromJSON (M.Map k T.Text), Ord k)
119+ :: (FromJSON (M.Map k T.Text), Ord k)
132 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
120 => Proxy k -> LBS.ByteString -> Maybe (M.Map k T.Text)
133−decodeMapA _ = A.decode
121+decodeMap _ = decode
135proxyText :: Proxy T.Text
123proxyText :: Proxy T.Text
136proxyText = Proxy
124proxyText = Proxy
@@ -156,15 +144,14 @@ benchDecodeHM
156 -> LBS.ByteString
144 -> LBS.ByteString
157 -> Benchmark
145 -> Benchmark
158benchDecodeHM name val = bgroup name
146benchDecodeHM name val = bgroup name
159− [ bench "Text" $ nf (decodeHMB proxyText) val
147+ [ bench "Text" $ nf (decodeHM proxyText) val
160− , bench "Identity" $ nf (decodeHMB proxyT1) val
148+ , bench "Identity" $ nf (decodeHM proxyT1) val
161− , bench "Coerce" $ nf (decodeHMB proxyT2) val
149+ , bench "Coerce" $ nf (decodeHM proxyT2) val
162− , bench "Parser" $ nf (decodeHMB proxyT3) val
150+ , bench "Parser" $ nf (decodeHM proxyT3) val
163− , bench "aeson-hackage" $ nf (decodeHMA proxyText) val
164− , bench "Tagged Text" $ nf (decodeHMB $ proxyTagged proxyText) val
151+ , bench "Tagged Text" $ nf (decodeHM $ proxyTagged proxyText) val
165− , bench "Tagged Identity" $ nf (decodeHMB $ proxyTagged proxyT1) val
152+ , bench "Tagged Identity" $ nf (decodeHM $ proxyTagged proxyT1) val
166− , bench "Tagged Coerce" $ nf (decodeHMB $ proxyTagged proxyT2) val
153+ , bench "Tagged Coerce" $ nf (decodeHM $ proxyTagged proxyT2) val
167− , bench "Tagged Parser" $ nf (decodeHMB $ proxyTagged proxyT3) val
154+ , bench "Tagged Parser" $ nf (decodeHM $ proxyTagged proxyT3) val
170benchDecodeMap
157benchDecodeMap
@@ -172,11 +159,10 @@ benchDecodeMap
172 -> LBS.ByteString
159 -> LBS.ByteString
173 -> Benchmark
160 -> Benchmark
174benchDecodeMap name val = bgroup name
161benchDecodeMap name val = bgroup name
175− [ bench "Text" $ nf (decodeMapB proxyText) val
162+ [ bench "Text" $ nf (decodeMap proxyText) val
176− , bench "Identity" $ nf (decodeMapB proxyT1) val
163+ , bench "Identity" $ nf (decodeMap proxyT1) val
177− , bench "Coerce" $ nf (decodeMapB proxyT2) val
164+ , bench "Coerce" $ nf (decodeMap proxyT2) val
178− , bench "Parser" $ nf (decodeMapB proxyT3) val
165+ , bench "Parser" $ nf (decodeMap proxyT3) val
179− , bench "aeson-hackage" $ nf (decodeMapA proxyText) val
182benchEncodeHM
168benchEncodeHM
@@ -184,8 +170,7 @@ benchEncodeHM
184 -> HM.HashMap T.Text T.Text
170 -> HM.HashMap T.Text T.Text
185 -> Benchmark
171 -> Benchmark
186benchEncodeHM name val = bgroup name
172benchEncodeHM name val = bgroup name
187− [ bench "Text" $ nf B.encode val
173+ [ bench "Text" $ nf encode val
188− , bench "aeson-0.11" $ nf A.encode val
191benchEncodeMap
176benchEncodeMap
@@ -193,8 +178,7 @@ benchEncodeMap
193 -> HM.HashMap T.Text T.Text
178 -> HM.HashMap T.Text T.Text
194 -> Benchmark
179 -> Benchmark
195benchEncodeMap name val = bgroup name
180benchEncodeMap name val = bgroup name
196− [ bench "Text" $ nf B.encode val'
181+ [ bench "Text" $ nf encode val'
197− , bench "aeson-0.11" $ nf A.encode val'
200 val' :: M.Map T.Text T.Text
184 val' :: M.Map T.Text T.Text