-
Notifications
You must be signed in to change notification settings - Fork 59
Make fromListN functions good consumers #424
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -586,18 +586,28 @@ mapArray' f a = | |
|
|
||
| -- | Create an array from a list of a known length. If the length | ||
| -- of the list does not match the given length, this throws an exception. | ||
|
|
||
| -- Note [fromListN] | ||
| -- ~~~~~~~~~~~~~~~~ | ||
| -- We want arrayFromListN to be a "good consumer" in list fusion, so we define | ||
| -- the function using foldr and inline it to help fire fusion rules. | ||
| -- If fusion occurs with a "good producer", it may reduce to a fold on some | ||
| -- structure. In certain cases (such as for Data.Set) GHC is not be able to | ||
| -- optimize the index to an unboxed Int# (see GHC #24628), so we explicitly use | ||
| -- an Int# here. | ||
| arrayFromListN :: Int -> [a] -> Array a | ||
| {-# INLINE arrayFromListN #-} | ||
| arrayFromListN n l = | ||
| createArray n (die "fromListN" "uninitialized element") $ \sma -> | ||
| let go !ix [] = if ix == n | ||
| let z ix# = if I# ix# == n | ||
| then return () | ||
| else die "fromListN" "list length less than specified size" | ||
| go !ix (x : xs) = if ix < n | ||
| f x k = GHC.Exts.oneShot $ \ix# -> if I# ix# < n | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andrewthad You didn't need to drop support for ghc-8.0 in #426, since
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I would've needed to add the dependency on ghc-prim back to primitive, and it was so nice to finally have it gone. Dropping GHC 8.0 has the additional advantage of letting me delete several shims in #427. I'll probably end up dropping support for all GHCs earlier than 8.6 soon. |
||
| then do | ||
| writeArray sma ix x | ||
| go (ix+1) xs | ||
| writeArray sma (I# ix#) x | ||
| k (ix# +# 1#) | ||
| else die "fromListN" "list length greater than specified size" | ||
| in go 0 l | ||
| in foldr f z l 0# | ||
|
|
||
| -- | Create an array from a list. | ||
| arrayFromList :: [a] -> Array a | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is best to move this sentence ("
In certain cases (such as for Data.Set) GHC is not be able to ...") to a comment into the body of the function, since that is not relevant information for the user.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which "user" do you mean? This comment block is not part of the Haddocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, you are right. I did not notice an empty new line on line number 589 between the haddock and the comment.