Skip to content

Ptr should have a phantom role

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.

Trac metadata
Trac field Value
Version 7.8.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information