From 2d0ca2c528dcf711e6404e57b6e0eea8e2a3be7f Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Tue, 30 Nov 2010 16:31:31 +0000 Subject: [PATCH] Loader functions: move from assoc lists to maps When loading big clusters, the association lists become a bit slow, so we'll replace this with a simple Map String Int; the change is trivial and can be reverted easily, while it brings up a good speedup in the data loading. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Balazs Lecz <leczb@google.com> --- Ganeti/HTools/Loader.hs | 13 ++++++++----- Ganeti/HTools/Types.hs | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Ganeti/HTools/Loader.hs b/Ganeti/HTools/Loader.hs index 30a261416..e1f35a0e1 100644 --- a/Ganeti/HTools/Loader.hs +++ b/Ganeti/HTools/Loader.hs @@ -40,6 +40,7 @@ module Ganeti.HTools.Loader import Data.Function (on) import Data.List import Data.Maybe (fromJust) +import qualified Data.Map as M import Text.Printf (printf) import qualified Ganeti.HTools.Container as Container @@ -78,14 +79,14 @@ data Request = Request RqType Node.List Instance.List [String] -- | Lookups a node into an assoc list. lookupNode :: (Monad m) => NameAssoc -> String -> String -> m Ndx lookupNode ktn inst node = - case lookup node ktn of + case M.lookup node ktn of Nothing -> fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst Just idx -> return idx -- | Lookups an instance into an assoc list. lookupInstance :: (Monad m) => NameAssoc -> String -> m Idx lookupInstance kti inst = - case lookup inst kti of + case M.lookup inst kti of Nothing -> fail $ "Unknown instance '" ++ inst ++ "'" Just idx -> return idx @@ -93,9 +94,11 @@ lookupInstance kti inst = assignIndices :: (Element a) => [(String, a)] -> (NameAssoc, [(Int, a)]) -assignIndices = - unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx))) - . zip [0..] +assignIndices nodes = + let (na, idx_node) = + unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx))) + . zip [0..] $ nodes + in (M.fromList na, idx_node) -- | Assoc element comparator assocEqual :: (Eq a) => (a, b) -> (a, b) -> Bool diff --git a/Ganeti/HTools/Types.hs b/Ganeti/HTools/Types.hs index 200163b9c..7a0c6db90 100644 --- a/Ganeti/HTools/Types.hs +++ b/Ganeti/HTools/Types.hs @@ -55,6 +55,8 @@ module Ganeti.HTools.Types , queryTimeout ) where +import qualified Data.Map as M + -- | The instance index type. type Idx = Int @@ -62,7 +64,7 @@ type Idx = Int type Ndx = Int -- | The type used to hold name-to-idx mappings. -type NameAssoc = [(String, Int)] +type NameAssoc = M.Map String Int -- | A separate name for the cluster score type. type Score = Double -- GitLab