on master help sign in
Use an IORef for the counts in hashmap

This reduces the garbage generated by frequently updating the hashmap.

commit349c9d7a64656794a377babc03a71638abcf3bd6 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 18 Sep 2019, 10:26 (7 years ago) parentdf93350
1 file changed+7−3
examples/WordClassifier.hs+7−3
examples/WordClassifier.hs+7−3
@@ -12,6 +12,7 @@ import Data.Function ((&))
1212import Data.Functor.Identity (Identity(..))
1313import qualified Data.HashMap.Strict as Map
1414import Data.Hashable
15+import Data.IORef
1516import qualified Data.List as List
1617import qualified Data.Ord as Ord
1718import Foreign.Storable (Storable(..))
@@ -48,7 +49,8 @@ isAlpha c
4849main :: IO ()
4950main =
5051 let
51− increment m str = Map.insertWith (+) str (1 :: Int) m
52+ alter Nothing = fmap Just $ newIORef (1 :: Int)
53+ alter (Just ref) = modifyIORef' ref (+ 1) >> return (Just ref)
5254 in do
5355 name <- fmap head getArgs
5456 src <- openFile name ReadMode
@@ -57,6 +59,8 @@ main =
5759 & Streamly.map toLower -- SerialT IO Char
5860 & Streamly.foldWords FL.toList -- SerialT IO String
5961 & Streamly.filter (all isAlpha)
60− & Streamly.foldl' increment Map.empty
61− & fmap (List.take 25 . List.sortOn (Ord.Down . snd) . Map.toList)
62+ & Streamly.foldlM' (flip (Map.alterF alter)) Map.empty
63+ & fmap Map.toList
64+ >>= mapM (\(w, ref) -> readIORef ref >>= \cnt -> return (w, cnt))
65+ & fmap (List.take 25 . List.sortOn (Ord.Down . snd))
6266 >>= traverse_ print