@@ -7,19 +7,28 @@ import Reflex.TestPlan
77
88import Control.Monad.Fix
99import Control.Monad
10+import Control.Monad.Identity
1011import Control.Applicative
11−import Data.Foldable
12−import Data.Traversable
12+--import Data.Foldable
13+import Data.Traversable (traverse, for)
1314
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
2024import Data.List.Split
2125
22−import Prelude
26+import Data.Word
27+import Data.Monoid
28+
29+
30+import Prelude hiding (sum, concat)
31+
2332
2433mergeTree :: Num a => (Monoid (f [a]), Functor f) => Int -> [f a] -> f a
2534mergeTree n es | length es <= n = sum' es
@@ -39,6 +48,8 @@ mergeTreeDyn n dyns | length dyns <= n = sumDyns dyns
3948 merges = chunksOf n dyns
4049 sumDyns ds = mergeListDyn ds >>= mapDyn sum
4150
51+
52+
4253-- N events all firing for N frames
4354continuous :: TestPlan t m => Word -> Word -> m [Event t Word]
4455continuous n frames = for [1..n] $ \i -> plan $ (, i) <$> [1..frames]
@@ -50,10 +61,13 @@ occasional n period frames = traverse plan $ zipWith occs [1..] phases
5061 phases = take (fromIntegral n) $ concat (repeat [1..period])
5162 occs i phase = (, i) <$> [1, phase+1..frames]
5263
64+
65+
5366-- Event which never fires (but isn't 'never')
5467never' :: TestPlan t m => m (Event t Word)
5568never' = plan []
5669
70+
5771-- Event which fires once on first frame
5872event :: TestPlan t m => m (Event t Word)
5973event = plan [(1, 0)]
@@ -62,30 +76,53 @@ event = plan [(1, 0)]
6276events :: TestPlan t m => Word -> m (Event t Word)
6377events n = plan $ (\i -> (i, i)) <$> [1..n]
6478
79+
6580-- N events all originating from one event
6681fmapFan :: Reflex t => Word -> Event t Word -> [Event t Word]
6782fmapFan n e = (\i -> (+i) <$> e) <$> [1..n]
6883
84+
85+-- Make a fan using the value in the event converting it to a singleton Map
86+-- use a fan of size n (key is the value mod n)
87+fanMerge :: Reflex t => Word -> Event t Word -> Event t Word
88+fanMerge n e = leftmost $ s <$> [0..n - 1] where
89+ s k = select f (Const2 k)
90+ f = fan (toMap <$> e)
91+ toMap k = DMap.singleton (Const2 $ k `mod` n) (Identity k)
92+
93+-- Dumb version of the above using simple fmapMaybe to filter
94+fmapFanMerge :: Reflex t => Word -> Event t Word -> Event t Word
95+fmapFanMerge n e = leftmost $ s <$> [0..n - 1] where
96+ s k = flip fmapMaybe e $ \j -> if (k == j `mod` n)
97+ then Just k
98+ else Nothing
99+
100+
101+
69102-- Create an Event with a Map which is dense to size N
70103denseMap :: TestPlan t m => Word -> m (Event t (Map Word Word))
71104denseMap n = plan [(1, m)] where
72105 m = Map.fromList $ zip [1..n] [1..n]
73106
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)
107+iter :: (a -> a) -> Word -> a -> a
108+iter _ 0 a = a
109+iter f n a = iter f (n - 1) (f a)
110+
111+iterM :: Monad m => (a -> m a) -> Word -> a -> m a
112+iterM _ 0 a = return a
113+iterM f n a = iterM f (n - 1) =<< f a
114+
115+fmapChain :: (Functor f) => Word -> f Word -> f Word
116+fmapChain = iter (fmap (+1))
77117
78118mapDynChain :: (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
119+mapDynChain = iterM (mapDyn (+1))
81120
82121joinDynChain :: (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
122+joinDynChain = iterM (\d -> joinDyn <$> mapDyn (const d) d)
85123
86124combineDynChain :: (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
125+combineDynChain = iterM (\d -> combineDyn (+) d d)
89126
90127pingPong :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> Event t a -> m (Event t a)
91128pingPong e1 e2 = do
@@ -100,27 +137,27 @@ switchFactors i e = do
100137 b <- hold ((+1) <$> e) (e <$ ffilter ((== 0) . (i `mod`)) e)
101138 switchFactors (i - 1) (switch b)
102139
140+
103141switchChain :: (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)
142+switchChain = iterM (\e -> pingPong e ((+1) <$> e))
143+
106144
107145switchChain2 :: (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])
146+switchChain2 = iterM (\e -> pingPong e ((+1) <$> leftmost [e, e]))
110147
111148switchPromptlyChain :: (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)
149+switchPromptlyChain = iterM $ \e ->
150+ switchPromptlyDyn <$> holdDyn e (e <$ e)
151+
116152
117153coinChain :: Reflex t => Word -> Event t Word -> Event t Word
118−coinChain 0 e = e
119−coinChain n e = coinChain (n - 1) $ coincidence (e <$ e)
154+coinChain = iter $ \e -> coincidence (e <$ e)
155+
156+fanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
157+fanMergeChain width = iter $ fanMerge width
120158
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
159+fmapFanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
160+fmapFanMergeChain width = iter $ fmapFanMerge width
124161
125162-- | Data type for a collection (a map) which is updated by providing differences
126163data UpdatedMap t k a = UpdatedMap (Map k a) (Event t (Map k (Maybe a)))
@@ -129,6 +166,9 @@ data UpdatedMap t k a = UpdatedMap (Map k a) (Event t (Map k (Maybe a)))
129166switchMergeEvents :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
130167switchMergeEvents mapChanges = switch . fmap mergeMap <$> holdMap mapChanges
131168
169+-- switchMergeMaybe :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
170+-- switchMergeMaybe (UpdatedMap initial changes) = switchMergeMap initial (fmap (fromMaybe never) <$> changes)
171+
132172switchMergeBehaviors :: 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))
133173switchMergeBehaviors mapChanges = pull <$> joinMap <$> holdMap mapChanges
134174 where joinMap m = traverse sample =<< sample m
@@ -136,6 +176,7 @@ switchMergeBehaviors mapChanges = pull <$> joinMap <$> holdMap mapChanges
136176-- | Turn an UpdatedMap into a Dynamic by applying the differences to the initial value
137177holdMapDyn :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Dynamic t (Map k a))
138178holdMapDyn (UpdatedMap initial changes) = foldDyn (flip (Map.foldWithKey modify)) initial changes
179+
139180 where
140181 modify k Nothing items = Map.delete k items
141182 modify k (Just item) items = Map.insert k item items
@@ -145,6 +186,7 @@ holdMapDyn (UpdatedMap initial changes) = foldDyn (flip (Map.foldWithKey modify)
145186holdMap :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Behavior t (Map k a))
146187holdMap = (current <$>) . holdMapDyn
147188
189+
148190increasingMerge :: TestPlan t m => [a] -> m (UpdatedMap t Int a)
149191increasingMerge xs = do
150192 e <- planList (zipWith Map.singleton [1..] (Just <$> xs) ++ [reset])
@@ -167,34 +209,43 @@ modifyMerge xs = do
167209 where
168210 initial = Map.fromList $ zip [1..] xs
169211
212+
170213countMany :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Behavior t Int]
171214countMany = traverse (fmap current . count)
172215
173216countManyDyn :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Dynamic t Int]
174217countManyDyn = traverse count
175218
219+
176220-- Given a function which creates a sub-network (from a single event)
177221-- Create a plan which uses a switch to substitute it
178222switches :: TestPlan t m => Word -> (forall n. (MonadHold t n, MonadFix n) => Event t Word -> n (Event t a)) -> m (Event t a)
179223switches numFrames f = do
180224 es <- events numFrames
181225 switch <$> hold never (makeEvents es)
226+
182227 where
183228 makeEvents es = pushAlways (\n -> f (fmap (+n) es)) es
184229
230+
231+
185232-- Two sets of benchmarks, one which we're interested in testing subscribe time (as well as time to fire frames)
186233-- the other, which we're only interested in time for running frames
187234subscribing :: Word -> Word -> [(String, TestCase)]
188235subscribing 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)
236+ [ testSub "fmapFanMerge" $ return . mergeList . fmapFan n
237+ , testSub "fanMerge" $ return . fanMerge n
238+ , testSub "fmapFan/mergeTree" $ return . mergeTree 8 . fmapFan n
239+ , testSub "fmapChain" $ return . fmapChain n
240+ , testSub "switchChain" $ switchChain n
241+ , testSub "switchPromptlyChain" $ switchPromptlyChain n
242+ , testSub "switchFactors" $ switchFactors n
243+ , testSub "coincidenceChain" $ return . coinChain n
196244 ]
197245
246+ where
247+ 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)
248+ testSub name test = testE name (switches frames test)
198249
199250-- Set of benchmark to test specifically merging dynamic collections
200251-- a pattern which occurs frequently in reflex-dom collections
@@ -209,6 +260,11 @@ merging n =
209260 , testB "increasing behaviors" $ switchMergeBehaviors =<< increasingMerge =<< counters
210261 , testB "decreasing behaviors" $ switchMergeBehaviors =<< decreasingMerge =<< counters
211262 , testB "modify behaviors" $ switchMergeBehaviors =<< modifyMerge =<< counters
263+
264+
265+ -- , testE "increasing events/switchMerge" $ switchMergeMaybe =<< increasingMerge =<< sparse
266+ -- , testE "decreasing events/switchMerge" $ switchMergeMaybe =<< decreasingMerge =<< sparse
267+ -- , testE "modify events/switchMerge" $ switchMergeMaybe =<< modifyMerge =<< sparse
212268 ]
213269
214270 where
@@ -218,15 +274,37 @@ merging n =
218274 counters :: TestPlan t m => m [Behavior t Int]
219275 counters = countMany =<< sparse
220276
277+
278+
279+fans :: Word -> [(String, TestCase)]
280+fans n =
281+ [ testE "fanMapChain" $ fanMergeChain n 10 <$> e
282+ , testE "fmapFanMapChain" $ fmapFanMergeChain n 10 <$> e
283+
284+ , testE "fanMerge " $ fanMerge n <$> events n
285+
286+ , testE "fmapFanMerge " $ fmapFanMerge n <$> events n
287+ , testE "triggerMerge" $ leftmost <$> for [1..n] (\i -> plan [(i, i)])
288+
289+ ]
290+ where
291+
292+ e :: TestPlan t m => m (Event t Word)
293+ e = events 100
294+
295+
296+
297+
221298dynamics :: Word -> [(String, TestCase)]
222299dynamics n =
223300 [ testE "mapDynChain" $ fmap updated $ mapDynChain n =<< d
224301 , testE "joinDynChain" $ fmap updated $ joinDynChain n =<< d
225302 , testE "combineDynChain" $ fmap updated $ combineDynChain n =<< d
226− , testE "dense mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< dense
303+ , testE "dense mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< dense
227304 , testE "sparse mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< sparse
228305 , testE "mergeDyn" $ fmap updated $ mapDyn sum =<< mergeListDyn =<< sparse
229306 ] where
307+
230308 d :: TestPlan t m => m (Dynamic t Word)
231309 d = count =<< events 10
232310
@@ -245,20 +323,25 @@ firing n =
245323
246324 , testE "dense mergeTree" $ mergeTree 8 <$> dense
247325 , testE "sparse mergeTree" $ mergeTree 8 <$> sparse
248− , testE "runFrame" $ events n
326+
327+ -- Triggers test failure in Spider
328+ --, testE "runFrame" $ events n
329+
249330 , testB "sum counters" $ do
250331 counts <- counters
251332 return $ pull $ sum <$> traverse sample counts
252333
253− , testB "pullChain" $ pullChain n . current <$> (count =<< events 4)
334+ , testB "pullChain" $ fmapChain n . current <$> (count =<< events 4)
254335 , 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− ]
336+ es <- events 4
337+ e <- plan [(0, ())]
338+
339+ b <- hold (constant 0) $
340+ pushAlways (const $ fmapChain n <$> hold 0 es) e
341+
260342 return (join b)
261343 , testB "counters/pullTree" $ mergeTree 8 <$> counters
344+
262345 ]
263346 where
264347 e :: TestPlan t m => m (Event t Word)
@@ -271,3 +354,7 @@ firing n =
271354 counters :: TestPlan t m => m [Behavior t Int]
272355 counters = countMany =<< sparse
273356
357+
358+
359+
360+