on master help sign in
Make SerializeConfig opaque
commitc0d3178fa4aa4da7788c30861a40937df8aa900e authorHarendra Kumar <harendra@composewell.com> dateTue 28 Nov 2023, 02:19 (3 years ago) parenta79caed
7 files changed+46−39
benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+1−1 core/src/Streamly/Data/MutByteArray.hs+2−2 core/src/Streamly/Internal/Data/Serialize/TH.hs+19−12 core/src/Streamly/Internal/Data/Serialize/TH/Bottom.hs+18−18 test/Streamly/Test/Data/Serialize.hs+2−2 test/Streamly/Test/Data/Serialize/CompatV0.hs+2−2 test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2
benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+1−1
@@ -20,5 +20,5 @@ import qualified Streamly.Internal.Data.MutByteArray as Serialize
2020
2121$(genLargeRecord "RecCompatible" 50)
2222$(Serialize.deriveSerializeWith
23− (Serialize.encodeRecordFields True Serialize.serializeConfig)
23+ (Serialize.encodeRecordFields True)
2424 [d|instance Serialize RecCompatible|])
core/src/Streamly/Data/MutByteArray.hs+2−2
@@ -72,13 +72,13 @@ module Streamly.Data.MutByteArray
7272 -- * Serialize
7373 , Serialize(..)
7474
75− -- Deriving Serialize
75+ -- ** Instance Config
7676 , SerializeConfig
77− , serializeConfig
7877 , inlineAddSizeTo
7978 , inlineSerializeAt
8079 , inlineDeserializeAt
8180
81+ -- ** Instance Deriving
8282 , deriveSerialize
8383 , deriveSerializeWith
8484 ) where
core/src/Streamly/Internal/Data/Serialize/TH.hs+19−12
@@ -446,29 +446,36 @@ deriveSerializeInternal conf headTy cons next = do
446446 let methods = concat [sizeDec, peekDec, pokeDec]
447447 next methods
448448
449−-- | @deriveSerializeWith config instance-dec@ generates a template Haskell
450−-- splice consisting of a declaration of a 'Serialize' instance. @instance-dec@
451−-- is a template Haskell declaration splice consisting of a standard Haskell
452−-- instance declaration without the type class methods (e.g. @[d|instance
453−-- Serialize a => Serialize (Maybe a)|]@). The type class methods for the given
454−-- instance are generated according to the supplied @config@ parameter.
449+-- | @deriveSerializeWith config-modifier instance-dec@ generates a template
450+-- Haskell splice consisting of a declaration of a 'Serialize' instance.
451+-- @instance-dec@ is a template Haskell declaration splice consisting of a
452+-- standard Haskell instance declaration without the type class methods (e.g.
453+-- @[d|instance Serialize a => Serialize (Maybe a)|]@).
454+--
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.
455458--
456459-- Usage:
457460--
458461-- @
459462-- \$(deriveSerializeWith
460−-- serializeConfig
463+-- ( inlineSerializeAt (Just NoInline)
464+-- . inlineDeserializeAt (Just NoInline)
465+-- )
461466-- [d|instance Serialize a => Serialize (Maybe a)|])
462467-- @
463−deriveSerializeWith :: SerializeConfig -> Q [Dec] -> Q [Dec]
464−deriveSerializeWith conf mDecs = do
468+deriveSerializeWith ::
469+ (SerializeConfig -> SerializeConfig) -> Q [Dec] -> Q [Dec]
470+deriveSerializeWith modifier mDecs = do
465471 dec <- mDecs
466472 case dec of
467473 [InstanceD mo preds headTyWC []] -> do
468474 let headTy = unwrap dec headTyWC
469475 dt <- reifyDataType (getMainTypeName dec headTy)
470476 let cons = dtCons dt
471− deriveSerializeInternal conf headTy cons (next mo preds headTyWC)
477+ deriveSerializeInternal
478+ (modifier serializeConfig) headTy cons (next mo preds headTyWC)
472479 _ -> errorMessage dec
473480
474481 where
@@ -505,7 +512,7 @@ deriveSerializeWith conf mDecs = do
505512-- @[d|instance Serialize a => Serialize (Maybe a)|]@), generate an instance
506513-- declaration including all the type class method implementations.
507514--
508−-- >>> deriveSerialize = deriveSerializeWith serializeConfig
515+-- >>> deriveSerialize = deriveSerializeWith id
509516--
510517-- Usage:
511518--
@@ -514,4 +521,4 @@ deriveSerializeWith conf mDecs = do
514521-- [d|instance Serialize a => Serialize (Maybe a)|])
515522-- @
516523deriveSerialize :: 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(..))
8080-- no inline specific pragma let's GHC decide whether to put the code in
8181-- interface file or not.
8282
83−-- | Configuration to control how the 'Serialize' instance is generated. Use
84−-- 'serializeConfig' and config setter functions to generate desired Config. For
85−-- example:
83+-- | Configuration to control how the 'Serialize' instance is generated. The
84+-- configuration is opaque and is modified by composing config modifier
85+-- functions, for example:
8686--
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)
8894--
8995data SerializeConfig =
9096 SerializeConfig
@@ -95,19 +101,19 @@ data SerializeConfig =
95101 , cfgRecordSyntaxWithHeader :: Bool
96102 }
97103
98−-- | How should we inline the 'addSizeTo' function? The default in 'serializeConfig'
99−-- is 'Nothing' which means left to the compiler. Forcing inline on @addSizeTo@
100−-- function actually worsens some benchmarks and improves none.
104+-- | How should we inline the 'addSizeTo' function? The default is 'Nothing'
105+-- which means left to the compiler. Forcing inline on @addSizeTo@ function
106+-- actually worsens some benchmarks and improves none.
101107inlineAddSizeTo :: Maybe Inline -> SerializeConfig -> SerializeConfig
102108inlineAddSizeTo v cfg = cfg {cfgInlineSize = v}
103109
104110-- XXX Should we make the default Inlinable instead?
105111
106−-- | How should we inline the 'serialize' function? The default in
107−-- 'serializeConfig' is 'Just Inline'. However, aggressive inlining can bloat
108−-- the code and increase in compilation times when there are big functions and
109−-- too many nesting levels so you can change it accordingly. A 'Nothing' value
110−-- leaves the decision to the compiler.
112+-- | How should we inline the 'serialize' function? The default 'Just Inline'.
113+-- However, aggressive inlining can bloat the code and increase in compilation
114+-- times when there are big functions and too many nesting levels so you can
115+-- change it accordingly. A 'Nothing' value leaves the decision to the
116+-- compiler.
111117inlineSerializeAt :: Maybe Inline -> SerializeConfig -> SerializeConfig
112118inlineSerializeAt v cfg = cfg {cfgInlineSerialize = v}
113119
@@ -182,12 +188,6 @@ encodeConstrNames v cfg = cfg {cfgConstructorTagAsString = v}
182188encodeRecordFields :: Bool -> SerializeConfig -> SerializeConfig
183189encodeRecordFields v cfg = cfg {cfgRecordSyntaxWithHeader = v}
184190
185−-- | The default configuration settings are:
186−--
187−-- * 'inlineAddSizeTo' 'Nothing'
188−-- * 'inlineSerializeAt' (Just Inline)
189−-- * 'inlineDeserializeAt' (Just Inline)
190−--
191191serializeConfig :: SerializeConfig
192192serializeConfig =
193193 SerializeConfig
test/Streamly/Test/Data/Serialize.hs+2−2
@@ -45,10 +45,10 @@ import Test.Hspec as H
4545
4646#ifdef ENABLE_constructorTagAsString
4747#define CONF_NAME "ENABLE_constructorTagAsString"
48−#define CONF (Serialize.encodeConstrNames True Serialize.serializeConfig)
48+#define CONF (Serialize.encodeConstrNames True)
4949#else
5050#define CONF_NAME "DEFAULT"
51−#define CONF Serialize.serializeConfig
51+#define CONF id
5252#endif
5353
5454--------------------------------------------------------------------------------
test/Streamly/Test/Data/Serialize/CompatV0.hs+2−2
@@ -29,7 +29,7 @@ instance Arbitrary a => Arbitrary (Rec a) where
2929 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary
3030
3131$(Serialize.deriveSerializeWith
32− (Serialize.encodeRecordFields True Serialize.serializeConfig)
32+ (Serialize.encodeRecordFields True)
3333 [d|instance Serialize a => Serialize (Rec a)|])
3434
3535data River
@@ -42,5 +42,5 @@ instance Arbitrary River where
4242 arbitrary = elements [Ganga, Yamuna, Godavari]
4343
4444$(Serialize.deriveSerializeWith
45− (Serialize.encodeConstrNames True Serialize.serializeConfig)
45+ (Serialize.encodeConstrNames True)
4646 [d|instance Serialize River|])
test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2
@@ -30,7 +30,7 @@ instance Arbitrary a => Arbitrary (Rec a) where
3030 arbitrary = Rec <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
3131
3232$(Serialize.deriveSerializeWith
33− (Serialize.encodeRecordFields True Serialize.serializeConfig)
33+ (Serialize.encodeRecordFields True)
3434 [d|instance Serialize a => Serialize (Rec a)|])
3535
3636data River
@@ -44,5 +44,5 @@ instance Arbitrary River where
4444 arbitrary = elements [Ganga, Yamuna, Godavari]
4545
4646$(Serialize.deriveSerializeWith
47− (Serialize.encodeConstrNames True Serialize.serializeConfig)
47+ (Serialize.encodeConstrNames True)
4848 [d|instance Serialize River|])