Skip to content

Commit

Permalink
Remove internal uses of withForeignPtr (#536)
Browse files Browse the repository at this point in the history
* Avoid 'withForeignPtr' via 'createf' and variants

* remove stupid/wrong temporary cpp

* Move new utilities into D.B.Internal.Utils

* Move unsafeWithForeignPtr shim into Utils

* Fix imports for unsafeWithForeignPtr shim

The error messages from ghc-8.0 weren't red, so I didn't notice them.
Silly stuff.

* Clean up potentially-sketchy unsafeWithForeignPtr uses

* Revert documentation change to createAndTrim

* Revert "Fix imports for unsafeWithForeignPtr shim"

This reverts commit b6b4de6.

Revert "Move unsafeWithForeignPtr shim into Utils"

This reverts commit 6df394e.

Revert "Move new utilities into D.B.Internal.Utils"

This reverts commit 6462e4f.

* Rename ForeignPtr helpers to use "Fp" suffix

* [NONBUILDABLE] rename Data.ByteString.Internal

to Data.ByteString.Internal.Type. The re-export-only compatibility
module to replace it at Data.ByteString.Internal is intentionally
deleted from this commit, to allow for a more reviewable diff.

* Add compatibility stub "Data.ByteString.Internal"

* Replace Data.ByteString.Internal.Type's description

* Remove the use of withRawBuffer, too.

It has the same performance issues as does withForeignPtr.

Co-authored-by: Matthew Craven <[email protected]>
  • Loading branch information
clyring and clyring authored Sep 26, 2022
1 parent 6c0a194 commit 3b0f5d3
Show file tree
Hide file tree
Showing 9 changed files with 1,203 additions and 1,029 deletions.
234 changes: 115 additions & 119 deletions Data/ByteString.hs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ import Data.Semigroup (Semigroup((<>)))
#endif

import qualified Data.ByteString as S
import qualified Data.ByteString.Internal as S
import qualified Data.ByteString.Internal.Type as S
import qualified Data.ByteString.Lazy.Internal as L
import qualified Data.ByteString.Short.Internal as Sh

Expand Down Expand Up @@ -1099,8 +1099,8 @@ buildStepToCIOS (AllocationStrategy nextBuffer bufSize trim) =
wrapChunk !op' mkCIOS
| chunkSize == 0 = mkCIOS True
| trim chunkSize size = do
bs <- S.create chunkSize $ \pbuf' ->
copyBytes pbuf' pbuf chunkSize
bs <- S.createFp chunkSize $ \fpbuf' ->
S.memcpyFp fpbuf' fpbuf chunkSize
-- FIXME: We could reuse the trimmed buffer here.
return $ Yield1 bs (mkCIOS False)
| otherwise =
Expand Down
12 changes: 6 additions & 6 deletions Data/ByteString/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ import Data.ByteString (null,length,tail,init,append
,useAsCString,useAsCStringLen
)

import Data.ByteString.Internal
import Data.ByteString.Internal.Type
import Data.ByteString.ReadInt
import Data.ByteString.ReadNat

Expand Down Expand Up @@ -975,11 +975,11 @@ unlines = \li -> let
(+!) = checkedAdd "Char8.unlines"

go [] _ = pure ()
go (BS srcFP len : srcs) dest = do
unsafeWithForeignPtr srcFP $ \src -> memcpy dest src len
pokeElemOff dest len (c2w '\n')
go srcs $ dest `plusPtr` (len + 1)
in unsafeCreate totLen (go li)
go (BS src len : srcs) dest = do
memcpyFp dest src len
pokeFpByteOff dest len (c2w '\n')
go srcs $ dest `plusForeignPtr` (len + 1)
in unsafeCreateFp totLen (go li)

-- | 'words' breaks a ByteString up into a list of words, which
-- were delimited by Chars representing white space.
Expand Down
Loading

0 comments on commit 3b0f5d3

Please sign in to comment.