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

NFData1,NFData2 instances (#767) #992

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

Conversation

dbeacham
Copy link

@dbeacham dbeacham commented Feb 20, 2024

I've added NFData1 and NFData2 instances where appropriate for Data.Graph, Data.IntMap, Data.IntSet, Data.Map, Data.Sequence, Data.Set, Data.Tree and relevant internal dependencies

TODO:

  • Check build across GHC versions
  • Add @since docs

@meooow25
Copy link
Contributor

meooow25 commented Aug 2, 2024

This would be good to have. @dbeacham would you like to finish this PR?

@dbeacham
Copy link
Author

dbeacham commented Aug 2, 2024

Happy to - I'll finish it up next week.

@dbeacham dbeacham force-pushed the nfdata1-nfdata2-instances branch 2 times, most recently from 83e4566 to 57cc685 Compare August 20, 2024 12:10
liftRnf _ Nil = ()
liftRnf rnfx (Tip _ v) = rnfx v
liftRnf rnfx (Bin _ l r) = liftRnf rnfx l `seq` liftRnf rnfx r

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use a helper function to allow this to inline?

Copy link
Author

Choose a reason for hiding this comment

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

I've added one here - but possibly there are other instances that could benefit?

Also not sure if there's any need for any INLINE or INLINABLE pragmas.

Copy link
Contributor

Choose a reason for hiding this comment

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

I probably wouldn't add pragmas for this. The function is small enough that I'd expect GHC to do a decent job of deciding when to inline it. Letting others inline would probably be good too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seq will be tricky to allow inlining for (it requires a GADT, or better a fake GADT, and I don't think we have one here), so don't worry too much about that.

Copy link
Author

Choose a reason for hiding this comment

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

I think that's now done for Map, Set, IntMap and Tree.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like it would be a good to have a ()-like type with a strict <> instance, then we can write all NFData stuff simply using foldMap and foldMapWithKey instead of rewriting tree traversals every time.

Copy link
Contributor

Choose a reason for hiding this comment

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

@meooow25 , that sounds clever.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wanted to see if this exists in/could be added to deepseq, and it seems like you made an attempt a while ago: haskell/deepseq#18 😄
It would be handy to have the types UnitLTR/UnitRTL available from deepseq, but for now we'll have to define our own if we do this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@meooow25 meooow25 left a comment

Choose a reason for hiding this comment

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

You could try the foldMap thing if you like, if not this looks good to me.

Comment on lines +48 to +49
* `NFData1`, `NFData2` instances for `Data.Graph`, `Data.IntMap`,
`Data.IntSet`, `Data.Map`, `Data.Sequence`, `Data.Set`, `Data.Tree` and
Copy link
Contributor

Choose a reason for hiding this comment

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

Data.Graph could be misunderstood, SCC got the instance (not Graph). And IntSet is not affected.
We can also just drop the Data. and say "...for IntMap, Map, Set, Tree, SCC", I think that's good enough.

@treeowl
Copy link
Contributor

treeowl commented Aug 22, 2024

Speaking of changes, we usually use @since TODO because we don't usually know what the actual next version number will be very far in advance. But if @meooow25 wants to make a release imminently, this could be fine.

@meooow25 meooow25 mentioned this pull request Oct 31, 2024
8 tasks
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.

3 participants