[Git][ghc/ghc][master] Avoid allocations in `splitAtList` (#18535)
Marge Bot
gitlab at gitlab.haskell.org
Mon Aug 10 01:18:41 UTC 2020
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
77398b67 by Sylvain Henry at 2020-08-09T21:18:34-04:00
Avoid allocations in `splitAtList` (#18535)
As suspected by @simonpj in #18535, avoiding allocations in
`GHC.Utils.Misc.splitAtList` when there are no leftover arguments is
beneficial for performance:
On CI validate-x86_64-linux-deb9-hadrian:
T12227 -7%
T12545 -12.3%
T5030 -10%
T9872a -2%
T9872b -2.1%
T9872c -2.5%
Metric Decrease:
T12227
T12545
T5030
T9872a
T9872b
T9872c
- - - - -
1 changed file:
- compiler/GHC/Utils/Misc.hs
Changes:
=====================================
compiler/GHC/Utils/Misc.hs
=====================================
@@ -774,12 +774,15 @@ dropList _ xs@[] = xs
dropList (_:xs) (_:ys) = dropList xs ys
+-- | Given two lists xs=x0..xn and ys=y0..ym, return `splitAt n ys`.
splitAtList :: [b] -> [a] -> ([a], [a])
-splitAtList [] xs = ([], xs)
-splitAtList _ xs@[] = (xs, xs)
-splitAtList (_:xs) (y:ys) = (y:ys', ys'')
- where
- (ys', ys'') = splitAtList xs ys
+splitAtList xs ys = go 0 xs ys
+ where
+ -- we are careful to avoid allocating when there are no leftover
+ -- arguments: in this case we can return "ys" directly (cf #18535)
+ go _ _ [] = (ys, []) -- len(ys) <= len(xs)
+ go n [] bs = (take n ys, bs) -- = splitAt n ys
+ go n (_:as) (_:bs) = go (n+1) as bs
-- drop from the end of a list
dropTail :: Int -> [a] -> [a]
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/77398b678aba45ba25932a39b7e8a7a31d0dd6f3
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/77398b678aba45ba25932a39b7e8a7a31d0dd6f3
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200809/0453934b/attachment.html>
More information about the ghc-commits
mailing list