Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for value serialization #14

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion benchmark/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Streamly.Internal.Data.Serialize hiding (encode)
import qualified Streamly.Data.Stream as Stream
import qualified Data.Text as TextS
import qualified Data.Text.Lazy as TextL
import qualified Data.Aeson as Aeson
import qualified Data.Vector as Vector

import Test.Tasty.Bench

Expand Down Expand Up @@ -171,18 +173,22 @@ main = do
!lazyText <- do
testSList <- Stream.replicateM 20 (genStrictText 50) & Stream.toList
pure $ force $ TextL.fromChunks testSList
!strictAeson <- genStrictAeson 1000

-- Asserts
when (not (TextS.length strictText == 1000))
(error "TextS.length strictText == 1000")
when (not (TextL.length lazyText == 1000))
(error "TextL.length lazyText == 1000")

when (not (arrayLength strictAeson == 1000))
(error "Vector.length strictAeson == 1000")

-- Benchmarks
defaultMain
[ bencher "[Int]" intList 100
, bencher "Strict.Text" strictText 100
, bencher "Lazy.Text" lazyText 100
, bencher "Strict.Aeson" strictAeson 100
]

where
Expand All @@ -192,3 +198,13 @@ main = do
Stream.replicateM n genChar
& Stream.toList
& fmap (force . TextS.pack)

genStrictAeson :: Int -> IO Aeson.Value
genStrictAeson n = do
aesonList <- Vector.replicateM n (generate (arbitrary :: Gen Aeson.Value))
let aesonArray = Aeson.Array aesonList
pure aesonArray

arrayLength :: Aeson.Value -> Int
arrayLength (Aeson.Array arr) = Vector.length (Vector.fromList $ Vector.toList arr)
arrayLength _ = 0
21 changes: 6 additions & 15 deletions src/Streamly/Data/Serialize/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ module Streamly.Data.Serialize.Instances () where
--------------------------------------------------------------------------------

import Data.Fixed (Fixed)
import Data.Maybe (fromJust)
import Data.Scientific (Scientific)
import Data.Time (Day, TimeOfDay, LocalTime, DiffTime, UTCTime)
import Streamly.Data.Serialize.Instances.Text ()
import Streamly.Internal.Data.Serialize (Serialize(..))
import Data.Aeson.KeyMap (KeyMap, Key)
import Data.Map (Map)

import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Vector as Vector
import qualified Streamly.Internal.Data.Serialize.TH as Serialize

--------------------------------------------------------------------------------
-- Time
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -66,19 +66,10 @@ instance Serialize BSL.ByteString where
-- Aeson.Value
--------------------------------------------------------------------------------

-- TODO: Serialize it independently

-- XXX Extremely inefficient serialization of Aeson.Value
instance Serialize Aeson.Value where
{-# INLINE size #-}
size i val = size i (Aeson.encode val)

{-# INLINE deserialize #-}
deserialize off arr end =
fmap (fromJust . Aeson.decode) <$> deserialize off arr end

{-# INLINE serialize #-}
serialize off arr val = serialize off arr (Aeson.encode val)
$(Serialize.deriveSerialize ''Key)
$(Serialize.deriveSerialize ''Map)
$(Serialize.deriveSerialize ''KeyMap)
$(Serialize.deriveSerialize ''Aeson.Value)

--------------------------------------------------------------------------------
-- Vector.Vector
Expand Down
6 changes: 5 additions & 1 deletion streamly-serialize-instances.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ library
, text >= 1.2 && < 2.1
, bytestring >= 0.10.0 && < 0.13.0
, vector
, aeson
, aeson >= 2.0 && < 3.0
, time
, scientific
, containers >= 0.6.7 && < 1.0.0

-- Directories containing source files.
hs-source-dirs: src
Expand Down Expand Up @@ -163,6 +164,7 @@ test-suite serialization-tests
, QuickCheck
, time
, text
, aeson >= 2.0 && < 3.0

benchmark serialization-benchmarks
-- Import common warning flags.
Expand Down Expand Up @@ -199,3 +201,5 @@ benchmark serialization-benchmarks
, tasty >= 1.4.1 && < 1.5
, text
, QuickCheck
, aeson >= 2.0 && < 3.0
, vector
4 changes: 4 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Data.Time (UTCTime)

import qualified Data.Text as TextS
import qualified Data.Text.Lazy as TextL
import qualified Data.Aeson as Aeson

import Test.Hspec.QuickCheck
import Test.Hspec as H
Expand Down Expand Up @@ -89,6 +90,9 @@ testCases = do
prop "Strict Text"
$ \(x :: TextS.Text) -> roundtrip x

prop "Aeson"
$ \(x :: Aeson.Value) -> roundtrip x

prop "Lazy Text"
$ \(x :: TextL.Text) -> roundtrip x

Expand Down
Loading