From 47943248fb5959182b9351bf5954f4c8fdbdd1ae Mon Sep 17 00:00:00 2001 From: gwern0 Date: Tue, 16 Oct 2007 01:48:50 +0200 Subject: ShellPrompt.hs: a quick optimization of nub I saw some complaints about ShellPrompt being slow - and noticed it myself - and it seems ShellPrompt uses 'nub' in an awkward place to uniquefy input. Nub doesn't perform well on long lists, but I once ran into a similar problem and the suggested solution was something clever: convert to a Set and then back to a List. Sets can't have duplicate entries, and they uniquefy faster than nub. The price is that the output is not sorted the same as nub's output would be, but this is OK because the output of (toList . fromList) is immediately passed to 'sort' - which should then produce the same output for both versions. I haven't really tested this but on long directories this should help. darcs-hash:20071015234850-f7719-ce02426337ffbbfb15dd1999713075c5aada81bd.gz --- ShellPrompt.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ShellPrompt.hs b/ShellPrompt.hs index b52fda8..9ff06c3 100644 --- a/ShellPrompt.hs +++ b/ShellPrompt.hs @@ -26,6 +26,7 @@ import XMonadContrib.Dmenu import Control.Monad import Data.List +import Data.Set (toList, fromList) import System.Directory import System.IO import System.Environment @@ -60,7 +61,7 @@ getShellCompl s | s /= "" && last s /= ' ' = do f <- fmap lines $ runProcessWithInput "/bin/bash" [] ("compgen -A file " ++ s ++ "\n") c <- commandCompletionFunction s - return . map escape . sort . nub $ f ++ c + return . map escape . sort . (toList . fromList) $ f ++ c | otherwise = return [] commandCompletionFunction :: String -> IO [String] -- cgit v1.2.3