Skip to content

Commit f97253d

Browse files
authored
Added more efficient unionWith, eliminate transformation Map to List in Foldable{WithIndex} instances. (#60)
* Added more efficient `unionWith`, eliminate transformation `Map` to `List` in `Foldable{WithIndex}` instances * Added version of Map before this PR to benchmark for comparison The characters appended to the old `Map` are the first few characters of the hash of the commit used for that implementation of `Map`. * Update CHANGELOG.md
1 parent 2a0bffe commit f97253d

File tree

4 files changed

+833
-12
lines changed

4 files changed

+833
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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)
1314

1415
Bugfixes:
1516

bench/Bench/Data/Map.purs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import Prelude
44

55
import Data.List as L
66
import Data.Map as M
7+
import Bench.Data.Map2a0bff as Map2a0bff
8+
import Data.Foldable as F
9+
import Data.FoldableWithIndex as FI
710
import Data.Tuple (Tuple(..))
811
import Effect (Effect)
912
import Effect.Console (log)
@@ -21,8 +24,38 @@ benchMap = do
2124
log "------------"
2225
benchFromFoldable
2326

27+
log ""
28+
29+
log "Foldable"
30+
log "---------------"
31+
benchFoldable
32+
33+
log ""
34+
35+
log "union"
36+
log "---------------"
37+
benchUnion
38+
2439
where
2540

41+
benchUnion = do
42+
let nats = L.range 0 999999
43+
nats2 = L.range 999999 1999999
44+
natPairs = (flip Tuple) unit <$> nats
45+
natPairs2 = (flip Tuple) unit <$> nats2
46+
bigMap = Map2a0bff.fromFoldable $ natPairs
47+
bigMap2 = Map2a0bff.fromFoldable $ natPairs2
48+
bigMap' = M.fromFoldable $ natPairs
49+
bigMap2' = M.fromFoldable $ natPairs2
50+
size = Map2a0bff.size bigMap
51+
size' = M.size bigMap'
52+
53+
log $ "Map2a0bff.union: big map (" <> show size <> ")"
54+
benchWith 10 \_ -> Map2a0bff.union bigMap bigMap2
55+
56+
log $ "M.union: big map (" <> show size' <> ")"
57+
benchWith 10 \_ -> M.union bigMap' bigMap2'
58+
2659
benchSize = do
2760
let nats = L.range 0 999999
2861
natPairs = (flip Tuple) unit <$> nats
@@ -43,6 +76,50 @@ benchMap = do
4376
log $ "size: big map (" <> show (M.size bigMap) <> ")"
4477
benchWith 10 \_ -> M.size bigMap
4578

79+
benchFoldable = do
80+
let nats = L.range 0 999999
81+
natPairs = (flip Tuple) unit <$> nats
82+
bigMap = Map2a0bff.fromFoldable $ natPairs
83+
bigMap' = M.fromFoldable $ natPairs
84+
size = Map2a0bff.size bigMap
85+
size' = M.size bigMap'
86+
87+
log $ "Map2a0bff.foldr big map (" <> show size <> ")"
88+
benchWith 10 \_ -> F.foldr (\_ _ -> unit) unit bigMap
89+
90+
log $ "M.foldr big map (" <> show size' <> ")"
91+
benchWith 10 \_ -> F.foldr (\_ _ -> unit) unit bigMap'
92+
93+
log $ "Map2a0bff.foldl big map (" <> show size <> ")"
94+
benchWith 10 \_ -> F.foldl (\_ _ -> unit) unit bigMap
95+
96+
log $ "M.foldl big map (" <> show size' <> ")"
97+
benchWith 10 \_ -> F.foldl (\_ _ -> unit) unit bigMap'
98+
99+
log $ "Map2a0bff.foldMap big map (" <> show size <> ")"
100+
benchWith 10 \_ -> F.foldMap (\_ -> unit) bigMap
101+
102+
log $ "M.foldMap big map (" <> show size' <> ")"
103+
benchWith 10 \_ -> F.foldMap (\_ -> unit) bigMap'
104+
105+
log $ "Map2a0bff.foldrWithIndex big map (" <> show size <> ")"
106+
benchWith 10 \_ -> FI.foldrWithIndex (\_ _ _ -> unit) unit bigMap
107+
108+
log $ "M.foldrWithIndex big map (" <> show size' <> ")"
109+
benchWith 10 \_ -> FI.foldrWithIndex (\_ _ _ -> unit) unit bigMap'
110+
111+
log $ "Map2a0bff.foldlWithIndex big map (" <> show size <> ")"
112+
benchWith 10 \_ -> FI.foldlWithIndex (\_ _ _ -> unit) unit bigMap
113+
114+
log $ "M.foldlWithIndex big map (" <> show size' <> ")"
115+
benchWith 10 \_ -> FI.foldlWithIndex (\_ _ _ -> unit) unit bigMap'
116+
117+
log $ "Map2a0bff.foldMapWithIndex big map (" <> show size <> ")"
118+
benchWith 10 \_ -> FI.foldMapWithIndex (\_ _ -> unit) bigMap
119+
120+
log $ "M.foldMapWithIndex big map (" <> show size' <> ")"
121+
benchWith 10 \_ -> FI.foldMapWithIndex (\_ _ -> unit) bigMap'
122+
46123
benchFromFoldable = do
47124
let natStrs = show <$> L.range 0 99999
48125
natPairs = (flip Tuple) unit <$> natStrs

0 commit comments

Comments
 (0)