@@ -211,6 +211,12 @@ nextNodeIdRef = unsafePerformIO $ newIORef 1
211211
212212newNodeId :: IO Int
213213newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
214+
215+{-# NOINLINE unsafeNodeId #-}
216+unsafeNodeId :: a -> Int
217+unsafeNodeId a = unsafePerformIO $ do
218+ touch a
219+ newNodeId
214220#endif
215221
216222--------------------------------------------------------------------------------
@@ -314,10 +320,9 @@ cacheEvent e =
314320#else
315321 Event $
316322#endif
317− unsafePerformIO $ do
318− mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
319− <- newIORef emptyFastWeak
320− pure $ \sub -> {-# SCC "cacheEvent" #-} do
323+ let mSubscribedRef :: IORef (FastWeak (CacheSubscribed x a))
324+ !mSubscribedRef = unsafeNewIORef e emptyFastWeak
325+ in \sub -> {-# SCC "cacheEvent" #-} do
321326#ifdef DEBUG_TRACE_EVENTS
322327 unless (BS8.null callSite) $ liftIO $ BS8.hPutStrLn stderr callSite
323328#endif
@@ -1206,12 +1211,18 @@ buildDynamic readV0 v' = do
12061211 return d
12071212
12081213unsafeBuildDynamic :: BehaviorM x (PatchTarget p) -> Event x p -> Dyn x p
1209−unsafeBuildDynamic readV0 v' =
1210− Dyn $ unsafePerformIO $ newIORef $ UnsafeDyn (readV0, v')
1214+unsafeBuildDynamic readV0 v' = Dyn $ unsafeNewIORef x $ UnsafeDyn x
1215+ where x = (readV0, v')
12111216
12121217-- ResultM can read behaviors and events
12131218type ResultM = EventM
12141219
1220+{-# NOINLINE unsafeNewIORef #-}
1221+unsafeNewIORef :: a -> b -> IORef b
1222+unsafeNewIORef a b = unsafePerformIO $ do
1223+ touch a
1224+ newIORef b
1225+
12151226instance HasSpiderTimeline x => Functor (Event x) where
12161227 fmap f = push $ return . Just . f
12171228
@@ -1224,35 +1235,26 @@ push f e = cacheEvent (pushCheap f e)
12241235
12251236{-# INLINABLE pull #-}
12261237pull :: BehaviorM x a -> Behavior x a
1227−pull a = unsafePerformIO $ do
1228− ref <- newIORef Nothing
1238+pull a = behaviorPull $ Pull
1239+ { pullCompute = a
1240+ , pullValue = unsafeNewIORef a Nothing
12291241#ifdef DEBUG_NODEIDS
1230− nid <- newNodeId
1242+ , pullNodeId = unsafeNodeId a
12311243#endif
1232− pure $ behaviorPull $ Pull
1233− { pullCompute = a
1234− , pullValue = ref
1235−#ifdef DEBUG_NODEIDS
1236− , pullNodeId = nid
1237−#endif
1238− }
1244+ }
12391245
12401246{-# INLINABLE switch #-}
12411247switch :: HasSpiderTimeline x => Behavior x (Event x a) -> Event x a
1242−switch a = unsafePerformIO $ do
1243− ref <- newIORef Nothing
1244− pure $ eventSwitch $ Switch
1245− { switchParent = a
1246− , switchSubscribed = ref
1247− }
1248+switch a = eventSwitch $ Switch
1249+ { switchParent = a
1250+ , switchSubscribed = unsafeNewIORef a Nothing
1251+ }
12481252
12491253coincidence :: HasSpiderTimeline x => Event x (Event x a) -> Event x a
1250−coincidence a = unsafePerformIO $ do
1251− ref <- newIORef Nothing
1252− pure $ eventCoincidence $ Coincidence
1253− { coincidenceParent = a
1254− , coincidenceSubscribed = ref
1255− }
1254+coincidence a = eventCoincidence $ Coincidence
1255+ { coincidenceParent = a
1256+ , coincidenceSubscribed = unsafeNewIORef a Nothing
1257+ }
12561258
12571259-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
12581260run :: forall x b. HasSpiderTimeline x => [DSum (RootTrigger x) Identity] -> ResultM x b -> SpiderHost x b
@@ -1456,9 +1458,6 @@ getRootSubscribed k r sub = do
14561458 when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
14571459 uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
14581460 writeIORef uninitRef $! uninit
1459−#ifdef DEBUG_NODEIDS
1460− nid <- newNodeId
1461−#endif
14621461 let !subscribed = RootSubscribed
14631462 { rootSubscribedKey = k
14641463 , rootSubscribedCachedSubscribed = cached
@@ -1467,7 +1466,7 @@ getRootSubscribed k r sub = do
14671466 , rootSubscribedUninit = uninit
14681467 , rootSubscribedWeakSelf = weakSelf
14691468#ifdef DEBUG_NODEIDS
1470− , rootSubscribedNodeId = nid
1469+ , rootSubscribedNodeId = unsafeNodeId (k, r, subs)
14711470#endif
14721471 }
14731472 -- If we die at the same moment that all our children die, they will
@@ -1516,10 +1515,16 @@ newFanInt = do
15161515 , _fanInt_occRef = occRef
15171516 }
15181517
1518+{-# NOINLINE unsafeNewFanInt #-}
1519+unsafeNewFanInt :: b -> FanInt x a
1520+unsafeNewFanInt b = unsafePerformIO $ do
1521+ touch b
1522+ newFanInt
1523+
15191524fanInt :: HasSpiderTimeline x => Event x (IntMap a) -> EventSelectorInt x a
1520−fanInt p = unsafePerformIO $ do
1521− self <- newFanInt
1522− pure $ EventSelectorInt $ \k -> Event $ \sub -> do
1525+fanInt p =
1526+ let self = unsafeNewFanInt p
1527+ in EventSelectorInt $ \k -> Event $ \sub -> do
15231528 isEmpty <- liftIO $ FastMutableIntMap.isEmpty (_fanInt_subscribers self)
15241529 when isEmpty $ do -- This is the first subscriber, so we need to subscribe to our input
15251530 (subscription, parentOcc) <- subscribeAndRead p $ Subscriber
@@ -1573,16 +1578,13 @@ getFanSubscribed k f sub = do
15731578 subscribersRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribersRef not yet initialized"
15741579 occRef <- liftIO $ newIORef parentOcc
15751580 when (isJust parentOcc) $ scheduleClear occRef
1576−#ifdef DEBUG_NODEIDS
1577− nid <- liftIO newNodeId
1578−#endif
15791581 let subscribed = FanSubscribed
15801582 { fanSubscribedCachedSubscribed = fanSubscribed f
15811583 , fanSubscribedOccurrence = occRef
15821584 , fanSubscribedParent = subscription
15831585 , fanSubscribedSubscribers = subscribersRef
15841586#ifdef DEBUG_NODEIDS
1585− , fanSubscribedNodeId = nid
1587+ , fanSubscribedNodeId = unsafeNodeId f
15861588#endif
15871589 }
15881590 let !self = (k, subscribed)
@@ -1642,9 +1644,6 @@ getSwitchSubscribed s sub = do
16421644 when (isJust parentOcc) $ scheduleClear occRef
16431645 weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
16441646 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1645−#ifdef DEBUG_NODEIDS
1646− nid <- liftIO newNodeId
1647−#endif
16481647 let !subscribed = SwitchSubscribed
16491648 { switchSubscribedCachedSubscribed = switchSubscribed s
16501649 , switchSubscribedOccurrence = occRef
@@ -1657,7 +1656,7 @@ getSwitchSubscribed s sub = do
16571656 , switchSubscribedCurrentParent = subscriptionRef
16581657 , switchSubscribedWeakSelf = weakSelf
16591658#ifdef DEBUG_NODEIDS
1660− , switchSubscribedNodeId = nid
1659+ , switchSubscribedNodeId = unsafeNodeId s
16611660#endif
16621661 }
16631662 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
@@ -1702,9 +1701,6 @@ getCoincidenceSubscribed c sub = do
17021701 scheduleClear innerSubdRef
17031702 weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
17041703 (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1705−#ifdef DEBUG_NODEIDS
1706− nid <- liftIO newNodeId
1707−#endif
17081704 let subscribed = CoincidenceSubscribed
17091705 { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
17101706 , coincidenceSubscribedOccurrence = occRef
@@ -1715,7 +1711,7 @@ getCoincidenceSubscribed c sub = do
17151711 , coincidenceSubscribedInnerParent = innerSubdRef
17161712 , coincidenceSubscribedWeakSelf = weakSelf
17171713#ifdef DEBUG_NODEIDS
1718− , coincidenceSubscribedNodeId = nid
1714+ , coincidenceSubscribedNodeId = unsafeNodeId c
17191715#endif
17201716 }
17211717 liftIO $ writeIORef weakSelf =<< evaluate =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
@@ -2089,13 +2085,12 @@ newtype EventSelector x k = EventSelector { select :: forall a. k a -> Event x a
20892085newtype EventSelectorG x k v = EventSelectorG { selectG :: forall a. k a -> Event x (v a) }
20902086
20912087fanG :: (HasSpiderTimeline x, GCompare k) => Event x (DMap k v) -> EventSelectorG x k v
2092−fanG e = unsafePerformIO $ do
2093− ref <- newIORef Nothing
2088+fanG e =
20942089 let f = Fan
20952090 { fanParent = e
2096− , fanSubscribed = ref
2091+ , fanSubscribed = unsafeNewIORef e Nothing
20972092 }
2098− pure $ EventSelectorG $ \k -> eventFan k f
2093+ in EventSelectorG $ \k -> eventFan k f
20992094
21002095runHoldInits :: HasSpiderTimeline x => IORef [SomeHoldInit x] -> IORef [SomeDynInit x] -> IORef [SomeMergeInit x] -> EventM x ()
21012096runHoldInits holdInitRef dynInitRef mergeInitRef = do