Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,347
    • Issues 5,347
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 574
    • Merge requests 574
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #14364
Closed
Open
Issue created Oct 17, 2017 by Ben Gamari@bgamari🐢Maintainer

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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking