on develop help sign in
Revert "Generalize fan following DMap (#318)"

This reverts commit 13e76bb3db6566cdd9e51544ea456eee807ed302.

commit04403d3e2a208c5634ee1229aa0cbb89803c2af9 authorMatthew Bauer <mjbauer95@gmail.com> dateFri 31 Jan 2020, 16:26 (7 years ago) parent123b62e
6 files changed+33−67
ChangeLog.md−4 src/Reflex/Class.hs+3−31 src/Reflex/Profiled.hs+1−1 src/Reflex/Pure.hs+3−3 src/Reflex/Spider/Internal.hs+24−26 test/GC.hs+2−2
ChangeLog.md−4
@@ -16,16 +16,12 @@
1616
1717## 0.6.2.1
1818
19−* Generalize `fan` to `fanG` to take a `DMap` with non-`Identity`
20− values.
21−
2219* Generalize merging functions:
2320 `merge` to `mergeG`,
2421 `mergeIncremental` to `mergeIncrementalG`,
2522 `distributeDMapOverDynPure` to `distributeDMapOverDynPureG`,
2623 `mergeIncrementalWithMove` to `mergeIncrementalWithMoveG`.
2724
28−
2925## 0.6.2.0
3026
3127* Fix `holdDyn` so that it is lazy in its event argument
src/Reflex/Class.hs+3−31
@@ -41,7 +41,6 @@ module Reflex.Class
4141 , MonadHold (..)
4242 -- ** 'fan' related types
4343 , EventSelector (..)
44− , EventSelectorG (..)
4544 , EventSelectorInt (..)
4645 -- * Convenience functions
4746 , constDyn
@@ -65,7 +64,6 @@ module Reflex.Class
6564 , alignEventWithMaybe
6665 -- ** Breaking up 'Event's
6766 , splitE
68− , fan
6967 , fanEither
7068 , fanThese
7169 , fanMap
@@ -273,16 +271,13 @@ class ( MonadHold t (PushM t)
273271 -- | Merge a collection of events; the resulting 'Event' will only occur if at
274272 -- least one input event is occurring, and will contain all of the input keys
275273 -- that are occurring simultaneously
276−
277− --TODO: Generalize to get rid of DMap use --TODO: Provide a type-level guarantee that the result is not empty
278274 mergeG :: GCompare k => (forall a. q a -> Event t (v a))
279275 -> DMap k q -> Event t (DMap k v)
280−
276+ --TODO: Generalize to get rid of DMap use --TODO: Provide a type-level guarantee that the result is not empty
281277 -- | Efficiently fan-out an event to many destinations. You should save the
282− -- result in a @let@-binding, and then repeatedly 'selectG' on the result to
278+ -- result in a @let@-binding, and then repeatedly 'select' on the result to
283279 -- create child events
284− fanG :: GCompare k => Event t (DMap k v) -> EventSelectorG t k v
285−
280+ fan :: GCompare k => Event t (DMap k Identity) -> EventSelector t k
286281 -- | Create an 'Event' that will occur whenever the currently-selected input
287282 -- 'Event' occurs
288283 switch :: Behavior t (Event t a) -> Event t a
@@ -330,18 +325,6 @@ class ( MonadHold t (PushM t)
330325 mergeIntIncremental :: Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
331326 fanInt :: Event t (IntMap a) -> EventSelectorInt t a
332327
333−-- | Efficiently fan-out an event to many destinations. You should save the
334−-- result in a @let@-binding, and then repeatedly 'select' on the result to
335−-- create child events
336−fan :: forall t k. (Reflex t, GCompare k)
337− => Event t (DMap k Identity) -> EventSelector t k
338− --TODO: Can we help enforce the partial application discipline here? The combinator is worthless without it
339−fan e = EventSelector (fixup (selectG (fanG e) :: k a -> Event t (Identity a)) :: forall a. k a -> Event t a)
340− where
341− fixup :: forall a. (k a -> Event t (Identity a)) -> k a -> Event t a
342− fixup = case eventCoercion Coercion :: Coercion (Event t (Identity a)) (Event t a) of
343− Coercion -> coerce
344−
345328--TODO: Specialize this so that we can take advantage of knowing that there's no changing going on
346329-- | Constructs a single 'Event' out of a map of events. The output event may fire with multiple
347330-- keys simultaneously.
@@ -535,17 +518,6 @@ newtype EventSelector t k = EventSelector
535518 select :: forall a. k a -> Event t a
536519 }
537520
538−newtype EventSelectorG t k v = EventSelectorG
539− { -- | Retrieve the 'Event' for the given key. The type of the 'Event' is
540− -- determined by the type of the key, so this can be used to fan-out
541− -- 'Event's whose sub-'Event's have different types.
542− --
543− -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
544− -- (but equivalent to) using 'mapMaybe' to select only the relevant
545− -- occurrences of an 'Event'.
546− selectG :: forall a. k a -> Event t (v a)
547− }
548−
549521-- | Efficiently select an 'Event' keyed on 'Int'. This is more efficient than manually
550522-- filtering by key.
551523newtype EventSelectorInt t a = EventSelectorInt { selectInt :: Int -> Event t a }
src/Reflex/Profiled.hs+1−1
@@ -136,11 +136,11 @@ instance Reflex t => Reflex (ProfiledTimeline t) where
136136 push f (Event_Profiled e) = coerce $ push (coerce f) $ profileEvent e -- Profile before rather than after; this way fanout won't count against us
137137 pushCheap f (Event_Profiled e) = coerce $ pushCheap (coerce f) $ profileEvent e
138138 pull = Behavior_Profiled . pull . coerce
139− fanG (Event_Profiled e) = EventSelectorG $ coerce $ selectG (fanG $ profileEvent e)
140139 mergeG :: forall (k :: z -> *) q v. GCompare k
141140 => (forall a. q a -> Event (ProfiledTimeline t) (v a))
142141 -> DMap k q -> Event (ProfiledTimeline t) (DMap k v)
143142 mergeG nt = Event_Profiled #. mergeG (coerce nt)
143+ fan (Event_Profiled e) = EventSelector $ coerce $ select (fan $ profileEvent e)
144144 switch (Behavior_Profiled b) = coerce $ profileEvent $ switch (coerceBehavior b)
145145 coincidence (Event_Profiled e) = coerce $ profileEvent $ coincidence (coerceEvent e)
146146 current (Dynamic_Profiled d) = coerce $ current d
src/Reflex/Pure.hs+3−3
@@ -7,7 +7,6 @@
77{-# LANGUAGE TypeFamilies #-}
88{-# LANGUAGE RankNTypes #-}
99{-# LANGUAGE PolyKinds #-}
10−
1110#ifdef USE_REFLEX_OPTIMIZER
1211{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
1312#endif
@@ -34,6 +33,7 @@ module Reflex.Pure
3433import Control.Monad
3534import Data.Dependent.Map (DMap, GCompare)
3635import qualified Data.Dependent.Map as DMap
36+import Data.Functor.Identity
3737import Data.IntMap (IntMap)
3838import qualified Data.IntMap as IntMap
3939import Data.Maybe
@@ -88,8 +88,8 @@ instance (Enum t, HasTrie t, Ord t) => Reflex (Pure t) where
8888 then Nothing
8989 else Just currentOccurrences
9090
91− -- fanG :: GCompare k => Event (Pure t) (DMap k v) -> EventSelectorG (Pure t) k v
92− fanG e = EventSelectorG $ \k -> Event $ \t -> unEvent e t >>= DMap.lookup k
91+ fan :: GCompare k => Event (Pure t) (DMap k Identity) -> EventSelector (Pure t) k
92+ fan e = EventSelector $ \k -> Event $ \t -> unEvent e t >>= fmap runIdentity . DMap.lookup k
9393
9494 switch :: Behavior (Pure t) (Event (Pure t) a) -> Event (Pure t) a
9595 switch b = Event $ memo $ \t -> unEvent (unBehavior b t) t
src/Reflex/Spider/Internal.hs+24−26
@@ -20,7 +20,6 @@
2020{-# LANGUAGE DeriveFunctor #-}
2121{-# LANGUAGE StandaloneDeriving #-}
2222{-# LANGUAGE InstanceSigs #-}
23−
2423#ifdef USE_REFLEX_OPTIMIZER
2524{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
2625#endif
@@ -388,7 +387,7 @@ eventRoot !k !r = Event $ wrap eventSubscribedRoot $ liftIO . getRootSubscribed
388387eventNever :: Event x a
389388eventNever = Event $ \_ -> return (EventSubscription (return ()) eventSubscribedNever, Nothing)
390389
391−eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k v -> Event x (v a)
390+eventFan :: (GCompare k, HasSpiderTimeline x) => k a -> Fan x k -> Event x a
392391eventFan !k !f = Event $ wrap eventSubscribedFan $ getFanSubscribed k f
393392
394393eventSwitch :: HasSpiderTimeline x => Switch x a -> Event x a
@@ -440,14 +439,14 @@ newSubscriberHold h = return $ Subscriber
440439 , subscriberRecalculateHeight = \_ -> return ()
441440 }
442441
443−newSubscriberFan :: forall x k v. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k v -> IO (Subscriber x (DMap k v))
442+newSubscriberFan :: forall x k. (HasSpiderTimeline x, GCompare k) => FanSubscribed x k -> IO (Subscriber x (DMap k Identity))
444443newSubscriberFan subscribed = return $ Subscriber
445444 { subscriberPropagate = \a -> {-# SCC "traverseFan" #-} do
446445 subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
447446 tracePropagate (Proxy :: Proxy x) $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
448447 liftIO $ writeIORef (fanSubscribedOccurrence subscribed) $ Just a
449448 scheduleClear $ fanSubscribedOccurrence subscribed
450− let f _ (Pair v subsubs) = do
449+ let f _ (Pair (Identity v) subsubs) = do
451450 propagate v $ _fanSubscribedChildren_list subsubs
452451 return $ Constant ()
453452 _ <- DMap.traverseWithKey f $ DMap.intersectionWithKey (\_ -> Pair) a subs --TODO: Would be nice to have DMap.traverse_
@@ -600,7 +599,7 @@ eventSubscribedNever = EventSubscribed
600599#endif
601600 }
602601
603−eventSubscribedFan :: FanSubscribed x k v -> EventSubscribed x
602+eventSubscribedFan :: FanSubscribed x k -> EventSubscribed x
604603eventSubscribedFan !subscribed = EventSubscribed
605604 { eventSubscribedHeightRef = eventSubscribedHeightRef $ _eventSubscription_subscribed $ fanSubscribedParent subscribed
606605 , eventSubscribedRetained = toAny subscribed
@@ -1018,7 +1017,7 @@ data RootSubscribed x a = forall k. GCompare k => RootSubscribed
10181017#endif
10191018 }
10201019
1021−data Root x k
1020+data Root x (k :: * -> *)
10221021 = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
10231022 , rootSubscribed :: !(IORef (DMap k (RootSubscribed x)))
10241023 , rootInit :: !(forall a. k a -> RootTrigger x a -> IO (IO ()))
@@ -1088,25 +1087,25 @@ heightBagVerify b@(HeightBag s c) = if
10881087heightBagVerify = id
10891088#endif
10901089
1091−data FanSubscribedChildren x k v a = FanSubscribedChildren
1092− { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x (v a)))
1093− , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k v)
1094− , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k v)))
1090+data FanSubscribedChildren (x :: *) k a = FanSubscribedChildren
1091+ { _fanSubscribedChildren_list :: !(WeakBag (Subscriber x a))
1092+ , _fanSubscribedChildren_self :: {-# NOUNPACK #-} !(k a, FanSubscribed x k)
1093+ , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed x k)))
10951094 }
10961095
1097−data FanSubscribed x k v
1098− = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1099− , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k v)))
1100− , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k v))) -- This DMap should never be empty
1096+data FanSubscribed (x :: *) k
1097+ = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
1098+ , fanSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
1099+ , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren x k))) -- This DMap should never be empty
11011100 , fanSubscribedParent :: !(EventSubscription x)
11021101#ifdef DEBUG_NODEIDS
11031102 , fanSubscribedNodeId :: Int
11041103#endif
11051104 }
11061105
1107−data Fan x k v
1108− = Fan { fanParent :: !(Event x (DMap k v))
1109− , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k v)))
1106+data Fan x k
1107+ = Fan { fanParent :: !(Event x (DMap k Identity))
1108+ , fanSubscribed :: !(IORef (Maybe (FanSubscribed x k)))
11101109 }
11111110
11121111data SwitchSubscribed x a
@@ -1560,7 +1559,7 @@ fanInt p =
15601559 return (EventSubscription (FastWeakBag.remove t) $! EventSubscribed heightRef $! toAny (_fanInt_subscriptionRef self, t), IntMap.lookup k currentOcc)
15611560
15621561{-# INLINABLE getFanSubscribed #-}
1563−getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k v -> Subscriber x (v a) -> EventM x (WeakBagTicket, FanSubscribed x k v, Maybe (v a))
1562+getFanSubscribed :: (HasSpiderTimeline x, GCompare k) => k a -> Fan x k -> Subscriber x a -> EventM x (WeakBagTicket, FanSubscribed x k, Maybe a)
15641563getFanSubscribed k f sub = do
15651564 mSubscribed <- liftIO $ readIORef $ fanSubscribed f
15661565 case mSubscribed of
@@ -1594,7 +1593,7 @@ getFanSubscribed k f sub = do
15941593 liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
15951594 return (slnForSub, subscribed, coerce $ DMap.lookup k =<< parentOcc)
15961595
1597−cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k v) -> IO ()
1596+cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed x k) -> IO ()
15981597cleanupFanSubscribed (k, subscribed) = do
15991598 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
16001599 let reducedSubscribers = DMap.delete k subscribers
@@ -1606,7 +1605,7 @@ cleanupFanSubscribed (k, subscribed) = do
16061605 else writeIORef (fanSubscribedSubscribers subscribed) $! reducedSubscribers
16071606
16081607{-# INLINE subscribeFanSubscribed #-}
1609−subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k v -> Subscriber x (v a) -> IO WeakBagTicket
1608+subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed x k -> Subscriber x a -> IO WeakBagTicket
16101609subscribeFanSubscribed k subscribed sub = do
16111610 subscribers <- readIORef $ fanSubscribedSubscribers subscribed
16121611 case DMap.lookup k subscribers of
@@ -2082,15 +2081,14 @@ mergeIntCheap d = Event $ \sub -> do
20822081 )
20832082
20842083newtype EventSelector x k = EventSelector { select :: forall a. k a -> Event x a }
2085−newtype EventSelectorG x k v = EventSelectorG { selectG :: forall a. k a -> Event x (v a) }
20862084
2087−fanG :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k v) -> EventSelectorG x k v
2088−fanG e =
2085+fan :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k Identity) -> EventSelector x k
2086+fan e =
20892087 let f = Fan
20902088 { fanParent = e
20912089 , fanSubscribed = unsafeNewIORef e Nothing
20922090 }
2093− in EventSelectorG $ \k -> eventFan k f
2091+ in EventSelector $ \k -> eventFan k f
20942092
20952093runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
20962094runHoldInits holdInitRef dynInitRef mergeInitRef = do
@@ -2559,8 +2557,6 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25592557 pushCheap f = SpiderEvent . pushCheap (coerce f) . unSpiderEvent
25602558 {-# INLINABLE pull #-}
25612559 pull = SpiderBehavior . pull . coerce
2562− {-# INLINABLE fanG #-}
2563− fanG e = R.EventSelectorG $ SpiderEvent . selectG (fanG (unSpiderEvent e))
25642560 {-# INLINABLE mergeG #-}
25652561 mergeG
25662562 :: forall (k :: k2 -> *) q (v :: k2 -> *). GCompare k
@@ -2568,6 +2564,8 @@ instance HasSpiderTimeline x => R.Reflex (SpiderTimeline x) where
25682564 -> DMap k q
25692565 -> R.Event (SpiderTimeline x) (DMap k v)
25702566 mergeG nt = SpiderEvent . mergeG (unSpiderEvent #. nt) . dynamicConst
2567+ {-# INLINABLE fan #-}
2568+ fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
25712569 {-# INLINABLE switch #-}
25722570 switch = SpiderEvent . switch . (coerce :: Behavior x (R.Event (SpiderTimeline x) a) -> Behavior x (Event x a)) . unSpiderBehavior
25732571 {-# INLINABLE coincidence #-}
test/GC.hs+2−2
@@ -56,8 +56,8 @@ hostPerf ref = S.runSpiderHost $ do
5656 { S.subscriberPropagate = S.subscriberPropagate sub
5757 }
5858 return (s, o))
59− $ runIdentity . runIdentity <$> S.selectG
60− (S.fanG $ S.pushCheap (return . Just . mapKeyValuePairsMonotonic (\(t :=> e) -> WrapArg t :=> Identity e)) response)
59+ $ runIdentity <$> S.select
60+ (S.fan $ S.pushCheap (return . Just . mapKeyValuePairsMonotonic (\(t :=> e) -> WrapArg t :=> Identity e)) response)
6161 (WrapArg Request)
6262 return $ alignWith (mergeThese (<>))
6363 (flip S.pushCheap eadd $ \_ -> return $ Just $ DMap.singleton Request $ do