I have a program (a SAX parser using Text.XML.Expat.SAX
) that builds up very big CDATA nodes using repeated appends of Data.Text
content, using Data.Sequence.(|>)
like so:
existingText |> newTextChunk
This builds up a very big piece of data of type Seq Text
.
After I've built up the data, I need to convert the Seq Text -> Text
. But this solution I tried was super-slow:
Data.Foldable.foldr1 Data.Text.append seqText
Is there a faster way to turn a Sequence of Text into a plain Text datum?
Another way to ask this might be, what's the most efficient way to do merge a list of Text into one Text, i.e. [Text] -> Text
?
append
will create a new array for every element in the list, and copy all data to it. As one of the comments said you might want to try concat
. For sequence you could try doing:
import Data.Foldable (toList)
import Data.Sequence (Seq)
import qualified Data.Sequence as S
import Data.Text (Text)
import qualified Data.Text as T
concatSeq :: Seq Text -> Text
concatSeq = T.concat . toList
This should be faster than doing a fold
with append
, but I haven't verified it. You could try to whip up a small test case using criterion (which is an amazing library).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With