Skip to content

Commit 55cd1c7

Browse files
committed
Avoid needlessly unboxed buffer address and length
GHC optimises them away, and the code is simpler without needless unboxed pointer and length.
1 parent a140979 commit 55cd1c7

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

Data/ByteString/Builder/Internal.hs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -892,44 +892,43 @@ cstringLiteral = \addr -> builder $ \k br -> do
892892
cstringUtf8Literal :: Addr# -> Builder
893893
cstringUtf8Literal = \addr0 -> builder $ \k br -> do
894894
#if __GLASGOW_HASKELL__ >= 811
895-
let len = cstringLength# addr0
895+
let len = I# (cstringLength# addr0)
896896
#else
897-
(I# len) <- fromIntegral <$> S.c_strlen (Ptr addr0)
897+
len <- fromIntegral <$> S.c_strlen (Ptr addr0)
898898
#endif
899899
nullAt <- c_strstr (Ptr addr0) (Ptr "\xc0\x80"#)
900-
cstringUtf8_step addr0 len nullAt k br
900+
cstringUtf8_step (Ptr addr0) len nullAt k br
901901

902-
cstringUtf8_step :: Addr# -> Int# -> Ptr Word8 -> BuildStep r -> BuildStep r
903-
cstringUtf8_step addr len ((== nullPtr) -> True) k br =
902+
cstringUtf8_step :: Ptr Word8-> Int -> Ptr Word8 -> BuildStep r -> BuildStep r
903+
cstringUtf8_step !ip !len ((== nullPtr) -> True) k br =
904904
-- Contains no encoded nulls, use simple copy codepath
905905
wrappedBytesCopyStep (BufferRange ip ipe) k br
906906
where
907-
ip = Ptr addr
908-
ipe = Ptr (addr `plusAddr#` len)
909-
cstringUtf8_step addr len !nullAt k (BufferRange op0 ope)
907+
!ipe = ip `plusPtr` len
908+
cstringUtf8_step !ip !len !nullAt k (BufferRange op0 ope)
910909
-- Copy as much of the null-free portion of the string as fits into the
911910
-- available buffer space. If the string is long enough, we may have asked
912911
-- for less than its full length, filling the buffer with the rest will go
913912
-- into the next builder step.
914913
| avail > nullFree = do
915-
when (nullFree > 0) (S.memcpy op0 (Ptr addr) nullFree)
914+
when (nullFree > 0) (S.memcpy op0 ip nullFree)
916915
pokeElemOff op0 nullFree 0
917916
let !op' = op0 `plusPtr` (nullFree + 1)
918-
nread# = nullFree# +# 2#
919-
addr' = addr `plusAddr#` nread#
920-
len' = len -# nread#
921-
nullAt' <- c_strstr (Ptr addr') (Ptr "\xc0\x80"#)
922-
cstringUtf8_step addr' len' nullAt' k (BufferRange op' ope)
917+
nread = nullFree + 2
918+
!ip' = ip `plusPtr` nread
919+
len' = len - nread
920+
nullAt' <- c_strstr ip' (Ptr "\xc0\x80"#)
921+
cstringUtf8_step ip' len' nullAt' k (BufferRange op' ope)
923922
| otherwise = do
924-
let !copy@(I# copy#) = min avail nullFree
925-
when (copy > 0) (S.memcpy op0 (Ptr addr) copy)
923+
let !copy = min avail nullFree
924+
when (copy > 0) (S.memcpy op0 ip copy)
926925
let !op' = op0 `plusPtr` copy
927-
addr' = addr `plusAddr#` copy#
928-
len' = len -# copy#
929-
return $ bufferFull 1 op' (cstringUtf8_step addr' len' nullAt k)
926+
!ip' = ip `plusPtr` copy
927+
len' = len - copy
928+
return $ bufferFull 1 op' (cstringUtf8_step ip' len' nullAt k)
930929
where
931930
!avail = ope `minusPtr` op0
932-
!nullFree@(I# nullFree#) = nullAt `minusPtr` (Ptr addr)
931+
!nullFree = nullAt `minusPtr` ip
933932

934933
foreign import ccall unsafe "string.h strstr" c_strstr
935934
:: CString -> CString -> IO (Ptr Word8)

0 commit comments

Comments
 (0)