Skip to content

Add unpackCStringLen# to create Strings from string literals

GHC insert calls to unpackCString# to convert string literals to Strings. Libraries like bytestring use rewrite rules to match on this call to optimize code like pack (unpackCString# s).

If GHC would instead use a version of unpackCString#, say unpackCStringLen#, that includes the (statically known) length, creating ByteStrings from literals could be a constant time operation instead of a linear time operation.

Another use case, which motivated this ticket, is appending string literals to builders (e.g. using Data.Binary.Builder.fromByteString). For small strings the most efficient way to append a string to the builder is to copy the statically allocated string directly into the builder's output buffer. If the string length was known statically, we could do this efficiently using memcpy or even using a small unrolled loop.

Trac metadata
Trac field Value
Version 7.0.3
Type FeatureRequest
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