Skip to content

Commit 766d172

Browse files
committed
Various NoThunks fixes for OrdinaryIndex
1 parent 45ca97f commit 766d172

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

src-extras/Database/LSMTree/Extras/NoThunks.hs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,31 @@ deriving anyclass instance Typeable s
460460
GrowingVector
461461
-------------------------------------------------------------------------------}
462462

463-
deriving stock instance Generic (GrowingVector s a)
464-
deriving anyclass instance (Typeable s, Typeable a, NoThunks a)
465-
=> NoThunks (GrowingVector s a)
463+
instance (NoThunks a, Typeable s, Typeable a) => NoThunks (GrowingVector s a) where
464+
showTypeOf (p :: Proxy (GrowingVector s a)) = show $ typeRep p
465+
wNoThunks ctx
466+
(GrowingVector (a :: STRef s (VM.MVector s a)) (b :: PrimVar s Int))
467+
= allNoThunks [
468+
noThunks ctx b
469+
-- Check that the STRef is in WHNF
470+
, noThunks ctx $ OnlyCheckWhnf a
471+
-- Check that the MVector is in WHNF
472+
, do
473+
mvec <- unsafeSTToIO $ readSTRef a
474+
noThunks ctx' $ OnlyCheckWhnf mvec
475+
-- Check that the vector elements contain no thunks. The vector
476+
-- contains undefined elements after the first @n@ elements
477+
, do
478+
n <- unsafeSTToIO $ readPrimVar b
479+
mvec <- unsafeSTToIO $ readSTRef a
480+
allNoThunks [
481+
unsafeSTToIO (VM.read mvec i) >>= \x -> noThunks ctx'' x
482+
| i <- [0..n-1]
483+
]
484+
]
485+
where
486+
ctx' = showTypeOf (Proxy @(STRef s (VM.MVector s a))) : ctx
487+
ctx'' = showTypeOf (Proxy @(VM.MVector s a)) : ctx'
466488

467489
{-------------------------------------------------------------------------------
468490
Baler
@@ -662,15 +684,21 @@ instance (NoThunks a, Typeable s, Typeable a) => NoThunks (MutableHeap s a) wher
662684
(MH (a :: PrimVar s Int) (b :: SmallMutableArray s a))
663685
= allNoThunks [
664686
noThunks ctx a
665-
-- the small array may contain bogus/undefined placeholder values
666-
-- after the first @n@ elements in the heap
667-
, noThunks ctx $! do
687+
-- Check that the array is in WHNF
688+
, noThunks ctx (OnlyCheckWhnf b)
689+
-- Check that the array elements contain no thunks. The small array
690+
-- may contain undefined placeholder values after the first @n@
691+
-- elements in the array. The very first element of the array can also
692+
-- be undefined.
693+
, do
668694
n <- unsafeSTToIO (readPrimVar a)
669695
allNoThunks [
670-
unsafeSTToIO (readSmallArray b i) >>= \x -> noThunks ctx x
671-
| i <- [0..n-1]
696+
unsafeSTToIO (readSmallArray b i) >>= \x -> noThunks ctx' x
697+
| i <- [1..n-1]
672698
]
673699
]
700+
where
701+
ctx' = showTypeOf (Proxy @(SmallMutableArray s a)) : ctx
674702

675703
{-------------------------------------------------------------------------------
676704
IOLike

src/Database/LSMTree/Internal/Index.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ fromSBS Ordinary input = second OrdinaryIndex <$> Ordinary.fromSBS input
154154
Incremental index construction is only guaranteed to work correctly when the
155155
supplied key ranges do not overlap and are given in ascending order.
156156
-}
157-
data IndexAcc s = CompactIndexAcc (IndexCompactAcc s)
158-
| OrdinaryIndexAcc (IndexOrdinaryAcc s)
157+
data IndexAcc s = CompactIndexAcc !(IndexCompactAcc s)
158+
| OrdinaryIndexAcc !(IndexOrdinaryAcc s)
159159

160160
-- | Create a new index accumulator, using a default configuration.
161161
newWithDefaults :: IndexType -> ST s (IndexAcc s)

src/Database/LSMTree/Internal/Index/Ordinary.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fromSBS shortByteString@(SBS unliftedByteArray)
234234
= Primitive.splitAt firstSize postFirstSizeBytes
235235

236236
first :: SerialisedKey
237-
first = SerialisedKey' (Primitive.force firstBytes)
237+
!first = SerialisedKey' (Primitive.force firstBytes)
238238

239239
others <- lastKeys othersBytes
240240
return (first : others)

src/Database/LSMTree/Internal/Vector/Growing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ append (GrowingVector bufferRef lengthRef) count val
7070
length <- readPrimVar lengthRef
7171
makeRoom
7272
buffer' <- readSTRef bufferRef
73-
Mutable.set (Mutable.slice length count buffer') val
73+
Mutable.set (Mutable.slice length count buffer') $! val
7474
where
7575

7676
makeRoom :: ST s ()

0 commit comments

Comments
 (0)