on master help sign in
Make SerializeConfig opaque
c0d3178Harendra Kumar
benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+1−1
@@ -20,5 +20,5 @@ import qualified Streamly.Internal.Data.MutByteArray as Serialize
20
20
21$(genLargeRecord "RecCompatible" 50)
21$(genLargeRecord "RecCompatible" 50)
22$(Serialize.deriveSerializeWith
22$(Serialize.deriveSerializeWith
23− (Serialize.encodeRecordFields True Serialize.serializeConfig)
23+ (Serialize.encodeRecordFields True)
24 [d|instance Serialize RecCompatible|])
24 [d|instance Serialize RecCompatible|])
core/src/Streamly/Data/MutByteArray.hs+2−2
@@ -72,13 +72,13 @@ module Streamly.Data.MutByteArray
72 -- * Serialize
72 -- * Serialize
73 , Serialize(..)
73 , Serialize(..)
74
74
75− -- Deriving Serialize
75+ -- ** Instance Config
76 , SerializeConfig
76 , SerializeConfig
77− , serializeConfig
78 , inlineAddSizeTo
77 , inlineAddSizeTo
79 , inlineSerializeAt
78 , inlineSerializeAt
80 , inlineDeserializeAt
79 , inlineDeserializeAt
81
80
81+ -- ** Instance Deriving
82 , deriveSerialize
82 , deriveSerialize
83 , deriveSerializeWith
83 , deriveSerializeWith
84 ) where
84 ) where
core/src/Streamly/Internal/Data/Serialize/TH.hs+19−12
@@ -446,29 +446,36 @@ deriveSerializeInternal conf headTy cons next = do
446 let methods = concat [sizeDec, peekDec, pokeDec]
446 let methods = concat [sizeDec, peekDec, pokeDec]
447 next methods
447 next methods
448
448
449−-- | @deriveSerializeWith config instance-dec@ generates a template Haskell
449+-- | @deriveSerializeWith config-modifier instance-dec@ generates a template
450−-- splice consisting of a declaration of a 'Serialize' instance. @instance-dec@
450+-- Haskell splice consisting of a declaration of a 'Serialize' instance.
451−-- is a template Haskell declaration splice consisting of a standard Haskell
451+-- @instance-dec@ is a template Haskell declaration splice consisting of a
452−-- instance declaration without the type class methods (e.g. @[d|instance
452+-- standard Haskell instance declaration without the type class methods (e.g.
453−-- Serialize a => Serialize (Maybe a)|]@). The type class methods for the given
453+-- @[d|instance Serialize a => Serialize (Maybe a)|]@).
454+--
454−-- instance are generated according to the supplied @config@ parameter.
455+-- The type class methods for the given instance are generated according to the
456+-- supplied @config-modifier@ parameter. See 'SerializeConfig' for default
457+-- configuration settings.
455--
458--
456-- Usage:
459-- Usage:
457--
460--
458-- @
461-- @
459-- \$(deriveSerializeWith
462-- \$(deriveSerializeWith
460−-- serializeConfig
463+-- ( inlineSerializeAt (Just NoInline)
464+-- . inlineDeserializeAt (Just NoInline)
465+-- )
461-- [d|instance Serialize a => Serialize (Maybe a)|])
466-- [d|instance Serialize a => Serialize (Maybe a)|])
462-- @
467-- @
463−deriveSerializeWith :: SerializeConfig -> Q [Dec] -> Q [Dec]
468+deriveSerializeWith ::
469+ (SerializeConfig -> SerializeConfig) -> Q [Dec] -> Q [Dec]
464−deriveSerializeWith conf mDecs = do
470+deriveSerializeWith modifier mDecs = do
465 dec <- mDecs
471 dec <- mDecs
466 case dec of
472 case dec of
467 [InstanceD mo preds headTyWC []] -> do
473 [InstanceD mo preds headTyWC []] -> do
468 let headTy = unwrap dec headTyWC
474 let headTy = unwrap dec headTyWC
469 dt <- reifyDataType (getMainTypeName dec headTy)
475 dt <- reifyDataType (getMainTypeName dec headTy)
470 let cons = dtCons dt
476 let cons = dtCons dt
471− deriveSerializeInternal conf headTy cons (next mo preds headTyWC)
477+ deriveSerializeInternal
478+ (modifier serializeConfig) headTy cons (next mo preds headTyWC)
472 _ -> errorMessage dec
479 _ -> errorMessage dec
473
480
474 where
481 where
@@ -505,7 +512,7 @@ deriveSerializeWith conf mDecs = do
505-- @[d|instance Serialize a => Serialize (Maybe a)|]@), generate an instance
512-- @[d|instance Serialize a => Serialize (Maybe a)|]@), generate an instance
506-- declaration including all the type class method implementations.
513-- declaration including all the type class method implementations.
507--
514--
508−-- >>> deriveSerialize = deriveSerializeWith serializeConfig
515+-- >>> deriveSerialize = deriveSerializeWith id
509--
516--
510-- Usage:
517-- Usage:
511--
518--
@@ -514,4 +521,4 @@ deriveSerializeWith conf mDecs = do
514-- [d|instance Serialize a => Serialize (Maybe a)|])
521-- [d|instance Serialize a => Serialize (Maybe a)|])
515-- @
522-- @
516deriveSerialize :: Q [Dec] -> Q [Dec]
523deriveSerialize :: Q [Dec] -> Q [Dec]
517−deriveSerialize = deriveSerializeWith serializeConfig
524+deriveSerialize = deriveSerializeWith id
core/src/Streamly/Internal/Data/Serialize/TH/Bottom.hs+18−18
@@ -80,11 +80,17 @@ import Streamly.Internal.Data.Unbox.TH (DataCon(..))
80-- no inline specific pragma let's GHC decide whether to put the code in
80-- no inline specific pragma let's GHC decide whether to put the code in
81-- interface file or not.
81-- interface file or not.
82
82
83−-- | Configuration to control how the 'Serialize' instance is generated. Use
83+-- | Configuration to control how the 'Serialize' instance is generated. The
84−-- 'serializeConfig' and config setter functions to generate desired Config. For
84+-- configuration is opaque and is modified by composing config modifier
85−-- example:
85+-- functions, for example:
86--
86--
87−-- >>> (inlineAddSizeTo (Just Inline)) . (inlineSerializeAt (Just Inlinable)) serializeConfig
87+-- >>> (inlineSerializeAt (Just NoInline)) . (inlineSerializeAt (Just Inlinable))
88+--
89+-- The default configuration settings are:
90+--
91+-- * 'inlineAddSizeTo' Nothing
92+-- * 'inlineSerializeAt' (Just Inline)
93+-- * 'inlineDeserializeAt' (Just Inline)
88--
94--
89data SerializeConfig =
95data SerializeConfig =
90 SerializeConfig
96 SerializeConfig
@@ -95,19 +101,19 @@ data SerializeConfig =
95 , cfgRecordSyntaxWithHeader :: Bool
101 , cfgRecordSyntaxWithHeader :: Bool
96 }
102 }
97
103
98−-- | How should we inline the 'addSizeTo' function? The default in 'serializeConfig'
104+-- | How should we inline the 'addSizeTo' function? The default is 'Nothing'
99−-- is 'Nothing' which means left to the compiler. Forcing inline on @addSizeTo@
105+-- which means left to the compiler. Forcing inline on @addSizeTo@ function
100−-- function actually worsens some benchmarks and improves none.
106+-- actually worsens some benchmarks and improves none.
101inlineAddSizeTo :: Maybe Inline -> SerializeConfig -> SerializeConfig
107inlineAddSizeTo :: Maybe Inline -> SerializeConfig -> SerializeConfig
102inlineAddSizeTo v cfg = cfg {cfgInlineSize = v}
108inlineAddSizeTo v cfg = cfg {cfgInlineSize = v}
103
109
104-- XXX Should we make the default Inlinable instead?
110-- XXX Should we make the default Inlinable instead?
105
111
106−-- | How should we inline the 'serialize' function? The default in
112+-- | How should we inline the 'serialize' function? The default 'Just Inline'.
107−-- 'serializeConfig' is 'Just Inline'. However, aggressive inlining can bloat
113+-- However, aggressive inlining can bloat the code and increase in compilation
108−-- the code and increase in compilation times when there are big functions and
114+-- times when there are big functions and too many nesting levels so you can
109−-- too many nesting levels so you can change it accordingly. A 'Nothing' value
115+-- change it accordingly. A 'Nothing' value leaves the decision to the
110−-- leaves the decision to the compiler.
116+-- compiler.
111inlineSerializeAt :: Maybe Inline -> SerializeConfig -> SerializeConfig
117inlineSerializeAt :: Maybe Inline -> SerializeConfig -> SerializeConfig
112inlineSerializeAt v cfg = cfg {cfgInlineSerialize = v}
118inlineSerializeAt v cfg = cfg {cfgInlineSerialize = v}
113
119
@@ -182,12 +188,6 @@ encodeConstrNames v cfg = cfg {cfgConstructorTagAsString = v}
182encodeRecordFields :: Bool -> SerializeConfig -> SerializeConfig
188encodeRecordFields :: Bool -> SerializeConfig -> SerializeConfig
183encodeRecordFields v cfg = cfg {cfgRecordSyntaxWithHeader = v}
189encodeRecordFields v cfg = cfg {cfgRecordSyntaxWithHeader = v}
184
190
185−-- | The default configuration settings are:
186−--
187−-- * 'inlineAddSizeTo' 'Nothing'
188−-- * 'inlineSerializeAt' (Just Inline)
189−-- * 'inlineDeserializeAt' (Just Inline)
190−--
191serializeConfig :: SerializeConfig
191serializeConfig :: SerializeConfig
192serializeConfig =
192serializeConfig =
193 SerializeConfig
193 SerializeConfig
test/Streamly/Test/Data/Serialize.hs+2−2
@@ -45,10 +45,10 @@ import Test.Hspec as H
45
45
46#ifdef ENABLE_constructorTagAsString
46#ifdef ENABLE_constructorTagAsString
47#define CONF_NAME "ENABLE_constructorTagAsString"
47#define CONF_NAME "ENABLE_constructorTagAsString"
48−#define CONF (Serialize.encodeConstrNames True Serialize.serializeConfig)
48+#define CONF (Serialize.encodeConstrNames True)
49#else
49#else
50#define CONF_NAME "DEFAULT"
50#define CONF_NAME "DEFAULT"
51−#define CONF Serialize.serializeConfig
51+#define CONF id
52#endif
52#endif
53
53
54--------------------------------------------------------------------------------
54--------------------------------------------------------------------------------
test/Streamly/Test/Data/Serialize/CompatV0.hs+2−2
@@ -29,7 +29,7 @@ instance Arbitrary a => Arbitrary (Rec a) where
29 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary
29 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary
30
30
31$(Serialize.deriveSerializeWith
31$(Serialize.deriveSerializeWith
32− (Serialize.encodeRecordFields True Serialize.serializeConfig)
32+ (Serialize.encodeRecordFields True)
33 [d|instance Serialize a => Serialize (Rec a)|])
33 [d|instance Serialize a => Serialize (Rec a)|])
34
34
35data River
35data River
@@ -42,5 +42,5 @@ instance Arbitrary River where
42 arbitrary = elements [Ganga, Yamuna, Godavari]
42 arbitrary = elements [Ganga, Yamuna, Godavari]
43
43
44$(Serialize.deriveSerializeWith
44$(Serialize.deriveSerializeWith
45− (Serialize.encodeConstrNames True Serialize.serializeConfig)
45+ (Serialize.encodeConstrNames True)
46 [d|instance Serialize River|])
46 [d|instance Serialize River|])
test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2
@@ -30,7 +30,7 @@ instance Arbitrary a => Arbitrary (Rec a) where
30 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
30 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
31
31
32$(Serialize.deriveSerializeWith
32$(Serialize.deriveSerializeWith
33− (Serialize.encodeRecordFields True Serialize.serializeConfig)
33+ (Serialize.encodeRecordFields True)
34 [d|instance Serialize a => Serialize (Rec a)|])
34 [d|instance Serialize a => Serialize (Rec a)|])
35
35
36data River
36data River
@@ -44,5 +44,5 @@ instance Arbitrary River where
44 arbitrary = elements [Ganga, Yamuna, Godavari]
44 arbitrary = elements [Ganga, Yamuna, Godavari]
45
45
46$(Serialize.deriveSerializeWith
46$(Serialize.deriveSerializeWith
47− (Serialize.encodeConstrNames True Serialize.serializeConfig)
47+ (Serialize.encodeConstrNames True)
48 [d|instance Serialize River|])
48 [d|instance Serialize River|])