Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the implementation of Data.Text.concat #551

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,18 +1032,18 @@ foldr' f z t = S.foldl' (P.flip f) z (reverseStream t)

-- | /O(n)/ Concatenate a list of 'Text's.
concat :: [Text] -> Text
concat ts = case ts' of
[] -> empty
[t] -> t
_ -> Text (A.run go) 0 len
concat ts = case ts of
[] -> empty
[t] -> t
_ | len == 0 -> empty
| otherwise -> Text (A.run go) 0 len
where
ts' = L.filter (not . null) ts
len = sumP "concat" $ L.map lengthWord8 ts'
len = sumP "concat" $ L.map lengthWord8 ts
go :: ST s (A.MArray s)
go = do
arr <- A.new len
let step i (Text a o l) = A.copyI l arr i a o >> return (i + l)
foldM step 0 ts' >> return arr
foldM step 0 ts >> return arr
Lysxia marked this conversation as resolved.
Show resolved Hide resolved

-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
-- concatenate the results.
Expand Down Expand Up @@ -2043,12 +2043,12 @@ stripSuffix p@(Text _arr _off plen) t@(Text arr off len)

-- | Add a list of non-negative numbers. Errors out on overflow.
sumP :: String -> [Int] -> Int
sumP fun = go 0
where go !a (x:xs)
| ax >= 0 = go ax xs
sumP fun = L.foldl' add 0
Lysxia marked this conversation as resolved.
Show resolved Hide resolved
where add a x
| ax >= 0 = ax
| otherwise = overflowError fun
where ax = a + x
go a _ = a
{-# INLINE sumP #-} -- Use foldl' and inline for fusion.

emptyError :: HasCallStack => String -> a
emptyError fun = P.error $ "Data.Text." ++ fun ++ ": empty input"
Expand Down
Loading