Missed optimization opportunity
I have the following function in text-2.0
:
unpackCStringAscii# :: Addr# -> Text
unpackCStringAscii# addr# = Text {- buffer -} ba {- offset -} 0 {- length -} l
where
l = I# (GHC.CString.cstringLength# addr#)
ba = runST $ do
marr <- A.new l
A.copyFromPointer marr 0 (Ptr addr#) l
A.unsafeFreeze marr
{-# INLINE unpackCStringAscii# #-}
For a static Addr#
both offset and length (!) are known in compile time, and I would expect functions, which pattern-match on them such as null (Text _ _ l) = l <= 0
, to be evaluated statically. However, this does not happen. What I see in Core is
lvl1_r21qs :: Addr#
lvl1_r21qs = "POST"#
s2_r21qt :: Text
s2_r21qt
= runRW#
(\ (s_X2p :: State# RealWorld) ->
case newByteArray# 4# s_X2p of { (# ipv_amFi, ipv1_amFj #) ->
case copyAddrToByteArray# lvl1_r21qs ipv1_amFj 0# 4# ipv_amFi
of s2#_amFl
{ __DEFAULT ->
case unsafeFreezeByteArray# ipv1_amFj s2#_amFl of
{ (# ipv2_amFn, ipv3_amFo #) ->
Text ipv3_amFo 0# 4#
}
}
})
-- RHS size: {terms: 7, types: 5, coercions: 0, joins: 0/0}
lvl13_r21qP :: Bool
lvl13_r21qP
= case s2_r21qt of { Text dt_ac3z dt1_ac3A dt2_ac3B ->
tagToEnum# (<=# dt2_ac3B 0#)
}
But I would expect lvl13_r21qP
to be statically known False
instead.
Why does GHC sink Text ba# 0# 4#
in the depth of runRW#
, making it unavailable for pattern matching?