Optimisation of unpackCString#

Neil Mitchell ndmitchell at gmail.com
Mon Apr 28 16:30:08 EDT 2008


(All these results are from GHC 6.9.20071226, but suspect they hold
for 6.9.* and 6.8)

The following code:

test = head "neil"

Produces (with -O2):

Text.HTML.TagSoup.Development.Sample.test =
  case GHC.Base.unpackCString# "neil" of wild_aaU {
    [] -> GHC.List.badHead @ GHC.Base.Char; : x_aaY ds1_aaZ -> x_aaY


test = head ['n','e','i','l']


Text.HTML.TagSoup.Development.Sample.test = GHC.Base.C# 'n'

The packing of the string has damaged the optimisation. Is there
anyway to mitigate this? I ideally want to do this in RULES to derive
an efficient parser from a set of parser combinators, and the
unpackCString# really gets in the way of future optimisations.

I could imagine adding two rules to the simplifier:

case unpackCString# "" of  ==> case [] of
case unpackCString# "xyz" of ==> case (C# 'x': unpackCString# "yz") of

Then the simplifier could recover the optimised version.



More information about the Glasgow-haskell-users mailing list