Skip to content

Commit 2c7a183

Browse files
committed
Prevent NoThunks failures using strict(er) vector operations
1 parent 060a04c commit 2c7a183

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Database/LSMTree/Internal/MergeSchedule.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Database.LSMTree.Internal.RunNumber
6060
import Database.LSMTree.Internal.Serialise (SerialisedBlob,
6161
SerialisedKey, SerialisedValue)
6262
import Database.LSMTree.Internal.UniqCounter
63-
import Database.LSMTree.Internal.Vector (mapStrict)
63+
import Database.LSMTree.Internal.Vector (forMStrict, mapStrict)
6464
import Database.LSMTree.Internal.WriteBuffer (WriteBuffer)
6565
import qualified Database.LSMTree.Internal.WriteBuffer as WB
6666
import Database.LSMTree.Internal.WriteBufferBlobs (WriteBufferBlobs)
@@ -202,7 +202,7 @@ mkLevelsCache reg lvls = do
202202
-> Levels m h
203203
-> m a
204204
foldRunAndMergeM k1 k2 ls =
205-
fmap fold $ V.forM ls $ \(Level ir rs) -> do
205+
fmap fold $ forMStrict ls $ \(Level ir rs) -> do
206206
incoming <- case ir of
207207
Single r -> k1 r
208208
Merging _ mr -> k2 mr
@@ -253,7 +253,7 @@ duplicateLevelsCache ::
253253
-> LevelsCache m h
254254
-> m (LevelsCache m h)
255255
duplicateLevelsCache reg cache = do
256-
rs' <- V.forM (cachedRuns cache) $ \r ->
256+
rs' <- forMStrict (cachedRuns cache) $ \r ->
257257
withRollback reg (dupRef r) releaseRef
258258
return cache { cachedRuns = rs' }
259259

@@ -302,9 +302,9 @@ duplicateLevels ::
302302
-> Levels m h
303303
-> m (Levels m h)
304304
duplicateLevels reg levels =
305-
V.forM levels $ \Level {incomingRun, residentRuns} -> do
305+
forMStrict levels $ \Level {incomingRun, residentRuns} -> do
306306
incomingRun' <- duplicateIncomingRun reg incomingRun
307-
residentRuns' <- V.forM residentRuns $ \r ->
307+
residentRuns' <- forMStrict residentRuns $ \r ->
308308
withRollback reg (dupRef r) releaseRef
309309
return $! Level {
310310
incomingRun = incomingRun',

src/Database/LSMTree/Internal/Vector.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Database.LSMTree.Internal.Vector (
99
mapStrict,
1010
mapMStrict,
1111
imapMStrict,
12+
forMStrict,
1213
zipWithStrict,
1314
binarySearchL,
1415
unsafeInsertWithMStrict,
@@ -79,6 +80,11 @@ imapMStrict f v = V.imapM (\i -> f i >=> (pure $!)) v
7980
zipWithStrict :: forall a b c. (a -> b -> c) -> V.Vector a -> V.Vector b -> V.Vector c
8081
zipWithStrict f xs ys = runST (V.zipWithM (\x y -> pure $! f x y) xs ys)
8182

83+
-- | /( O(n) /) Like 'V.forM', but strict in the produced elements of type @b@.
84+
{-# INLINE forMStrict #-}
85+
forMStrict :: Monad m => V.Vector a -> (a -> m b) -> m (V.Vector b)
86+
forMStrict xs f = V.forM xs (f >=> (pure $!))
87+
8288
{-|
8389
Finds the lowest index in a given sorted vector at which the given element
8490
could be inserted while maintaining the sortedness.

0 commit comments

Comments
 (0)