Skip to content

Conversation

@Bodigrim
Copy link
Contributor

basicUnsafe{Read/Write/IndexM} are class members and, unless a call site was already specialised to a specific vector instance, GHC has no clue that the index is most certainly to be used eagerly. Bang before the index provides this vital for optimizer information.

I've been recently looking at Core for programs, where vectors did not specialise to a specific instance for one reason or another. It's really horrible: even if a specific instance remains unknown, we should tell GHC that passing boxed indices around is not OK.

@Bodigrim Bodigrim force-pushed the indices-are-strict branch from 5de58ff to 9f21834 Compare March 11, 2024 20:16
@Shimuuar Shimuuar self-requested a review March 11, 2024 21:21
Copy link
Contributor

@Shimuuar Shimuuar left a comment

Choose a reason for hiding this comment

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

Great find! I think slice/unsafeSlice would benefit from the same treatment as well.

P.S. Out of curiosity I looking into GHC's timing information. This change reduced allocation by simplifier by about 1e-3 when compiling unboxed vectors.

{-# INLINE unsafeRead #-}
unsafeRead v i = checkIndex Unsafe i (length v)
unsafeRead v !i = checkIndex Unsafe i (length v)
$ stToPrim

Choose a reason for hiding this comment

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

I 100% agree with this change, but unfortunately it means the following lines are no longer indented correctly with respect to the =. Is that something that can be fixed as part of this PR?

{-# INLINE unsafeWrite #-}
unsafeWrite v i x = checkIndex Unsafe i (length v)
unsafeWrite v !i x = checkIndex Unsafe i (length v)
$ stToPrim

Choose a reason for hiding this comment

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

Same comment as above.

{-# INLINE unsafeModify #-}
unsafeModify v f i = checkIndex Unsafe i (length v)
unsafeModify v f !i = checkIndex Unsafe i (length v)
$ stToPrim

Choose a reason for hiding this comment

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

Same comment as above.

{-# INLINE unsafeModifyM #-}
unsafeModifyM v f i = checkIndex Unsafe i (length v)
unsafeModifyM v f !i = checkIndex Unsafe i (length v)
$ stToPrim . basicUnsafeWrite v i =<< f =<< stToPrim (basicUnsafeRead v i)

Choose a reason for hiding this comment

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

Same comment as above.

@Shimuuar
Copy link
Contributor

@Bodigrim I extended same treatment to unsafeSlice and converted comment to GHC note. This seems to be a good format for long form comment which are referenced from multiple places in source code

If you're fine with this I'd like to merge this PR

@Bodigrim
Copy link
Contributor Author

@Shimuuar sure, go ahead.

Bodigrim and others added 4 commits March 20, 2024 21:24
'basicUnsafe{Read/Write/IndexM}' are class members and, unless a call site was
already specialised to a specific vector instance, GHC has no clue that the index
is most certainly to be used eagerly. Bang before the index provides
this vital for optimizer information.
We have longish comment which is referenced from multiple places in source
code. GHC notes seems good option for that
@Shimuuar Shimuuar force-pushed the indices-are-strict branch from 96ee359 to e13cb00 Compare March 20, 2024 18:24
@Shimuuar Shimuuar merged commit 655b5d6 into master Mar 20, 2024
@Shimuuar Shimuuar deleted the indices-are-strict branch March 20, 2024 18:54
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.

4 participants