Skip to content

Instantly share code, notes, and snippets.

@ndmitchell
Created May 26, 2020 07:39
Show Gist options
  • Save ndmitchell/7c62c03cc0966ebfa108e07e0549943a to your computer and use it in GitHub Desktop.
Save ndmitchell/7c62c03cc0966ebfa108e07e0549943a to your computer and use it in GitHub Desktop.
setValues state key file val = modifyVar_ state $ \vals -> do
-- Force to make sure the old HashMap is not retained
evaluate val
let v = fmap toDyn val
_ <- evaluate v
let k = (file, Key key)
weak <- case HMap.lookup k vals of
Nothing -> pure Nothing
Just v -> do
evaluate v
Just <$> mkWeakPtr v Nothing
res <- evaluate $ HMap.insert k v vals
performGC
whenJust weak $ \w -> do
b <- deRefWeak w
when (isJust b) $
print "DEREF VALUE STILL LIVES"
-- This odd construction is required to prevent a space leak.
-- We insert a fully evaluated value, but, somehow, the HashMap
-- seems to keep track of the old value. I don't know how.
-- Is there a space-leak in unordered-containers?
-- The evaluate below fixes it, at the cost of walking the
-- HashMap a second time.
-- Without this evaluate, leaks of 30Mb/min have been observed.
case HMap.lookup k res of
Nothing -> pure ()
Just v -> evaluate v >> pure () -- void $ evaluate v
performGC
whenJust weak $ \w -> do
b <- deRefWeak w
when (isJust b) $
print "CONTINUES"
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment