@@ -9,7 +9,6 @@
99{-# LANGUAGE GADTs #-}
1010{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1111{-# LANGUAGE LambdaCase #-}
12−{-# LANGUAGE MultiWayIf #-}
1312{-# LANGUAGE RankNTypes #-}
1413{-# LANGUAGE RoleAnnotations #-}
1514{-# LANGUAGE ScopedTypeVariables #-}
@@ -20,6 +19,7 @@
2019{-# LANGUAGE DeriveFunctor #-}
2120{-# LANGUAGE StandaloneDeriving #-}
2221{-# LANGUAGE InstanceSigs #-}
22+{-# LANGUAGE MultiWayIf #-}
2323
2424#ifdef USE_REFLEX_OPTIMIZER
2525{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
@@ -38,7 +38,6 @@ import Control.Monad hiding (forM, forM_, mapM, mapM_)
3838import Control.Monad.Exception
3939import Control.Monad.Identity hiding (forM, forM_, mapM, mapM_)
4040import Control.Monad.Primitive
41−--import Control.Monad.Reader hiding (forM, forM_, mapM, mapM_)
4241import Control.Monad.Reader.Class
4342import Control.Monad.IO.Class
4443import Control.Monad.ReaderIO
@@ -47,8 +46,9 @@ import Control.Monad.Fail (MonadFail)
4746import qualified Control.Monad.Fail as MonadFail
4847import Data.Align
4948import Data.Coerce
50−import Data.Dependent.Map (DMap, DSum (..))
49+import Data.Dependent.Map (DMap)
5150import qualified Data.Dependent.Map as DMap
51+import Data.Dependent.Sum (DSum (..))
5252import Data.FastMutableIntMap (FastMutableIntMap, PatchIntMap (..))
5353import qualified Data.FastMutableIntMap as FastMutableIntMap
5454import Data.Foldable hiding (concat, elem, sequence_)
@@ -114,7 +114,13 @@ import Reflex.PerformEvent.Base (PerformEventT)
114114import qualified Data.ByteString.Char8 as BS8
115115import System.IO (stderr)
116116import Data.List (isPrefixOf)
117+#endif
117118
119+-- TODO stdout might not be the best channel for debug output
120+debugStrLn :: String -> IO ()
121+debugStrLn = putStrLn
122+
123+#ifdef DEBUG_TRACE_EVENTS
118124withStackOneLine :: (BS8.ByteString -> a) -> a
119125withStackOneLine expr = unsafePerformIO $ do
120126 stack <- currentCallStack
@@ -123,9 +129,33 @@ withStackOneLine expr = unsafePerformIO $ do
123129
124130#endif
125131
132+debugPropagate :: Bool
133+
134+debugInvalidateHeight :: Bool
135+
136+debugInvalidate :: Bool
137+
126138#ifdef DEBUG
127139#define DEBUG_NODEIDS
128140
141+#ifdef DEBUG_TRACE_PROPAGATION
142+debugPropagate = True
143+#else
144+debugPropagate = False
145+#endif
146+
147+#ifdef DEBUG_TRACE_HEIGHT
148+debugInvalidateHeight = True
149+#else
150+debugInvalidateHeight = False
151+#endif
152+
153+#ifdef DEBUG_TRACE_INVALIDATION
154+debugInvalidate = True
155+#else
156+debugInvalidate = False
157+#endif
158+
129159class HasNodeId a where
130160 getNodeId :: a -> Int
131161
@@ -163,6 +193,10 @@ showNodeId' = ("#"<>) . show
163193
164194#else
165195
196+debugPropagate = False
197+debugInvalidateHeight = False
198+debugInvalidate = False
199+
166200-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
167201{-# INLINE showNodeId #-}
168202showNodeId :: a -> String
@@ -219,11 +253,11 @@ subscribeAndRead = unEvent
219253{-# INLINE [1] pushCheap #-}
220254pushCheap :: HasSpiderTimeline x => (a -> ComputeM x (Maybe b)) -> Event x a -> Event x b
221255pushCheap !f e = Event $ \sub -> do
222− (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" (sub
256+ (subscription, occ) <- subscribeAndRead e $ debugSubscriber' "push" $ sub
223257 { subscriberPropagate = \a -> do
224258 mb <- f a
225259 mapM_ (subscriberPropagate sub) mb
226− })
260+ }
227261 occ' <- join <$> mapM f occ
228262 return (subscription, occ')
229263
@@ -273,7 +307,20 @@ data CacheSubscribed x a
273307#endif
274308 }
275309
276−
310+nowSpiderEventM :: (HasSpiderTimeline x) => EventM x (R.Event (SpiderTimeline x) ())
311+nowSpiderEventM =
312+ SpiderEvent <$> now
313+
314+now :: (MonadIO m, Defer (Some Clear) m, HasSpiderTimeline x
315+ ) => m (Event x ())
316+now = do
317+ nowOrNot <- liftIO $ newIORef $ Just ()
318+ scheduleClear nowOrNot
319+ return . Event $ \_ -> do
320+ occ <- liftIO . readIORef $ nowOrNot
321+ return ( EventSubscription (return ()) eventSubscribedNow
322+ , occ
323+ )
277324
278325-- | Construct an 'Event' whose value is guaranteed not to be recomputed
279326-- repeatedly
@@ -417,16 +464,6 @@ data Subscriber x a = Subscriber
417464 , subscriberRecalculateHeight :: !(Height -> IO ())
418465 }
419466
420−--TODO: Move this comment to WeakBag
421−-- These function are constructor functions that are marked NOINLINE so they are
422−-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
423−-- so any weak references that are attached to the constructors will have their
424−-- finalizer run. Using the opaque constructor, does not see the
425−-- constructor application, so it behaves like an IORef and cannot be fused away.
426−--
427−-- The result is also evaluated to WHNF, since forcing a thunk invalidates
428−-- the weak pointer to it in some cases.
429−
430467newSubscriberHold :: (HasSpiderTimeline x, Patch p) => Hold x p -> IO (Subscriber x p)
431468newSubscriberHold h = return $ Subscriber
432469 { subscriberPropagate = {-# SCC "traverseHold" #-} propagateSubscriberHold h
@@ -570,6 +607,16 @@ eventSubscribedNever = EventSubscribed
570607 , eventSubscribedWhoCreated = return ["never"]
571608#endif
572609 }
610+eventSubscribedNow :: EventSubscribed x
611+eventSubscribedNow = EventSubscribed
612+ { eventSubscribedHeightRef = zeroRef
613+ , eventSubscribedRetained = toAny ()
614+#ifdef DEBUG_CYCLES
615+ , eventSubscribedGetParents = return []
616+ , eventSubscribedHasOwnHeightRef = False
617+ , eventSubscribedWhoCreated = return ["now"]
618+#endif
619+ }
573620
574621eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
575622eventSubscribedFan !subscribed = EventSubscribed
@@ -1299,7 +1346,7 @@ mkWeakPtrWithDebug x debugNote = do
12991346 x' <- evaluate x
13001347 mkWeakPtr x' $
13011348 if debugFinalize
1302− then Just $ putStrLn $ "finalizing: " ++ debugNote
1349+ then Just $ debugStrLn $ "finalizing: " ++ debugNote
13031350 else Nothing
13041351
13051352type WeakList a = [Weak a]
@@ -1321,13 +1368,13 @@ debugSubscriber' description subscribed = Subscriber
13211368 tracePropagate (Proxy :: Proxy x) ("subscriberPropagate: " <> description)
13221369 subscriberPropagate subscribed m
13231370 , subscriberInvalidateHeight = \old -> do
1324− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
1371+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", old = " <> show (unHeight old)
13251372 subscriberInvalidateHeight subscribed old
1326− putStrLn $ "invalidateSubscriberHeight: " <> description <> ", done"
1373+ traceInvalidateHeight $ "invalidateSubscriberHeight: " <> description <> ", done"
13271374 , subscriberRecalculateHeight = \new -> do
1328− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
1375+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", new = " <> show (unHeight new)
13291376 subscriberRecalculateHeight subscribed new
1330− putStrLn $ "subscriberRecalculateHeight: " <> description <> ", done"
1377+ traceInvalidateHeight $ "subscriberRecalculateHeight: " <> description <> ", done"
13311378 }
13321379
13331380
@@ -1341,11 +1388,15 @@ withIncreasedDepth _ a = do
13411388
13421389{-# INLINE tracePropagate #-}
13431390tracePropagate :: (CanTrace x m) => proxy x -> String -> m ()
1344−tracePropagate p = trace p
1391+tracePropagate p = when debugPropagate . trace p
13451392
13461393{-# INLINE traceInvalidate #-}
13471394traceInvalidate :: String -> IO ()
1348−traceInvalidate = liftIO . putStrLn
1395+traceInvalidate = when debugInvalidate . liftIO . debugStrLn
1396+
1397+{-# INLINE traceInvalidateHeight #-}
1398+traceInvalidateHeight :: String -> IO ()
1399+traceInvalidateHeight = when debugInvalidateHeight . liftIO . debugStrLn
13491400
13501401{-# INLINE trace #-}
13511402trace :: (CanTrace x m) => proxy x -> String -> m ()
@@ -1356,7 +1407,7 @@ traceM :: forall x proxy m. (CanTrace x m) => proxy x -> m String -> m ()
13561407traceM _ getMessage = do
13571408 message <- getMessage
13581409 d <- liftIO $ readIORef $ _spiderTimeline_depth $ unSTE (spiderTimeline :: SpiderTimelineEnv x)
1359− liftIO $ putStrLn $ replicate d ' ' <> message
1410+ liftIO $ debugStrLn $ replicate d ' ' <> message
13601411
13611412#else
13621413
@@ -1372,6 +1423,10 @@ tracePropagate _ _ = return ()
13721423traceInvalidate :: String -> IO ()
13731424traceInvalidate _ = return ()
13741425
1426+{-# INLINE traceInvalidateHeight #-}
1427+traceInvalidateHeight :: String -> IO ()
1428+traceInvalidateHeight _ = return ()
1429+
13751430{-# INLINE debugSubscriber #-}
13761431debugSubscriber :: String -> Subscriber x a -> IO (Subscriber x a)
13771432debugSubscriber _ = return
@@ -1412,8 +1467,11 @@ showStacks :: [[String]] -> String
14121467showStacks = drawForest . listsToForest . fmap (filterStack "Reflex.Spider.Internal")
14131468
14141469filterStack :: String -> [String] -> [String]
1470+#ifdef DEBUG_HIDE_INTERNALS
14151471filterStack prefix = filter (not . (prefix `isPrefixOf`))
1416−
1472+#else
1473+filterStack prefix = id
1474+#endif
14171475
14181476#ifdef DEBUG_CYCLES
14191477
@@ -1440,7 +1498,7 @@ instance Show EventLoopException where
14401498{-# INLINE propagateSubscriberHold #-}
14411499propagateSubscriberHold :: forall x p. (HasSpiderTimeline x, Patch p) => Hold x p -> p -> EventM x ()
14421500propagateSubscriberHold h a = do
1443− traceM (Proxy :: Proxy x) $ liftIO $ do
1501+ {-# SCC "trace" #-} when debugPropagate $ traceM (Proxy :: Proxy x) $ liftIO $ do
14441502 invalidators <- liftIO $ readIORef $ holdInvalidators h
14451503 return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
14461504
@@ -1448,7 +1506,7 @@ propagateSubscriberHold h a = do
14481506 case {-# SCC "apply" #-} apply a v of
14491507 Nothing -> return ()
14501508 Just v' -> do
1451− withIncreasedDepth (Proxy :: Proxy x) $
1509+ {-# SCC "trace2" #-} withIncreasedDepth (Proxy :: Proxy x) $
14521510 tracePropagate (Proxy :: Proxy x) ("propagateSubscriberHold: assigning Hold" <> showNodeId h)
14531511 vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
14541512 iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
@@ -1980,7 +2038,7 @@ revalidateMergeHeight m = do
19802038 LT -> return ()
19812039 EQ -> do
19822040 let height = succHeight $ heightBagMax heights
1983− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2041+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
19842042 writeIORef (_merge_heightRef m) $! height
19852043 subscriberRecalculateHeight (_merge_sub m) height
19862044 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show (DMap.size parents) <> ") for Merge"
@@ -2144,7 +2202,7 @@ mergeIntCheap d = Event $ \sub -> do
21442202 LT -> return ()
21452203 EQ -> do
21462204 let height = succHeight $ heightBagMax heights
2147− traceInvalidate $ "recalculateSubscriberHeight: height: " <> show height
2205+ traceInvalidateHeight $ "recalculateSubscriberHeight: height: " <> show height
21482206 writeIORef heightRef $! height
21492207 subscriberRecalculateHeight sub height
21502208 GT -> error $ "revalidateMergeHeight: more heights (" <> show (heightBagSize heights) <> ") than parents (" <> show numParents <> ") for Merge"
@@ -2327,7 +2385,7 @@ runFrame a = SpiderHost $ do
23272385 subscription <- unSpiderHost $ runFrame $ {-# SCC "subscribeSwitch" #-} subscribe e sub --TODO: Assert that the event isn't firing --TODO: This should not loop because none of the events should be firing, but still, it is inefficient
23282386 {-
23292387 stackTrace <- liftIO $ fmap renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
2330− liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
2388+ liftIO $ debugStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
23312389 EventRoot _ -> "EventRoot"
23322390 EventNever -> "EventNever"
23332391 _ -> "something else"
@@ -2462,6 +2520,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Event
24622520 {-# INLINABLE headE #-}
24632521 headE = R.slowHeadE
24642522-- headE (SpiderEvent e) = SpiderEvent <$> Reflex.Spider.Internal.headE e
2523+ {-# INLINABLE now #-}
2524+ now = nowSpiderEventM
24652525
24662526instance Reflex.Class.MonadSample (SpiderTimeline x) (SpiderPullM x) where
24672527 {-# INLINABLE sample #-}
@@ -2483,6 +2543,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
24832543 {-# INLINABLE headE #-}
24842544 headE = R.slowHeadE
24852545-- headE (SpiderEvent e) = SpiderPushM $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2546+ {-# INLINABLE now #-}
2547+ now = SpiderPushM nowSpiderEventM
2548+
24862549
24872550instance HasSpiderTimeline x => Monad (Reflex.Class.Dynamic (SpiderTimeline x)) where
24882551 {-# INLINE return #-}
@@ -2546,6 +2609,9 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25462609 buildDynamic getV0 e = runFrame . runSpiderHostFrame $ Reflex.Class.buildDynamic getV0 e
25472610 {-# INLINABLE headE #-}
25482611 headE e = runFrame . runSpiderHostFrame $ Reflex.Class.headE e
2612+ {-# INLINABLE now #-}
2613+ now = runFrame . runSpiderHostFrame $ Reflex.Class.now
2614+
25492615
25502616instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHostFrame x) where
25512617 sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
@@ -2562,6 +2628,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Spide
25622628 {-# INLINABLE headE #-}
25632629 headE = R.slowHeadE
25642630-- headE (SpiderEvent e) = SpiderHostFrame $ SpiderEvent <$> Reflex.Spider.Internal.headE e
2631+ {-# INLINABLE now #-}
2632+ now = SpiderHostFrame Reflex.Class.now
25652633
25662634instance HasSpiderTimeline x => Reflex.Class.MonadSample (SpiderTimeline x) (SpiderHost x) where
25672635 {-# INLINABLE sample #-}
@@ -2582,6 +2650,8 @@ instance HasSpiderTimeline x => Reflex.Class.MonadHold (SpiderTimeline x) (Refle
25822650 buildDynamic getV0 e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.buildDynamic getV0 e
25832651 {-# INLINABLE headE #-}
25842652 headE e = Reflex.Spider.Internal.ReadPhase $ Reflex.Class.headE e
2653+ {-# INLINABLE now #-}
2654+ now = Reflex.Spider.Internal.ReadPhase Reflex.Class.now
25852655
25862656--------------------------------------------------------------------------------
25872657-- Deprecated items
@@ -2705,7 +2775,7 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
27052775 fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
27062776 {-# INLINABLE mergeG #-}
27072777 mergeG
2708− :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
2778+ :: forall k2 (k :: k2 -> *) q (v :: k2 -> *). GCompare k
27092779 => (forall a. q a -> R.Event (SpiderTimeline x) (v a))
27102780 -> DMap k q
27112781 -> R.Event (SpiderTimeline x) (DMap k v)