Reduce repetition in derived Read instances
While looking at #7258 with tdammers, I noticed that 5000 of the 7000 terms that the W2
module simplifies to are attributed to $creadPrec
. In fact, much of this is repetition of the form,
(GHC.Read.expectP
(Text.Read.Lex.Ident (GHC.CString.unpackCString# "field1"#)))
(>>
@ Text.ParserCombinators.ReadPrec.ReadPrec
Text.ParserCombinators.ReadPrec.$fMonadReadPrec
@ ()
@ DT
(GHC.Read.expectP
(Text.Read.Lex.Punc (GHC.CString.unpackCString# "="#)))
(>>=
@ Text.ParserCombinators.ReadPrec.ReadPrec
Text.ParserCombinators.ReadPrec.$fMonadReadPrec
@ Int
@ DT
(Text.ParserCombinators.ReadPrec.reset
@ Int (GHC.Read.readPrec @ Int GHC.Read.$fReadInt))
(\ (a1_a1oe :: Int) ->
>>
@ Text.ParserCombinators.ReadPrec.ReadPrec
Text.ParserCombinators.ReadPrec.$fMonadReadPrec
@ ()
@ DT
(GHC.Read.expectP
(Text.Read.Lex.Punc (GHC.CString.unpackCString# ","#)))
Let's factor this pattern out into a readField
helper in GHC.Read
,
readField :: String -> ReadPrec a -> ReadPrec a
readField fieldName readVal = do
expectP (Ident fieldName)
expectP (Punc "=")
readVal
{-# NOINLINE readField #-}
This will at least knock off a constant factor from the size of what should not be performance-critical code.
We could also try folding the terminal "," into this, although then we would need to somehow handle the last field specially. This might not be worth it. Perhaps instead just factor out the comma case as well,
readComma :: ReadPrec ()
readComma = expectP (Punc ",")
{-# NOINLINE readField #-}
It's unclear whether this is worth it, however.
Trac metadata
Trac field | Value |
---|---|
Version | 8.2.1 |
Type | Task |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | tdammers |
Operating system | |
Architecture |