optimize createFpAndTrim with shrinkMutableByteArray##576
Open
oberblastmeister wants to merge 2 commits intohaskell:masterfrom
Open
optimize createFpAndTrim with shrinkMutableByteArray##576oberblastmeister wants to merge 2 commits intohaskell:masterfrom
oberblastmeister wants to merge 2 commits intohaskell:masterfrom
Conversation
Member
|
It looks like GHC indeed behaves poorly when shrinking large |
clyring
reviewed
Feb 16, 2023
| unsafeWithForeignPtr fq $ \q -> memcpy p q s | ||
|
|
||
| shrinkFp :: ForeignPtr Word8 -> Int -> IO () | ||
| shrinkFp (ForeignPtr _ (PlainPtr marr)) (I# l#) = |
Member
Contributor
There was a problem hiding this comment.
I'd write shrinkFp _ _ = pure () instead of error, but otherwise looks sane to me as is.
| if assert (0 <= l' && l' <= l) $ l' >= l | ||
| then return $! BS fp l | ||
| else | ||
| if l < 4096 |
Member
There was a problem hiding this comment.
This condition is the main tricky bit.
- Large
MutableByteArray#s do not return their memory to the RTS when we shrink them in-place. But they do return their memory to the RTS when we make a copy. - Small pinned
MutableByteArray#s may well retain all of their memory regardless of whether we shrink in place or create a copy, because of how they are currently allocated in GHC. (There are probably several GHC issues related to this topic. See for example this one and this one. It's not clear if or when this will be improved.)
So we want to make a copy when:
- We expect the underlying buffer to be a large heap object, and
- the amount of space shrinking would leak is large enough to care about.
If I recall correctly, the exact threshold for when a buffer is allocated as a large heap object depends on both the platform and the GHC version, but has been somewhere around 3K for 64-bit systems and half that for 32-bit systems for some time now.
(This reasoning should be documented in the code.)
Contributor
|
@oberblastmeister could you please rebase? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #549