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

Make use of shrinkSmallMutableArray# primop #362

Closed
sjakobi opened this issue Mar 3, 2022 · 5 comments
Closed

Make use of shrinkSmallMutableArray# primop #362

sjakobi opened this issue Mar 3, 2022 · 5 comments

Comments

@sjakobi
Copy link
Member

sjakobi commented Mar 3, 2022

Since GHC 8.10 there is a shrinkSmallMutableArray# primop that may be useful for functions like filter and mapMaybe. It would allow us to perform deletions in-place instead of creating a new array.

@treeowl
Copy link
Collaborator

treeowl commented Mar 3, 2022

Ooh, good thinking!

@sjakobi
Copy link
Member Author

sjakobi commented Mar 8, 2022

We may be able to use it wherever we're currently using trim:

-- | Create a new array of the @n@ first elements of @mary@.
trim :: MArray s a -> Int -> ST s (Array a)
trim mary n = cloneM mary 0 n >>= unsafeFreeze
{-# INLINE trim #-}

@sjakobi
Copy link
Member Author

sjakobi commented Apr 15, 2022

#406 has introduced shrink:

-- | When 'Exts.shrinkSmallMutableArray#' is available, the returned array is the same as the array given, as it is shrunk in place.
-- Otherwise a copy is made.
shrink :: MArray s a -> Int -> ST s (MArray s a)
#if __GLASGOW_HASKELL__ >= 810
shrink mary _n@(I# n#) =
CHECK_GT("shrink", _n, (0 :: Int))
CHECK_LE("shrink", _n, (lengthM mary))
ST $ \s -> case Exts.shrinkSmallMutableArray# (unMArray mary) n# s of
s' -> (# s', mary #)
#else
shrink mary n = cloneM mary 0 n
#endif
{-# INLINE shrink #-}

So far it's only used in intersection[With[Key]] though.

sjakobi added a commit that referenced this issue Apr 24, 2022
This results in a ~8% speedup in the filterWithKey benchmark.

Context: #362
sjakobi added a commit that referenced this issue Apr 24, 2022
This results in a ~8% speedup in the filterWithKey benchmark.

Context: #362
sjakobi added a commit that referenced this issue Apr 25, 2022
This results in a ~8% speedup in the filterWithKey benchmark.

Context: #362
@sjakobi
Copy link
Member Author

sjakobi commented Apr 30, 2022

With #433 we're using shrink in filter and mapMaybe now. #435 refactors updateOrConcatWithKey to use shrink. I can't find any obvious other spots where shrink would be useful.

@sjakobi sjakobi closed this as completed Apr 30, 2022
@sjakobi
Copy link
Member Author

sjakobi commented Apr 30, 2022

For reference: some performance aspects of shrinkSmallMutableArray# are still unclear: #441

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

No branches or pull requests

2 participants