Skip to content

Commit

Permalink
Replace memcmp_index with MutByteArray.unsafeByteCmp
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Dec 11, 2024
1 parent ec13854 commit 1d16a00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
5 changes: 0 additions & 5 deletions core/src/Streamly/Internal/Data/MutArray/Lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ size_t memchr_index(const void *dst, size_t off, int c, size_t len) {
return len;
}
}

int memcmp_index(const void *p1, size_t p1_off, const void *p2, size_t p2_off, size_t len) {
int cmp = memcmp((char *)p1 + p1_off, (char *)p2 + p2_off, len);
return cmp;
}
10 changes: 1 addition & 9 deletions core/src/Streamly/Internal/Data/MutArray/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ module Streamly.Internal.Data.MutArray.Type
, isPower2
, roundUpToPower2

-- * Foreign APIs
, c_memcmp_index

-- * Deprecated
, realloc
, createOfWith
Expand Down Expand Up @@ -371,7 +368,7 @@ import Data.Char (ord)
import Data.Functor.Identity (Identity(..))
import Data.Proxy (Proxy(..))
import Data.Word (Word8, Word16)
import Foreign.C.Types (CSize(..), CInt(..))
import Foreign.C.Types (CSize(..))
import Foreign.Ptr (plusPtr)
import Streamly.Internal.Data.MutByteArray.Type
( MutByteArray(..)
Expand Down Expand Up @@ -428,11 +425,6 @@ foreign import ccall unsafe "memchr_index" c_memchr_index
foreign import ccall unsafe "string.h strlen" c_strlen
:: Ptr Word8 -> IO CSize

foreign import ccall unsafe "memcmp_index" c_memcmp_index
:: MutableByteArray# RealWorld -> CSize
-> MutableByteArray# RealWorld -> CSize
-> CSize -> IO CInt

-- | Given an 'Unboxed' type (unused first arg) and a number of bytes, return
-- how many elements of that type will completely fit in those bytes.
--
Expand Down
38 changes: 19 additions & 19 deletions core/src/Streamly/Internal/Data/RingArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -642,21 +642,21 @@ eqArrayN RingArray{..} Array.Array{..} nBytes
| arrEnd - arrStart < nBytes = error "eqArrayN: array is shorter than n"
| ringSize < nBytes = error "eqArrayN: ring is shorter than n"
| nBytes == 0 = return True
| nBytesC <= p1Len = do
part1 <- MutArray.c_memcmp_index arr# 0 ring# p1Off nBytesC
| nBytes <= p1Len = do
part1 <-
MutByteArray.unsafeByteCmp
arrContents 0 ringContents ringHead nBytes
pure $ part1 == 0
| otherwise = do
part1 <- MutArray.c_memcmp_index arr# 0 ring# p1Off p1Len
part2 <- MutArray.c_memcmp_index arr# p1Len ring# p2Off p2Len
part1 <-
MutByteArray.unsafeByteCmp
arrContents 0 ringContents ringHead p1Len
part2 <-
MutByteArray.unsafeByteCmp arrContents p1Len ringContents 0 p2Len
pure $ part1 == 0 && part2 == 0
where
nBytesC = fromIntegral nBytes
arr# = MutByteArray.getMutByteArray# arrContents
ring# = MutByteArray.getMutByteArray# ringContents
p1Off = fromIntegral ringHead
p1Len = fromIntegral $ ringSize - ringHead
p2Off = 0
p2Len = nBytesC - p1Len
p1Len = ringSize - ringHead
p2Len = nBytes - p1Len

-- | Byte compare the entire length of ringBuffer with the given array,
-- starting at the supplied ring head index. Returns true if the Array and
Expand All @@ -669,16 +669,16 @@ eqArray :: RingArray a -> Array a -> IO Bool
eqArray RingArray{..} Array.Array{..}
| arrEnd - arrStart < ringSize = error "eqArrayN: array is shorter than ring"
| otherwise = do
part1 <- MutArray.c_memcmp_index arr# 0 ring# p1Off p1Len
part2 <- MutArray.c_memcmp_index arr# p1Len ring# p2Off p2Len
part1 <-
MutByteArray.unsafeByteCmp
arrContents 0 ringContents ringHead p1Len
part2 <-
MutByteArray.unsafeByteCmp
arrContents p1Len ringContents 0 p2Len
pure $ part1 == 0 && part2 == 0
where
arr# = MutByteArray.getMutByteArray# arrContents
ring# = MutByteArray.getMutByteArray# ringContents
p1Off = fromIntegral ringHead
p1Len = fromIntegral $ ringSize - ringHead
p2Off = 0
p2Len = fromIntegral ringHead
p1Len = ringSize - ringHead
p2Len = ringHead

-------------------------------------------------------------------------------
-- Folding
Expand Down

0 comments on commit 1d16a00

Please sign in to comment.