[GHC] #9163: Ptr should have a phantom role

GHC ghc-devs at haskell.org
Tue Jun 3 13:36:19 UTC 2014


#9163: Ptr should have a phantom role
------------------------------------+-------------------------------------
       Reporter:  simonpj           |             Owner:
           Type:  bug               |            Status:  new
       Priority:  normal            |         Milestone:
      Component:  Compiler          |           Version:  7.8.2
       Keywords:                    |  Operating System:  Unknown/Multiple
   Architecture:  Unknown/Multiple  |   Type of failure:  None/Unknown
     Difficulty:  Unknown           |         Test Case:
     Blocked By:                    |          Blocking:
Related Tickets:                    |
------------------------------------+-------------------------------------
 In `GHC.Ptr` we see
 {{{
 type role Ptr representational
 data Ptr a = Ptr Addr# deriving (Eq, Ord)
 }}}
 with no comments.  Why is `Ptr` representational?

 In the same module we have `castPtr`:
 {{{
 castPtr :: Ptr a -> Ptr b
 castPtr (Ptr addr) = Ptr addr
 }}}
 which unpacks and repacks a `Ptr`.  If `Ptr` was phantom, we could use
 `coerce`.  And that in turn would actually make a lot of code more
 efficient – there are lots of calls to `castPtr`. Specifically, in
 `nofib`, I tried implementing `castPtr` with `unsafeCoerce`.  Then I
 found:
  * 12% less allocation in `reverse-complem`
  * 7.3% less allocation in `fasta`.
  * Binary sizes fell 0.1%.
 Both these benchmarks are ones that do a ''lot'' of I/O, and it turns out
 that `GHC.IO.Handle.Text.$wa1` has a `castPtr` that (all by itself)
 accounts for 12% of `reverse-complem`'s total allocation!    So making
 `castPtr` free is good.

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


More information about the ghc-tickets mailing list