@@ -7,6 +7,7 @@ import Reflex.TestPlan
77
88import Control.Monad.Fix
99import Control.Monad
10+import Control.Monad.Identity
1011import Control.Applicative
1112import Data.Foldable
1213import Data.Traversable
@@ -14,6 +15,9 @@ import Data.Traversable
1415import Data.Maybe
1516import Data.Map (Map)
1617import qualified Data.Map as Map
18+import qualified Data.Dependent.Map as DMap
19+
20+import Data.Functor.Misc
1721
1822import Data.Tuple
1923import Data.List
@@ -21,6 +25,7 @@ import Data.List.Split
2125
2226import Prelude
2327
28+
2429mergeTree :: Num a => (Monoid (f [a]), Functor f) => Int -> [f a] -> f a
2530mergeTree n es | length es <= n = sum' es
2631 | otherwise = mergeTree n subTrees
@@ -39,6 +44,8 @@ mergeTreeDyn n dyns | length dyns <= n = sumDyns dyns
3944 merges = chunksOf n dyns
4045 sumDyns ds = mergeListDyn ds >>= mapDyn sum
4146
47+
48+
4249-- N events all firing for N frames
4350continuous :: TestPlan t m => Word -> Word -> m [Event t Word]
4451continuous n frames = for [1..n] $ \i -> plan $ (, i) <$> [1..frames]
@@ -50,10 +57,13 @@ occasional n period frames = traverse plan $ zipWith occs [1..] phases
5057 phases = take (fromIntegral n) $ concat (repeat [1..period])
5158 occs i phase = (, i) <$> [1, phase+1..frames]
5259
60+
61+
5362-- Event which never fires (but isn't 'never')
5463never' :: TestPlan t m => m (Event t Word)
5564never' = plan []
5665
66+
5767-- Event which fires once on first frame
5868event :: TestPlan t m => m (Event t Word)
5969event = plan [(1, 0)]
@@ -62,30 +72,53 @@ event = plan [(1, 0)]
6272events :: TestPlan t m => Word -> m (Event t Word)
6373events n = plan $ (\i -> (i, i)) <$> [1..n]
6474
75+
6576-- N events all originating from one event
6677fmapFan :: Reflex t => Word -> Event t Word -> [Event t Word]
6778fmapFan n e = (\i -> (+i) <$> e) <$> [1..n]
6879
80+
81+-- Make a fan using the value in the event converting it to a singleton Map
82+-- use a fan of size n (key is the value mod n)
83+fanMerge :: Reflex t => Word -> Event t Word -> Event t Word
84+fanMerge n e = leftmost $ s <$> [0..n - 1] where
85+ s k = select f (Const2 k)
86+ f = fan (toMap <$> e)
87+ toMap k = DMap.singleton (Const2 $ k `mod` n) (Identity k)
88+
89+-- Dumb version of the above using simple fmapMaybe to filter
90+fmapFanMerge :: Reflex t => Word -> Event t Word -> Event t Word
91+fmapFanMerge n e = leftmost $ s <$> [0..n - 1] where
92+ s k = flip fmapMaybe e $ \j -> if (k == j `mod` n)
93+ then Just k
94+ else Nothing
95+
96+
97+
6998-- Create an Event with a Map which is dense to size N
7099denseMap :: TestPlan t m => Word -> m (Event t (Map Word Word))
71100denseMap n = plan [(1, m)] where
72101 m = Map.fromList $ zip [1..n] [1..n]
73102
74−fmapChain :: Reflex t => Word -> Event t Word -> (Event t Word)
75−fmapChain 0 e = e
76−fmapChain n e = fmapChain (n - 1) (fmap (+1) e)
103+iter :: (a -> a) -> Word -> a -> a
104+iter _ 0 a = a
105+iter f n a = iter f (n - 1) (f a)
106+
107+iterM :: Monad m => (a -> m a) -> Word -> a -> m a
108+iterM _ 0 a = return a
109+iterM f n a = iterM f (n - 1) =<< f a
110+
111+fmapChain :: (Functor f) => Word -> f Word -> f Word
112+fmapChain = iter (fmap (+1))
77113
78114mapDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
79−mapDynChain 0 d = return d
80−mapDynChain n d = mapDynChain (n - 1) =<< mapDyn (+1) d
115+mapDynChain = iterM (mapDyn (+1))
81116
82117joinDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
83−joinDynChain 0 d = return d
84−joinDynChain n d = joinDynChain (n - 1) =<< joinDyn <$> mapDyn (const d) d
118+joinDynChain = iterM (\d -> joinDyn <$> mapDyn (const d) d)
85119
86120combineDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
87−combineDynChain 0 d = return d
88−combineDynChain n d = combineDynChain (n - 1) =<< combineDyn (+) d d
121+combineDynChain = iterM (\d -> combineDyn (+) d d)
89122
90123pingPong :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> Event t a -> m (Event t a)
91124pingPong e1 e2 = do
@@ -100,27 +133,27 @@ switchFactors i e = do
100133 b <- hold ((+1) <$> e) (e <$ ffilter ((== 0) . (i `mod`)) e)
101134 switchFactors (i - 1) (switch b)
102135
136+
103137switchChain :: (Reflex t, MonadHold t m, MonadFix m) => Word -> Event t Word -> m (Event t Word)
104−switchChain 0 e = return e
105−switchChain i e = switchChain (i - 1) =<< pingPong e ((+1) <$> e)
138+switchChain = iterM (\e -> pingPong e ((+1) <$> e))
139+
106140
107141switchChain2 :: (Reflex t, MonadHold t m, MonadFix m) => Word -> Event t Word -> m (Event t Word)
108−switchChain2 0 e = return e
109−switchChain2 i e = switchChain2 (i - 1) =<< pingPong e ((+1) <$> leftmost [e, e])
142+switchChain2 = iterM (\e -> pingPong e ((+1) <$> leftmost [e, e]))
110143
111144switchPromptlyChain :: (Reflex t, MonadHold t m) => Word -> Event t Word -> m (Event t Word)
112−switchPromptlyChain 0 e = return e
113−switchPromptlyChain i e = do
114− d <- holdDyn e (e <$ e)
115− switchPromptlyChain (i - 1) (switchPromptlyDyn d)
145+switchPromptlyChain = iterM $ \e ->
146+ switchPromptlyDyn <$> holdDyn e (e <$ e)
147+
116148
117149coinChain :: Reflex t => Word -> Event t Word -> Event t Word
118−coinChain 0 e = e
119−coinChain n e = coinChain (n - 1) $ coincidence (e <$ e)
150+coinChain = iter $ \e -> coincidence (e <$ e)
151+
152+fanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
153+fanMergeChain width = iter $ fanMerge width
120154
121−pullChain :: Reflex t => Word -> Behavior t Word -> Behavior t Word
122−pullChain 0 b = b
123−pullChain n b = pullChain (n - 1) $ (+1) <$> b
155+fmapFanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
156+fmapFanMergeChain width = iter $ fmapFanMerge width
124157
125158-- | Data type for a collection (a map) which is updated by providing differences
126159data UpdatedMap t k a = UpdatedMap (Map k a) (Event t (Map k (Maybe a)))
@@ -129,6 +162,9 @@ data UpdatedMap t k a = UpdatedMap (Map k a) (Event t (Map k (Maybe a)))
129162switchMergeEvents :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
130163switchMergeEvents mapChanges = switch . fmap mergeMap <$> holdMap mapChanges
131164
165+-- switchMergeMaybe :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
166+-- switchMergeMaybe (UpdatedMap initial changes) = switchMergeMap initial (fmap (fromMaybe never) <$> changes)
167+
132168switchMergeBehaviors :: forall a t m k. (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Behavior t a) -> m (Behavior t (Map k a))
133169switchMergeBehaviors mapChanges = pull <$> joinMap <$> holdMap mapChanges
134170 where joinMap m = traverse sample =<< sample m
@@ -136,6 +172,7 @@ switchMergeBehaviors mapChanges = pull <$> joinMap <$> holdMap mapChanges
136172-- | Turn an UpdatedMap into a Dynamic by applying the differences to the initial value
137173holdMapDyn :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Dynamic t (Map k a))
138174holdMapDyn (UpdatedMap initial changes) = foldDyn (flip (Map.foldWithKey modify)) initial changes
175+
139176 where
140177 modify k Nothing items = Map.delete k items
141178 modify k (Just item) items = Map.insert k item items
@@ -145,6 +182,7 @@ holdMapDyn (UpdatedMap initial changes) = foldDyn (flip (Map.foldWithKey modify)
145182holdMap :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Behavior t (Map k a))
146183holdMap = (current <$>) . holdMapDyn
147184
185+
148186increasingMerge :: TestPlan t m => [a] -> m (UpdatedMap t Int a)
149187increasingMerge xs = do
150188 e <- planList (zipWith Map.singleton [1..] (Just <$> xs) ++ [reset])
@@ -167,34 +205,43 @@ modifyMerge xs = do
167205 where
168206 initial = Map.fromList $ zip [1..] xs
169207
208+
170209countMany :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Behavior t Int]
171210countMany = traverse (fmap current . count)
172211
173212countManyDyn :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Dynamic t Int]
174213countManyDyn = traverse count
175214
215+
176216-- Given a function which creates a sub-network (from a single event)
177217-- Create a plan which uses a switch to substitute it
178218switches :: TestPlan t m => Word -> (forall n. (MonadHold t n, MonadFix n) => Event t Word -> n (Event t a)) -> m (Event t a)
179219switches numFrames f = do
180220 es <- events numFrames
181221 switch <$> hold never (makeEvents es)
222+
182223 where
183224 makeEvents es = pushAlways (\n -> f (fmap (+n) es)) es
184225
226+
227+
185228-- Two sets of benchmarks, one which we're interested in testing subscribe time (as well as time to fire frames)
186229-- the other, which we're only interested in time for running frames
187230subscribing :: Word -> Word -> [(String, TestCase)]
188231subscribing n frames =
189− [ testE "fmapFan merge" $ switches frames (return . mergeList . fmapFan n)
190− , testE "fmapFan/mergeTree" $ switches frames (return . mergeTree 8 . fmapFan n)
191− , testE "fmapChain" $ switches frames (return . fmapChain n)
192− , testE "switchChain" $ switches frames (switchChain n)
193− , testE "switchPromptlyChain" $ switches frames (switchPromptlyChain n)
194− , testE "switchFactors" $ switches frames (switchFactors n)
195− , testE "coincidenceChain" $ switches frames (return . coinChain n)
232+ [ testSub "fmapFanMerge" $ return . mergeList . fmapFan n
233+ , testSub "fanMerge" $ return . fanMerge n
234+ , testSub "fmapFan/mergeTree" $ return . mergeTree 8 . fmapFan n
235+ , testSub "fmapChain" $ return . fmapChain n
236+ , testSub "switchChain" $ switchChain n
237+ , testSub "switchPromptlyChain" $ switchPromptlyChain n
238+ , testSub "switchFactors" $ switchFactors n
239+ , testSub "coincidenceChain" $ return . coinChain n
196240 ]
197241
242+ where
243+ testSub :: (Eq a, Show a) => String -> (forall t n. (Reflex t, MonadHold t n, MonadFix n) => Event t Word -> n (Event t a)) -> (String, TestCase)
244+ testSub name test = testE name (switches frames test)
198245
199246-- Set of benchmark to test specifically merging dynamic collections
200247-- a pattern which occurs frequently in reflex-dom collections
@@ -209,6 +256,11 @@ merging n =
209256 , testB "increasing behaviors" $ switchMergeBehaviors =<< increasingMerge =<< counters
210257 , testB "decreasing behaviors" $ switchMergeBehaviors =<< decreasingMerge =<< counters
211258 , testB "modify behaviors" $ switchMergeBehaviors =<< modifyMerge =<< counters
259+
260+
261+ -- , testE "increasing events/switchMerge" $ switchMergeMaybe =<< increasingMerge =<< sparse
262+ -- , testE "decreasing events/switchMerge" $ switchMergeMaybe =<< decreasingMerge =<< sparse
263+ -- , testE "modify events/switchMerge" $ switchMergeMaybe =<< modifyMerge =<< sparse
212264 ]
213265
214266 where
@@ -218,15 +270,37 @@ merging n =
218270 counters :: TestPlan t m => m [Behavior t Int]
219271 counters = countMany =<< sparse
220272
273+
274+
275+fans :: Word -> [(String, TestCase)]
276+fans n =
277+ [ testE "fanMapChain" $ fanMergeChain n 10 <$> e
278+ , testE "fmapFanMapChain" $ fmapFanMergeChain n 10 <$> e
279+
280+ , testE "fanMerge " $ fanMerge n <$> events n
281+
282+ , testE "fmapFanMerge " $ fmapFanMerge n <$> events n
283+ , testE "triggerMerge" $ leftmost <$> forM [1..n] (\i -> plan [(i, i)])
284+
285+ ]
286+ where
287+
288+ e :: TestPlan t m => m (Event t Word)
289+ e = events 100
290+
291+
292+
293+
221294dynamics :: Word -> [(String, TestCase)]
222295dynamics n =
223296 [ testE "mapDynChain" $ fmap updated $ mapDynChain n =<< d
224297 , testE "joinDynChain" $ fmap updated $ joinDynChain n =<< d
225298 , testE "combineDynChain" $ fmap updated $ combineDynChain n =<< d
226− , testE "dense mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< dense
299+ , testE "dense mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< dense
227300 , testE "sparse mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< sparse
228301 , testE "mergeDyn" $ fmap updated $ mapDyn sum =<< mergeListDyn =<< sparse
229302 ] where
303+
230304 d :: TestPlan t m => m (Dynamic t Word)
231305 d = count =<< events 10
232306
@@ -245,20 +319,25 @@ firing n =
245319
246320 , testE "dense mergeTree" $ mergeTree 8 <$> dense
247321 , testE "sparse mergeTree" $ mergeTree 8 <$> sparse
248− , testE "runFrame" $ events n
322+
323+ -- Triggers test failure in Spider
324+ --, testE "runFrame" $ events n
325+
249326 , testB "sum counters" $ do
250327 counts <- counters
251328 return $ pull $ sum <$> traverse sample counts
252329
253− , testB "pullChain" $ pullChain n . current <$> (count =<< events 4)
330+ , testB "pullChain" $ fmapChain n . current <$> (count =<< events 4)
254331 , testB "pullChain2" $ do
255− e' <- events 4
256− b <- hold (constant 0) $ leftmost
257− [ constant 0 <$ ffilter (==4) e'
258− , pushAlways (const $ pullChain n <$> hold 0 e') e'
259− ]
332+ es <- events 4
333+ e <- plan [(0, ())]
334+
335+ b <- hold (constant 0) $
336+ pushAlways (const $ fmapChain n <$> hold 0 es) e
337+
260338 return (join b)
261339 , testB "counters/pullTree" $ mergeTree 8 <$> counters
340+
262341 ]
263342 where
264343 e :: TestPlan t m => m (Event t Word)
@@ -271,3 +350,7 @@ firing n =
271350 counters :: TestPlan t m => m [Behavior t Int]
272351 counters = countMany =<< sparse
273352
353+
354+
355+
356+