Skip to content

Ppr: compute length of string literals at compile time (#19266)

Sylvain Henry requested to merge hsyl20/ghc:hsyl20/perf/strlen into master

Commit message:

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.

Before this patch, the size in bytes was 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]

But since GHC 9.0, we can use `cstringLength#` instead to get:

    x :: Addr#
    x = "xzy"#

    s :: PtrString
    s = PtrString! [x 3#]

Literals become statically known constructor applications. Allocations
seem to decrease a little in perf tests (between -0.1% and -0.7% on CI).
Edited by Sylvain Henry

Merge request reports