Compute literal length at compile time
SDoc string literals created for example with text "xyz"
are converted
into PtrString
(Addr#
+ size in bytes) with a rewrite rule to avoid
allocating a String.
Currently the size in bytes is still computed at runtime. For every literal, we obtained the following pseudo STG:
x :: Addr#
x = "xzy"#
s :: PtrString
s = \u [] case ffi:strlen [x realWorld#] of
(# _, sz #) -> PtrString [x sz]
Since GHC 9.0, we can use cstringLength#
instead to get:
x :: Addr#
x = "xzy"#
s :: PtrString
s = PtrString! [x 3#]