[commit: packages/containers] cleaned_bugfix394, master, merge-doc-target, merge-fixes-5.9, revert-408-bugfix_394: Implement fromAscList independently (79b6aaa)

git at git.haskell.org git at git.haskell.org
Mon Apr 17 21:45:28 UTC 2017


Repository : ssh://git@git.haskell.org/containers

On branches: cleaned_bugfix394,master,merge-doc-target,merge-fixes-5.9,revert-408-bugfix_394
Link       : http://git.haskell.org/packages/containers.git/commitdiff/79b6aaa75d5a8d6d60466e530de1da6ce6c2f9f6

>---------------------------------------------------------------

commit 79b6aaa75d5a8d6d60466e530de1da6ce6c2f9f6
Author: David Feuer <David.Feuer at gmail.com>
Date:   Sun Sep 4 04:38:11 2016 -0400

    Implement fromAscList independently
    
    Implementing the lazy version of `fromAscList` in terms of
    `fromAscListWithKey` leaks memory if there are many repeated
    keys. Inlining `fromAscListWithKey` into `fromAscList` manually
    should fix this. The same goes for `fromDescList`.


>---------------------------------------------------------------

79b6aaa75d5a8d6d60466e530de1da6ce6c2f9f6
 Data/Map/Internal.hs | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/Data/Map/Internal.hs b/Data/Map/Internal.hs
index 0b8202f..8ed1d08 100644
--- a/Data/Map/Internal.hs
+++ b/Data/Map/Internal.hs
@@ -3312,7 +3312,19 @@ foldlFB = foldlWithKey
 
 fromAscList :: Eq k => [(k,a)] -> Map k a
 fromAscList xs
-  = fromAscListWithKey (\_ x _ -> x) xs
+  = fromDistinctAscList (combineEq xs)
+  where
+  -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
+  combineEq xs'
+    = case xs' of
+        []     -> []
+        [x]    -> [x]
+        (x:xx) -> combineEq' x xx
+
+  combineEq' z [] = [z]
+  combineEq' z@(kz,zz) (x@(kx,xx):xs')
+    | kx==kz    = combineEq' (kx,xx) xs'
+    | otherwise = z:combineEq' x xs'
 #if __GLASGOW_HASKELL__
 {-# INLINABLE fromAscList #-}
 #endif
@@ -3326,8 +3338,19 @@ fromAscList xs
 -- > valid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
 
 fromDescList :: Eq k => [(k,a)] -> Map k a
-fromDescList xs
-  = fromDescListWithKey (\_ x _ -> x) xs
+fromDescList xs = fromDistinctDescList (combineEq xs)
+  where
+  -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
+  combineEq xs'
+    = case xs' of
+        []     -> []
+        [x]    -> [x]
+        (x:xx) -> combineEq' x xx
+
+  combineEq' z [] = [z]
+  combineEq' z@(kz,zz) (x@(kx,xx):xs')
+    | kx==kz    = combineEq' (kx,xx) xs'
+    | otherwise = z:combineEq' x xs'
 #if __GLASGOW_HASKELL__
 {-# INLINABLE fromDescList #-}
 #endif



More information about the ghc-commits mailing list