[GHC] #14364: Reduce repetition in derived Read instances

GHC ghc-devs at haskell.org
Tue Oct 17 16:50:33 UTC 2017


#14364: Reduce repetition in derived Read instances
-------------------------------------+-------------------------------------
           Reporter:  bgamari        |             Owner:  (none)
               Type:  task           |            Status:  new
           Priority:  normal         |         Milestone:  8.4.1
          Component:  Compiler       |           Version:  8.2.1
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:  #10980 #7258
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 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,
 {{{#!hs
                   (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`,
 {{{#!hs
 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,
 {{{#!hs
 readComma :: ReadPrec ()
 readComma = expectP (Punc ",")
 {-# NOINLINE readField #-}
 }}}

 It's unclear whether this is worth it, however.

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14364>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list