Skip to content

Commit 02bfcd7

Browse files
committed
added support for Show/Read
1 parent 55632bd commit 02bfcd7

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

serialization.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ benchmark all
5050
filepath,
5151
containers >= 0.5.6.2,
5252
criterion >= 1.4.1.0,
53+
text,
5354
serialise,
5455
cereal,
5556
binary,

src/Main.hs

+32-23
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@ module Main where
1515
import Control.DeepSeq
1616
import qualified Data.ByteString as BS
1717
import qualified Data.ByteString.Lazy as LBS
18-
19-
-- import Data.Monoid ((<>))
18+
import qualified Data.Text as T
19+
import qualified Data.Text.Encoding as T
2020
import Data.Typeable
21-
22-
-- import Data.Word
23-
-- import Criterion.IO
2421
import Criterion.Types
25-
26-
-- import Data.Bifunctor
2722
import qualified Data.Binary as B
28-
29-
-- import Data.Binary.Serialise.CBOR as CBOR
3023
import Codec.Serialise as CBOR
31-
32-
-- import Data.List
33-
-- import Data.Ord
3424
import GHC.Generics
35-
36-
-- import Statistics.Resampling.Bootstrap
3725
import System.Mem (performMajorGC)
38-
39-
-- import System.Random
40-
-- import Text.Printf
41-
-- import Data.Binary.Serialise.CBOR.Decoding as CBOR
42-
-- import Data.Binary.Serialise.CBOR.Encoding as CBOR
4326
import qualified Data.Flat as F
4427
import qualified Data.Serialize as C
4528
import qualified Data.Store as S
@@ -58,7 +41,7 @@ data BinTree a
5841
= Tree (BinTree a)
5942
(BinTree a)
6043
| Leaf a
61-
deriving (Show, Eq, Typeable, Generic)
44+
deriving (Show, Read, Eq, Typeable, Generic)
6245

6346
-- General instances
6447
instance {-# OVERLAPPABLE #-} F.Flat a => F.Flat (BinTree a)
@@ -88,6 +71,10 @@ instance {-# OVERLAPPABLE #-} CBOR.Serialise a =>
8871
-- instance {-# OVERLAPPING #-} CBOR.Serialise [Direction]
8972
-- instance {-# OVERLAPPING #-} CBOR.Serialise (BinTree Direction)
9073
-- instance {-# OVERLAPPING #-} CBOR.Serialise (BinTree Int)
74+
75+
-- instance {-# OVERLAPPING #-} F.Flat [Car]
76+
-- instance {-# OVERLAPPING #-} F.Flat [Iris]
77+
9178
instance NFData a => NFData (BinTree a) where
9279
rnf (Leaf a) = rnf a `seq` ()
9380
rnf (Tree left right) = rnf left `seq` rnf right `seq` ()
@@ -178,6 +165,9 @@ data PkgFlat =
178165
data PkgStore =
179166
PkgStore
180167

168+
data PkgShow =
169+
PkgShow
170+
181171
class Serialize lib a where
182172
serialize :: lib -> a -> IO BS.ByteString
183173
deserialize :: lib -> BS.ByteString -> IO a
@@ -220,6 +210,12 @@ instance (F.Flat a, NFData a) => Serialize PkgFlat a where
220210
{-# NOINLINE deserialize #-}
221211
deserialize _ = return . force . fromRight . F.unflat
222212

213+
instance (Show a, Read a, NFData a) => Serialize PkgShow a where
214+
{-# NOINLINE serialize #-}
215+
serialize _ = return . force . T.encodeUtf8 . T.pack . show
216+
{-# NOINLINE deserialize #-}
217+
deserialize _ = return . force . read . T.unpack . T.decodeUtf8
218+
223219
pkgs ::
224220
( NFData a
225221
, C.Serialize a
@@ -228,15 +224,22 @@ pkgs ::
228224
, S.Store a
229225
, F.Flat a
230226
, B.Binary a
227+
, Show a
228+
, Read a
231229
)
232230
=> [(String, a -> IO BS.ByteString, BS.ByteString -> IO a)]
231+
-- pkgs =
232+
-- [ ("flat-ser", serialize PkgFlat, deserialize PkgFlat)
233+
-- , ("store-ser", serialize PkgStore, deserialize PkgStore)]
234+
-- ]
233235
pkgs =
234236
[ ("flat", serialize PkgFlat, deserialize PkgFlat)
235237
, ("store", serialize PkgStore, deserialize PkgStore)
236238
, ("binary", serialize PkgBinary, deserialize PkgBinary)
237239
, ("cereal", serialize PkgCereal, deserialize PkgCereal)
238240
, ("packman", serialize PkgPackman, deserialize PkgPackman)
239241
, ("serialise", serialize PkgCBOR, deserialize PkgCBOR)
242+
, ("show", serialize PkgShow, deserialize PkgShow)
240243
]
241244

242245
prop :: Serialize lib (BinTree Int) => lib -> Property
@@ -266,6 +269,8 @@ runBench :: IO ()
266269
runBench
267270
-- Data structures to (de)serialise
268271
= do
272+
273+
-- datasets
269274
!intTree <-
270275
force . ("BinTree Int", ) <$> (generateBalancedTree 21 :: IO (BinTree Int))
271276
!directionTree <-
@@ -277,14 +282,18 @@ runBench
277282
!carsDataset <- force . ("Cars", ) <$> carsData
278283
-- !abaloneDataset <- force . ("Abalone dataset",) <$> abaloneData
279284
let !irisDataset = force ("Iris", irisData)
285+
280286
performMajorGC
287+
281288
let jsonReport = reportsFile workDir
282289
let htmlReport = "report.html"
290+
283291
let tests =
284292
benchs directionList ++
285293
benchs intTree ++
286294
benchs directionTree ++ benchs carsDataset ++ benchs irisDataset
287295
-- let tests = []
296+
288297
defaultMainWith
289298
(defaultConfig {jsonFile = Just jsonReport, reportFile = Just htmlReport}) $
290299
tests
@@ -316,7 +325,7 @@ sizes ::
316325
, F.Flat t
317326
, Serialise t
318327
, C.Serialize t
319-
, S.Store t
328+
, S.Store t,Show t, Read t
320329
)
321330
=> (String, t)
322331
-> IO ()
@@ -336,15 +345,15 @@ benchs ::
336345
, F.Flat a
337346
, Serialise a
338347
, C.Serialize a
339-
, S.Store a
348+
, S.Store a,Read a,Show a
340349
)
341350
=> (String, a)
342351
-> [Benchmark]
343352
benchs (name, obj) =
344353
let nm pkg = concat [name, "-", pkg]
345354
-- env (return obj) $ \sobj -> bgroup ("serialization (mSecs)") $ map (\(pkg,s,_) -> bench (nm pkg) (nfIO (s sobj))) pkgs
346355
in [ bgroup "serialization (time)" $
347-
map (\(pkg, s, _) -> bench (nm pkg) (nfIO (s obj))) pkgs
356+
map (\(pkg, s, _) -> bench (nm pkg) (nfIO (BS.length <$> s obj))) pkgs
348357
-- NOTE: the benchmark time includes the comparison of the deserialised obj with the original
349358
, bgroup "deserialization (time)" $
350359
map

0 commit comments

Comments
 (0)