Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup 'nubBy' by doing more things to intermediate array in-place #205

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions bench/Data/Array.purs
Original file line number Diff line number Diff line change
@@ -3,20 +3,25 @@ module Bench.Data.Array where
import Prelude

import Data.Array as Array
import Data.Traversable (sequence_)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Performance.Minibench (benchWith)

benchArray :: Effect Unit
benchArray = do
log "mapMaybe"
log "---------------"
benchMapMaybe
sequence_ $ Array.intersperse (log "")
[ benchMapMaybe
, benchNubBy
]


where

benchMapMaybe = do
log "mapMaybe"
log "---------------"
let shortNats = Array.range 0 100
longNats = Array.range 0 10000
onlyEven x = if x `mod` 2 == 0 then Just x else Nothing
@@ -26,3 +31,16 @@ benchArray = do

log $ "mapMaybe (" <> show (Array.length longNats) <> ")"
benchWith 100 \_ -> Array.mapMaybe onlyEven longNats

benchNubBy = do
log "nubBy"
log "---------------"
let shortNats = Array.range 0 100
longNats = Array.range 0 10000
mod3Cmp x y = compare (x `mod` 3) (y `mod` 3)

log $ "nubBy (" <> show (Array.length shortNats) <> ")"
benchWith 1000 \_ -> Array.nubBy mod3Cmp shortNats

log $ "nubBy (" <> show (Array.length longNats) <> ")"
benchWith 100 \_ -> Array.nubBy mod3Cmp longNats
9 changes: 6 additions & 3 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
@@ -1031,17 +1031,20 @@ nubEq = nubByEq eq
nubBy :: forall a. (a -> a -> Ordering) -> Array a -> Array a
nubBy comp xs = case head indexedAndSorted of
Nothing -> []
Just x -> map snd $ sortWith fst $ ST.run do
Just x -> map snd $ ST.run do
-- TODO: use NonEmptyArrays here to avoid partial functions
result <- STA.unsafeThaw $ singleton x
ST.foreach indexedAndSorted \pair@(Tuple i x') -> do
lst <- snd <<< unsafePartial (fromJust <<< last) <$> STA.unsafeFreeze result
when (comp lst x' /= EQ) $ void $ STA.push pair result
_ <- STA.sortWith fst result
STA.unsafeFreeze result
where
indexedAndSorted :: Array (Tuple Int a)
indexedAndSorted = sortBy (\x y -> comp (snd x) (snd y))
(mapWithIndex Tuple xs)
indexedAndSorted = ST.run do
arr <- STA.unsafeThaw (mapWithIndex Tuple xs)
_ <- STA.sortBy (\x y -> comp (snd x) (snd y)) arr
STA.unsafeFreeze arr

-- | Remove the duplicates from an array, where element equality is determined
-- | by the specified equivalence relation, creating a new array.