Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize some cut properties in the cut record #2086

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

larskuhtz
Copy link
Contributor

No description provided.

@@ -357,6 +391,12 @@ projectChains m = HM.intersection m
$ chainIdsAt (cutHeadersChainwebVersion m) (cutHeadersMinHeight m)
{-# INLINE projectChains #-}

cutProjectChains :: Cut -> Cut
cutProjectChains c = unsafeMkCut v $! projectChains $ _cutHeaders c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This $! is unnecessary. unsafeMkCut is strict in its second input, because _cutHeaders' is a strict field.

Comment on lines 711 to +715
True -> return $ Just
$! over unsafeCutHeaders extendChains
$ set (unsafeCutHeaders . ix' (_chainId h)) h c
$! unsafeMkCut v
$! extendChains
$ set (ix' (_chainId h)) h
$ _cutHeaders c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the result strict, we have to do this:

return $! Just
  $! unsafeMkCut v
  $ extendChains
  $ set (ix' (_chainId h)) h
  $ _cutHeaders c

Because return is lazy, Just is lazy, and extendChains and set (ix' ...) ... are both dealing with HashMaps, which cannot have laziness anywhere but their elements.

-> Cut
-> IO Cut
limitCut wdb h c
| all (\bh -> h >= view blockHeight bh) (view cutHeaders c) =
return c
| otherwise = do
hdrs <- itraverse go $ view cutHeaders c
return $! set unsafeCutHeaders (projectChains $ HM.mapMaybe id hdrs) c
return $! unsafeMkCut v $! projectChains $ HM.mapMaybe id hdrs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return $! unsafeMkCut v $! projectChains $ HM.mapMaybe id hdrs
return $! unsafeMkCut v $ projectChains $ HM.mapMaybe id hdrs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because unsafeMkCut is strict already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants