From 96e561bc477bd0a4bbffb769369fabe1e1e1982f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 30 Jan 2011 01:41:15 -0400 Subject: [PATCH] use Set instead of existence Map more efficient and idiomatic I did try using Set.difference, it's still slower than my method. --- Command/Unused.hs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Command/Unused.hs b/Command/Unused.hs index 32d70aa19d..28a26f82c6 100644 --- a/Command/Unused.hs +++ b/Command/Unused.hs @@ -9,7 +9,7 @@ module Command.Unused where import Control.Monad (filterM, unless) import Control.Monad.State (liftIO) -import qualified Data.Map as M +import qualified Data.Set as S import Data.Maybe import System.FilePath import System.Directory @@ -99,16 +99,12 @@ calcUnusedKeys present referenced tmps = (unused, staletmp, duptmp) staletmp = tmps `exclude` present duptmp = tmps `exclude` staletmp - -- Constructing a single map, of the set that tends to be + -- Constructing a single set, of the list that tends to be -- smaller, appears more efficient in both memory and CPU - -- than constructing and taking the M.difference of two maps. + -- than constructing and taking the S.difference of two sets. exclude [] _ = [] -- optimisation - exclude smaller larger = M.keys $ remove larger $ existsMap smaller - - existsMap :: Ord k => [k] -> M.Map k Int - existsMap l = M.fromList $ map (\k -> (k, 1)) l - - remove a b = foldl (flip M.delete) b a + exclude smaller larger = S.toList $ remove larger $ S.fromList smaller + remove a b = foldl (flip S.delete) b a {- List of keys referenced by symlinks in the git repo. -} getKeysReferenced :: Annex [Key]