Skip to content

Commit ba5f689

Browse files
Change values to use foldr for improved speed (#61)
Since this implementation does not traverse through an intermediary list multiple times, it is must faster
1 parent f97253d commit ba5f689

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Breaking changes:
1010
New features:
1111
- Exported `Data.Map.Internal` data constructors (#52 by @natefaubion)
1212
- Add unbiased `Semigroup`/`Monoid` instances to `Map` with `Warn` (#54 by @JordanMartinez)
13-
- Improved speed of `foldr`, `foldl`, `foldMap`, `foldlWithIndex`, `foldrWithIndex`, `foldMapWithIndex`, `unionWith` (#60 by @xgrommx)
13+
- Improved speed of `foldr`, `foldl`, `foldMap`, `foldlWithIndex`, `foldrWithIndex`, `foldMapWithIndex`, `unionWith`, and `values` (#60 by @xgrommx, #61 by @JordanMartinez)
1414

1515
Bugfixes:
1616

bench/Bench/Data/Map.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ benchMap = do
3636
log "---------------"
3737
benchUnion
3838

39+
log ""
40+
41+
log "values"
42+
log "---------------"
43+
benchValues
44+
3945
where
4046

4147
benchUnion = do
@@ -56,6 +62,20 @@ benchMap = do
5662
log $ "M.union: big map (" <> show size' <> ")"
5763
benchWith 10 \_ -> M.union bigMap' bigMap2'
5864

65+
benchValues = do
66+
let nats = L.range 0 999999
67+
natPairs = (flip Tuple) unit <$> nats
68+
bigMap = Map2a0bff.fromFoldable $ natPairs
69+
bigMap' = M.fromFoldable $ natPairs
70+
size = Map2a0bff.size bigMap
71+
size' = M.size bigMap'
72+
73+
log $ "Map2a0bff.values: big map (" <> show size <> ")"
74+
benchWith 10 \_ -> Map2a0bff.values bigMap
75+
76+
log $ "M.values: big map (" <> show size' <> ")"
77+
benchWith 10 \_ -> M.values bigMap'
78+
5979
benchSize = do
6080
let nats = L.range 0 999999
6181
natPairs = (flip Tuple) unit <$> nats

src/Data/Map/Internal.purs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,7 @@ keys (Three left k1 _ mid k2 _ right) = keys left <> pure k1 <> keys mid <> pure
657657

658658
-- | Get a list of the values contained in a map
659659
values :: forall k v. Map k v -> List v
660-
values Leaf = Nil
661-
values (Two left _ v right) = values left <> pure v <> values right
662-
values (Three left _ v1 mid _ v2 right) = values left <> pure v1 <> values mid <> pure v2 <> values right
660+
values = foldr Cons Nil
663661

664662
-- | Compute the union of two maps, using the specified function
665663
-- | to combine values for duplicate keys.

0 commit comments

Comments
 (0)