Skip to content

Commit 54bcfdd

Browse files
authored
Improve the documentation on the regression test
1 parent d1d0151 commit 54bcfdd

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

tests/Regressions.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,37 @@ mapFromKeys keys = HM.fromList (zip keys (repeat ()))
7979
------------------------------------------------------------------------
8080
-- Issue #254
8181

82-
data KC = KC Int
82+
-- Key type that always collides.
83+
newtype KC = KC Int
8384
deriving (Eq, Ord, Show)
8485
instance Hashable KC where
8586
hashWithSalt salt _ = salt
8687

8788
issue254Lazy :: Assertion
8889
issue254Lazy = issue254LazyLambda 2
8990

90-
-- Important that oldV is not hoisted out by optimisation, so use NOINLINE
91+
-- We want to make sure that old values in the HashMap are evicted when new values are inserted,
92+
-- even if they aren't evaluated. To do that, we use the WeakPtr trick described at
93+
-- http://simonmar.github.io/posts/2018-06-20-Finding-fixing-space-leaks.html.
94+
-- We insert a value named oldV into the HashMap, then insert over it, checking oldV is no longer reachable.
95+
--
96+
-- To make the test robust, it's important that oldV isn't hoisted up to the top or shared. To do that,
97+
-- we use NOINLINE, make oldV dependent on an unseen argument, and insert _ <- return () to ensure oldV
98+
-- is under a lambda.
9199
{-# NOINLINE issue254LazyLambda #-}
92100
issue254LazyLambda :: Int -> Assertion
93101
issue254LazyLambda i = do
94102
_ <- return ()
95-
let oldV = show i
103+
let oldV = error $ "Should not be evaluated: " ++ show i
96104
weakV <- mkWeakPtr oldV Nothing
97-
let mp = HML.insert (KC 1) "3" $ HML.fromList [(KC 0, "1"), (KC 1, oldV)]
105+
let mp = HML.insert (KC 1) (error "Should not be evaluated") $ HML.fromList [(KC 0, "1"), (KC 1, oldV)]
98106
_ <- evaluate mp
99107
performGC
100108
res <- deRefWeak weakV
101109
_ <- evaluate mp
102110
assert $ isNothing res
103111

112+
-- Like issue254Lazy, but using strict HashMap
104113
issue254Strict :: Assertion
105114
issue254Strict = issue254StrictLambda 2
106115

0 commit comments

Comments
 (0)