Skip to content

peekCAText is less efficient than it could be #79

@chessai

Description

@chessai

currently it's just defined as

peekCAText cp = Text.pack <$> peekCAString cp

so, it not only calls the more inneficient peekCAString from base, but then it must convert the String into text! This seems unnecessarily expensive. There is an alternative in something similar to Data.Text.Foreign.peekCStringLen, which goes from CStringLen -> IO Text. There is a drawback to this, in that it only supports CStrings that are valid UTF-8, and throws an exception otherwise. Another drawback is that there's only a CStringLen variant, but that's not hard to get around:

peekCString :: CString -> IO Text
peekCStringLen cs = do
  bs <- Data.ByteString.Unsafe.unsafePackCString cs
  return $! decodeUtf8 bs

This is almost exactly like peekCStringLen, but calls a different function from Data.ByteString.Unsafe, since there's no length information present.

This seems like a good idea, if you're willing to sacrifice support for non-UTF-8. you could always use something like bytestring-encodings.Data.ByteString.Encoding.isUtf8 (https://hackage.haskell.org/package/bytestring-encodings-0.2.0.2/docs/Data-ByteString-Encodings.html#v:isUtf8) to verify that the ByteString is UTF-8 encoded before proceeding, but then you'd probably have to return a 'Maybe Text', which doesn't seem worth it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions