-
Notifications
You must be signed in to change notification settings - Fork 141
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
optimize createFpAndTrim with shrinkMutableByteArray# #576
Open
oberblastmeister
wants to merge
2
commits into
haskell:master
Choose a base branch
from
oberblastmeister:shrink
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,7 @@ import Data.Word | |
import Data.Data (Data(..), mkNoRepType) | ||
|
||
import GHC.Base (nullAddr#,realWorld#,unsafeChr) | ||
import GHC.Exts (IsList(..)) | ||
import GHC.Exts (IsList(..), shrinkMutableByteArray#) | ||
import GHC.CString (unpackCString#) | ||
import GHC.Exts (Addr#, minusAddr#) | ||
|
||
|
@@ -160,7 +160,8 @@ import GHC.ForeignPtr (ForeignPtr(ForeignPtr) | |
#if __GLASGOW_HASKELL__ < 900 | ||
, newForeignPtr_ | ||
#endif | ||
, mallocPlainForeignPtrBytes) | ||
|
||
, mallocPlainForeignPtrBytes, ForeignPtrContents (PlainPtr)) | ||
|
||
#if MIN_VERSION_base(4,10,0) | ||
import GHC.ForeignPtr (plusForeignPtr) | ||
|
@@ -183,6 +184,7 @@ import GHC.ForeignPtr (unsafeWithForeignPtr) | |
|
||
import qualified Language.Haskell.TH.Lib as TH | ||
import qualified Language.Haskell.TH.Syntax as TH | ||
import Data.Functor (($>)) | ||
|
||
#if !MIN_VERSION_base(4,15,0) | ||
unsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b | ||
|
@@ -641,10 +643,13 @@ createFpUptoN' l action = do | |
-- | ||
createFpAndTrim :: Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteString | ||
createFpAndTrim l action = do | ||
fp <- mallocByteString l | ||
l' <- action fp | ||
if assert (0 <= l' && l' <= l) $ l' >= l | ||
then return $! BS fp l | ||
fp <- mallocByteString l | ||
l' <- action fp | ||
if assert (0 <= l' && l' <= l) $ l' >= l | ||
then return $! BS fp l | ||
else | ||
if l < 4096 | ||
then shrinkFp fp l' $> BS fp l' | ||
else createFp l' $ \fp' -> memcpyFp fp' fp l' | ||
{-# INLINE createFpAndTrim #-} | ||
|
||
|
@@ -1023,6 +1028,12 @@ memcpy p q s = void $ c_memcpy p q (fromIntegral s) | |
memcpyFp :: ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO () | ||
memcpyFp fp fq s = unsafeWithForeignPtr fp $ \p -> | ||
unsafeWithForeignPtr fq $ \q -> memcpy p q s | ||
|
||
shrinkFp :: ForeignPtr Word8 -> Int -> IO () | ||
shrinkFp (ForeignPtr _ (PlainPtr marr)) (I# l#) = | ||
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. 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. I'd write |
||
IO $ \s1# -> case shrinkMutableByteArray# marr l# s1# of | ||
s2# -> (# s2#, () #) | ||
shrinkFp _ _ = error "Must be PlainPtr" | ||
|
||
{- | ||
foreign import ccall unsafe "string.h memmove" c_memmove | ||
|
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.
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.
This condition is the main tricky bit.
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.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:
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.)