From 4c8e8b2d724fdb59af1d4ef0a09e434dab9d0d75 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Tue, 17 May 2016 08:33:41 -0400 Subject: [PATCH] Switch to faster implementation of grouping a list Why? ==== Immediately after searching the codebase after tokens, we group results together based on match. This can be slow, especially within large codebases. This improves the time taken, previously O(n^2). Code reference: http://stackoverflow.com/a/15412231 Corresponding benchmark: https://gist.github.com/joshuaclayton/3dcde3b19e2c3006ee922053edebc417 --- src/Unused/Util.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Unused/Util.hs b/src/Unused/Util.hs index 50e171f..27f5ae8 100644 --- a/src/Unused/Util.hs +++ b/src/Unused/Util.hs @@ -2,11 +2,11 @@ module Unused.Util ( groupBy ) where -import Data.List (nub) +import Control.Arrow ((&&&)) +import qualified Data.List as L +import Data.Function -groupBy :: Eq b => (a -> b) -> [a] -> [(b, [a])] -groupBy f l = - fmap (\t -> (t, byTerm t)) uniqueTerms - where - byTerm t = filter ((== t) . f) l - uniqueTerms = nub $ fmap f l +groupBy :: (Ord b) => (a -> b) -> [a] -> [(b, [a])] +groupBy f = map (f . head &&& id) + . L.groupBy ((==) `on` f) + . L.sortBy (compare `on` f)