bytestring icon indicating copy to clipboard operation
bytestring copied to clipboard

add toStrictByteString

Open fumieval opened this issue 2 years ago • 5 comments

This change saves the labour of having to import Data.ByteString.Lazy.toStrict just to obtain a strict ByteString.

fumieval avatar May 30 '23 08:05 fumieval

This change saves the labour of having to import Data.ByteString.Lazy.toStrict just to obtain a strict ByteString.

toStrict is available from Data.ByteString as well, which is presumably already imported if you intend to work with strict bytestrings.

Bodigrim avatar May 30 '23 20:05 Bodigrim

Oh, I hadn't realised that it is available since 0.11!

Still, I think it's a bit more convenient to have a strict counterpart in Data.ByteString.Builder.

fumieval avatar May 31 '23 04:05 fumieval

I'm somewhat reluctant to add a trivial composition of toLazyByteString and toStrict under its own name; see https://github.com/haskell/text/pull/295#issuecomment-680165743 for related discussion. @clyring what do you think?

Bodigrim avatar Jun 04 '23 15:06 Bodigrim

I'm mildly in favor as far as API design goes.

L.toStrict . toLazyByteString is not the only reasonable implementation. (Accumulating chunks in reverse order and fixing that up at concatenation time would even save allocating a few thunks. But I'm not sure that's enough of an improvement to be worth providing.)

clyring avatar Jun 04 '23 18:06 clyring

The thing is that if you want to build a StrictByteString you'd be better off using strict builders such as bytestring-strict-builder or text-builder-linear.

What happens in practice way too often is that a user does not really care about performance at all and just needs itoa :: Int -> StrictByteString, and the only way to implement it is Data.ByteString.toStrict . Data.ByteString.Builder.toLazyByteString . Data.ByteString.Builder.intDec. This is mouthful, I agree.

Bodigrim avatar Jun 04 '23 19:06 Bodigrim