Skip to content

Commit

Permalink
Replaced nub with nubOrd/nubInt which are more efficient
Browse files Browse the repository at this point in the history
nub is O(n^2), while nubOrd is O(n log n)

Makes a difference when compiling lots of HSX
  • Loading branch information
mpscholten committed Sep 28, 2021
1 parent 601b37b commit bee61ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion IHP/HSX/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import qualified Language.Haskell.Meta as Haskell
import qualified Language.Haskell.TH.Syntax as Haskell
import qualified "template-haskell" Language.Haskell.TH as TH
import qualified Data.Set as Set
import qualified Data.Containers.ListUtils as List

data AttributeValue = TextValue !Text | ExpressionValue !Haskell.Exp deriving (Eq, Show)

Expand Down Expand Up @@ -125,7 +126,7 @@ hsxNodeAttributes end = staticAttributes
attributes <- manyTill (hsxNodeAttribute <|> hsxSplicedAttributes) end
let staticAttributes = List.filter isStaticAttribute attributes
let keys = List.map (\(StaticAttribute name _) -> name) staticAttributes
let uniqueKeys = List.nub keys
let uniqueKeys = List.nubOrd keys
unless (keys == uniqueKeys) (fail $ "Duplicate attribute found in tag: " <> show (keys List.\\ uniqueKeys))
pure attributes

Expand Down
3 changes: 2 additions & 1 deletion IHP/Pagination/ViewFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import IHP.View.Classes
import qualified Network.Wai as Wai
import qualified Network.HTTP.Types.URI as Query
import IHP.ViewSupport (theRequest)
import qualified Data.Containers.ListUtils as List


-- | Render a navigation for your pagination. This is to be used in your view whenever
Expand Down Expand Up @@ -134,7 +135,7 @@ renderPagination pagination@Pagination {currentPage, window, pageSize} =
if window > totalPages then
[1..getLastPage pagination]
else
nub $ 1 : [max 1 lowerBound..min (getLastPage pagination) upperBound] ++ [totalPages]
List.nubInt $ 1 : [max 1 lowerBound..min (getLastPage pagination) upperBound] ++ [totalPages]

-- | Render a filtering box in your view. Allows the user to type in a query and filter
-- results according to what they type.
Expand Down

1 comment on commit bee61ae

@CSchank
Copy link
Contributor

@CSchank CSchank commented on bee61ae Oct 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.