text icon indicating copy to clipboard operation
text copied to clipboard

Export `replicateChar`

Open mitchellwrosen opened this issue 6 years ago • 11 comments

Could this function be exported from Data.Text?

mitchellwrosen avatar May 13 '18 17:05 mitchellwrosen

what's the benefit? why isn't replicate+singleton good enough? Note that we're rather reluctant to increase the API surface of Data.Text without a very good justification.

hvr avatar May 14 '18 09:05 hvr

If replicate+singleton has the same performance characteristics, that's great. replicateChar just looked like it was accidentally unexported, because it has a properly formatted haddock.

mitchellwrosen avatar May 14 '18 15:05 mitchellwrosen

Fwiw, it was intentionally removed from the public API almost a decade ago via c8a85dc34e24e7b3aebc22a782f2957734b5d830 ; so this is clearly no oversight.

hvr avatar May 14 '18 20:05 hvr

Sounds good, thanks!

mitchellwrosen avatar May 14 '18 21:05 mitchellwrosen

I wish replicateChar were still exported.

replicate n (singleton c) is much less convenient, causing people to write replicate n "c", which is much less efficient! (at least in terms of Core)

See https://github.com/quchen/prettyprinter/issues/131 for context.

sjakobi avatar Jan 25 '20 00:01 sjakobi

IMHO this gotcha regarding using overloaded literals for single-character Texts should at least be pointed out in the documentation.

sjakobi avatar Jan 25 '20 00:01 sjakobi

what's the benefit? why isn't replicate+singleton good enough? Note that we're rather reluctant to increase the API surface of Data.Text without a very good justification.

Couldn't replicateChar be moved to Data.Text.Internal and exported there? This would give a clear indication that it is not part of the officially supported API, but still allow people to use it.

It is frustrating to know about the performance overhead of replicate n "c" while replicateChar is sitting there unexported. The current alternatives are either ugly (replicate n (singleton 'c')) or require adding boilerplate (redefining replicateChar).

pedrominicz avatar Aug 28 '22 16:08 pedrominicz

@pedrominicz is there a performance overhead still? Could you demonstrate it with text-2.0.1?

Bodigrim avatar Aug 28 '22 16:08 Bodigrim

@Bodigrim the Core for overloadedString is 20 times bigger than for singletonChar (compiled with -O2 -ddump-simpl -ddump-to-file and text-2.0.1). I haven't looked deep into it, but the size difference doesn't seem like a good sign.

{-# LANGUAGE OverloadedStrings #-}

import Data.Text
import qualified Data.Text as T

overloadedString :: Text
overloadedString = T.replicate 10 "c"

singletonChar :: Text
singletonChar = T.replicate 10 (T.singleton 'c')

pedrominicz avatar Aug 28 '22 19:08 pedrominicz

@pedrominicz thanks, I see. Is it any better with the latest master branch?

Bodigrim avatar Aug 28 '22 20:08 Bodigrim

@Bodigrim just tried the latest master branch (https://github.com/haskell/text/commit/5558730e76923f2d5d7fbc8783ab1ecc25bfe15d) and the Core for overloadedString is still massive compared to singletonChar. I used the same GHC flags as in my previous comment.

pedrominicz avatar Aug 28 '22 23:08 pedrominicz