Skip to content

Speed up keys by using foldrWithIndex #62

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

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Breaking changes:
New features:
- Exported `Data.Map.Internal` data constructors (#52 by @natefaubion)
- Add unbiased `Semigroup`/`Monoid` instances to `Map` with `Warn` (#54 by @JordanMartinez)
- Improved speed of `foldr`, `foldl`, `foldMap`, `foldlWithIndex`, `foldrWithIndex`, `foldMapWithIndex`, `unionWith`, and `values` (#60 by @xgrommx, #61 by @JordanMartinez)
- Improved speed of `foldr`, `foldl`, `foldMap`, `foldlWithIndex`, `foldrWithIndex`, `foldMapWithIndex`, `unionWith`, `keys` and `values` (#60 by @xgrommx, #61 and #62 by @JordanMartinez)

Bugfixes:

Expand Down
20 changes: 20 additions & 0 deletions bench/Bench/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ benchMap = do
log "---------------"
benchValues

log ""

log "keys"
log "---------------"
benchKeys

where

benchUnion = do
Expand Down Expand Up @@ -76,6 +82,20 @@ benchMap = do
log $ "M.values: big map (" <> show size' <> ")"
benchWith 10 \_ -> M.values bigMap'

benchKeys = do
let nats = L.range 0 999999
natPairs = (flip Tuple) unit <$> nats
bigMap = Map2a0bff.fromFoldable $ natPairs
bigMap' = M.fromFoldable $ natPairs
size = Map2a0bff.size bigMap
size' = M.size bigMap'

log $ "Map2a0bff.keys: big map (" <> show size <> ")"
benchWith 10 \_ -> Map2a0bff.keys bigMap

log $ "M.keys: big map (" <> show size' <> ")"
benchWith 10 \_ -> M.keys bigMap'

benchSize = do
let nats = L.range 0 999999
natPairs = (flip Tuple) unit <$> nats
Expand Down
4 changes: 1 addition & 3 deletions src/Data/Map/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,7 @@ toUnfoldableUnordered m = unfoldr go (m : Nil) where

-- | Get a list of the keys contained in a map
keys :: forall k v. Map k v -> List k
keys Leaf = Nil
keys (Two left k _ right) = keys left <> pure k <> keys right
keys (Three left k1 _ mid k2 _ right) = keys left <> pure k1 <> keys mid <> pure k2 <> keys right
keys = foldrWithIndex (\k _ acc -> k : acc) Nil

-- | Get a list of the values contained in a map
values :: forall k v. Map k v -> List v
Expand Down