on master help sign in
Add ability to not use any INLINE pragma on size

Leave it to the compiler.

commit626e35565b6ceb136459227d86bad71180565d49 authorHarendra Kumar <harendra@composewell.com> dateTue 21 Nov 2023, 23:51 (3 years ago) parente7f6e7e
2 files changed+55−38
core/src/Streamly/Internal/Data/Serialize/TH.hs+30−17 core/src/Streamly/Internal/Data/Serialize/TH/Bottom.hs+25−21
core/src/Streamly/Internal/Data/Serialize/TH.hs+30−17
@@ -169,9 +169,12 @@ mkSizeDec (Config {..}) headTy cons = do
169169 cfgRecordSyntaxWithHeader
170170 (typeOfType headTy cons)
171171 pure
172− [ PragmaD (InlineP 'size cfgInlineSize FunLike AllPhases)
173− , FunD 'size [Clause [] (NormalB sizeOfMethod) []]
174− ]
172+ ( maybe
173+ []
174+ (\x -> [PragmaD (InlineP 'size x FunLike AllPhases)])
175+ cfgInlineSize
176+ ++ [FunD 'size [Clause [] (NormalB sizeOfMethod) []]]
177+ )
175178
176179--------------------------------------------------------------------------------
177180-- Peek
@@ -275,8 +278,12 @@ mkDeserializeDec (Config {..}) headTy cons = do
275278 headTy
276279 (typeOfType headTy cons)
277280 pure
278− [ PragmaD (InlineP 'deserialize cfgInlineDeserialize FunLike AllPhases)
279− , FunD
281+ ( maybe
282+ []
283+ (\x -> [PragmaD (InlineP 'deserialize x FunLike AllPhases)])
284+ cfgInlineDeserialize
285+ ++
286+ [ FunD
280287 'deserialize
281288 [ Clause
282289 (if isUnitType cons && not cfgConstructorTagAsString
@@ -285,7 +292,8 @@ mkDeserializeDec (Config {..}) headTy cons = do
285292 (NormalB peekMethod)
286293 []
287294 ]
288− ]
295+ ]
296+ )
289297
290298--------------------------------------------------------------------------------
291299-- Poke
@@ -379,17 +387,22 @@ mkSerializeDec (Config {..}) headTy cons = do
379387 cfgRecordSyntaxWithHeader
380388 (typeOfType headTy cons)
381389 pure
382− [ PragmaD (InlineP 'serialize cfgInlineSerialize FunLike AllPhases)
383− , FunD
384− 'serialize
385− [ Clause
386− (if isUnitType cons && not cfgConstructorTagAsString
387− then [VarP _initialOffset, WildP, WildP]
388− else [VarP _initialOffset, VarP _arr, VarP _val])
389− (NormalB pokeMethod)
390− []
391− ]
392− ]
390+ ( maybe
391+ []
392+ (\x -> [PragmaD (InlineP 'serialize x FunLike AllPhases)])
393+ cfgInlineSerialize
394+ ++
395+ [FunD
396+ 'serialize
397+ [ Clause
398+ (if isUnitType cons && not cfgConstructorTagAsString
399+ then [VarP _initialOffset, WildP, WildP]
400+ else [VarP _initialOffset, VarP _arr, VarP _val])
401+ (NormalB pokeMethod)
402+ []
403+ ]
404+ ]
405+ )
393406
394407--------------------------------------------------------------------------------
395408-- Main
core/src/Streamly/Internal/Data/Serialize/TH/Bottom.hs+25−21
@@ -74,38 +74,42 @@ import Streamly.Internal.Data.Unbox.TH (DataCon(..))
7474-- Config
7575--------------------------------------------------------------------------------
7676
77−-- XXX Need to make it (Maybe Inline) so that we can use Nothing as default.
78−
7977-- | Configuration to control how the 'Serialize' instance is generated. Use
8078-- 'defaultConfig' and config setter functions to generate desired Config. For
8179-- example:
8280--
83−-- >>> (inlineSize Inline) . (inlineSerialize Inlinable) defaultConfig
81+-- >>> (inlineSize (Just Inline)) . (inlineSerialize (Just Inlinable)) defaultConfig
8482--
8583data Config =
8684 Config
87− { cfgInlineSize :: Inline
88− , cfgInlineSerialize :: Inline
89− , cfgInlineDeserialize :: Inline
85+ { cfgInlineSize :: Maybe Inline
86+ , cfgInlineSerialize :: Maybe Inline
87+ , cfgInlineDeserialize :: Maybe Inline
9088 , cfgConstructorTagAsString :: Bool
9189 , cfgRecordSyntaxWithHeader :: Bool
9290 }
9391
9492-- | How should we inline the 'size' function? The default in 'defaultConfig'
95−-- is 'Inline'. However, aggressive inlining can bloat the code and increase
96−-- compilation times in when there are big functions and too many nesting
97−-- levels so you can change it accordingly.
98−inlineSize :: Inline -> Config -> Config
93+-- is 'Nothing' which means left to the compiler. Forcing inline on @size@
94+-- function actually worsens some benchmarks and improves none.
95+inlineSize :: Maybe Inline -> Config -> Config
9996inlineSize v cfg = cfg {cfgInlineSize = v}
10097
101−-- | How should we inline the 'serialize' function? See guidelines in
102−-- 'inlineSize'.
103−inlineSerialize :: Inline -> Config -> Config
98+-- XXX Should we make the default Inlinable instead?
99+
100+-- | How should we inline the 'serialize' function? The default in
101+-- 'defaultConfig' is 'Just Inline'. However, aggressive inlining can bloat the
102+-- code and increase in compilation times when there are big functions and too
103+-- many nesting levels so you can change it accordingly. A 'Nothing' value
104+-- leaves the decision to the compiler.
105+inlineSerialize :: Maybe Inline -> Config -> Config
104106inlineSerialize v cfg = cfg {cfgInlineSerialize = v}
105107
108+-- XXX Should we make the default Inlinable instead?
109+
106110-- | How should we inline the 'deserialize' function? See guidelines in
107−-- 'inlineSize'.
108−inlineDeserialize :: Inline -> Config -> Config
111+-- 'inlineSerialize'.
112+inlineDeserialize :: Maybe Inline -> Config -> Config
109113inlineDeserialize v cfg = cfg {cfgInlineDeserialize = v}
110114
111115-- | __Experimental__
@@ -174,16 +178,16 @@ encodeRecordFields v cfg = cfg {cfgRecordSyntaxWithHeader = v}
174178
175179-- | The default configuration settings are:
176180--
177−-- * 'inlineSize' 'Inline'
178−-- * 'inlineSerialize' 'Inline'
179−-- * 'inlineDeserialize' 'Inline'
181+-- * 'inlineSize' 'Nothing'
182+-- * 'inlineSerialize' 'Just Inline'
183+-- * 'inlineDeserialize' 'Just Inline'
180184--
181185defaultConfig :: Config
182186defaultConfig =
183187 Config
184− { cfgInlineSize = Inline
185− , cfgInlineSerialize = Inline
186− , cfgInlineDeserialize = Inline
188+ { cfgInlineSize = Nothing
189+ , cfgInlineSerialize = Just Inline
190+ , cfgInlineDeserialize = Just Inline
187191 , cfgConstructorTagAsString = False
188192 , cfgRecordSyntaxWithHeader = False
189193 }