Ticket #5218 (new feature request)
Add unpackCStringLen# to create Strings from string literals
|Reported by:||tibbe||Owned by:||igloo|
|Keywords:||Cc:||johan.tibell@…, dons, dcoutts, pho@…, lykahb@…, reiner.pope@…, alexey.skladnoy@…, wren@…, patrick@…, hackage.haskell.org@…|
|Type of failure:||None/Unknown||Difficulty:||Unknown|
|Test Case:||Blocked By:|
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.