Skip to content

Commit

Permalink
Rename Ring to RingArray
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Oct 11, 2024
1 parent 4eacf63 commit 3531008
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 235 deletions.
28 changes: 14 additions & 14 deletions benchmark/Streamly/Benchmark/Data/Fold/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import System.Random (randomRIO)

import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Internal.Data.Fold as Window
import qualified Streamly.Internal.Data.Ring as Ring
import qualified Streamly.Internal.Data.RingArray as RingArray
import qualified Streamly.Internal.Data.Stream as Stream

import Streamly.Benchmark.Common
Expand Down Expand Up @@ -94,29 +94,29 @@ o_1_space_folds numElements =
(Window.windowRange 1000)

, benchWithFoldInt numElements "sumInt (window size 100)"
(Ring.slidingWindow 100 Window.windowSumInt)
(RingArray.slidingWindow 100 Window.windowSumInt)
, benchWithFoldInt numElements "sum for Int (window size 100)"
(Ring.slidingWindow 100 Window.windowSum)
(RingArray.slidingWindow 100 Window.windowSum)
, benchWithFold numElements "sum (window size 100)"
(Ring.slidingWindow 100 Window.windowSum)
(RingArray.slidingWindow 100 Window.windowSum)
, benchWithFold numElements "sum (window size 1000)"
(Ring.slidingWindow 1000 Window.windowSum)
(RingArray.slidingWindow 1000 Window.windowSum)
, benchWithFold numElements "sum (entire stream)"
(Window.cumulative Window.windowSum)
, benchWithFold numElements "sum (Data.Fold)"
Fold.sum

, benchWithFold numElements "mean (window size 100)"
(Ring.slidingWindow 100 Window.windowMean)
(RingArray.slidingWindow 100 Window.windowMean)
, benchWithFold numElements "mean (window size 1000)"
(Ring.slidingWindow 1000 Window.windowMean)
(RingArray.slidingWindow 1000 Window.windowMean)
, benchWithFold numElements "mean (entire stream)"
(Window.cumulative Window.windowMean)
, benchWithFold numElements "mean (Data.Fold)"
Fold.mean

, benchWithFold numElements "powerSum 2 (window size 100)"
(Ring.slidingWindow 100 (Window.windowPowerSum 2))
(RingArray.slidingWindow 100 (Window.windowPowerSum 2))
, benchWithFold numElements "powerSum 2 (entire stream)"
(Window.cumulative (Window.windowPowerSum 2))

Expand Down Expand Up @@ -159,19 +159,19 @@ o_1_space_scans numElements =
(Window.windowRange 1000)

, benchWithPostscan numElements "sum (window size 100)"
(Ring.slidingWindow 100 Window.windowSum)
(RingArray.slidingWindow 100 Window.windowSum)
, benchWithPostscan numElements "sum (window size 1000)"
(Ring.slidingWindow 1000 Window.windowSum)
(RingArray.slidingWindow 1000 Window.windowSum)

, benchWithPostscan numElements "mean (window size 100)"
(Ring.slidingWindow 100 Window.windowMean)
(RingArray.slidingWindow 100 Window.windowMean)
, benchWithPostscan numElements "mean (window size 1000)"
(Ring.slidingWindow 1000 Window.windowMean)
(RingArray.slidingWindow 1000 Window.windowMean)

, benchWithPostscan numElements "powerSum 2 (window size 100)"
(Ring.slidingWindow 100 (Window.windowPowerSum 2))
(RingArray.slidingWindow 100 (Window.windowPowerSum 2))
, benchWithPostscan numElements "powerSum 2 (window size 1000)"
(Ring.slidingWindow 1000 (Window.windowPowerSum 2))
(RingArray.slidingWindow 1000 (Window.windowPowerSum 2))
]
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- |
-- Module : Streamly.Benchmark.Data.Ring
-- Module : Streamly.Benchmark.Data.RingArray
-- Copyright : (c) 2022 Composewell
-- License : BSD-3-Clause
-- Maintainer : [email protected]
Expand All @@ -12,7 +12,7 @@ module Main (main) where

import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.MutArray as MutArray
import qualified Streamly.Internal.Data.Ring as Ring
import qualified Streamly.Internal.Data.RingArray as RingArray

import Test.Tasty.Bench
import Streamly.Benchmark.Common
Expand All @@ -23,17 +23,17 @@ import Prelude as P
-- Benchmark ops
-------------------------------------------------------------------------------

eqArrayN :: (Int, Array.Array Int, Ring.Ring Int) -> IO Bool
eqArrayN (value, arr, ring) = Ring.eqArrayN ring arr value
eqArrayN :: (Int, Array.Array Int, RingArray.RingArray Int) -> IO Bool
eqArrayN (value, arr, ring) = RingArray.eqArrayN ring arr value

eqArray :: (Array.Array Int, Ring.Ring Int) -> IO Bool
eqArray (arr, ring) = Ring.eqArray ring arr
eqArray :: (Array.Array Int, RingArray.RingArray Int) -> IO Bool
eqArray (arr, ring) = RingArray.eqArray ring arr

-------------------------------------------------------------------------------
-- Benchmark groups
-------------------------------------------------------------------------------

o_1_space_serial :: Int -> Array.Array Int -> Ring.Ring Int -> [Benchmark]
o_1_space_serial :: Int -> Array.Array Int -> RingArray.RingArray Int -> [Benchmark]
o_1_space_serial value arr ring =
[ bench "eqArrayN" $ nfIO $ eqArrayN (value, arr, ring)
, bench "eqArray" $ nfIO $ eqArray (arr, ring)
Expand All @@ -44,7 +44,7 @@ o_1_space_serial value arr ring =
-------------------------------------------------------------------------------

moduleName :: String
moduleName = "Data.Ring"
moduleName = "Data.RingArray"

main :: IO ()
main = do
Expand All @@ -56,7 +56,7 @@ main = do
let input = [1 .. value] :: [Int]
let arr = Array.fromList input
marr <- MutArray.fromList input
let ring = maybe (error "cast failed") id (Ring.castMutArray marr)
let ring = maybe (error "cast failed") id (RingArray.castMutArray marr)

return (arr, ring)

Expand Down
4 changes: 2 additions & 2 deletions benchmark/streamly-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ benchmark Data.ParserK.Chunked.Generic
buildable: True
build-depends: exceptions >= 0.8 && < 0.11

benchmark Data.Ring
benchmark Data.RingArray
import: bench-options
type: exitcode-stdio-1.0
main-is: Streamly/Benchmark/Data/Ring.hs
main-is: Streamly/Benchmark/Data/RingArray.hs
if flag(limit-build-mem)
ghc-options: +RTS -M1500M -RTS

Expand Down
2 changes: 1 addition & 1 deletion core/src/DocTestDataScanl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
For APIs that have not been released yet.
>>> import qualified Streamly.Internal.Data.Fold as Fold
>>> import qualified Streamly.Internal.Data.Ring as Ring
>>> import qualified Streamly.Internal.Data.RingArray as RingArray
>>> import qualified Streamly.Internal.Data.Scanl as Scanl
>>> import qualified Streamly.Internal.Data.Stream as Stream
-}
2 changes: 1 addition & 1 deletion core/src/Streamly/Internal/Data/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ import qualified Streamly.Internal.Data.Fold.Type as Fold
import qualified Streamly.Internal.Data.Serialize.Type as Serialize
import qualified Streamly.Internal.Data.MutByteArray.Type as MBA
import qualified Streamly.Internal.Data.MutArray as MA
import qualified Streamly.Internal.Data.Ring as RB
import qualified Streamly.Internal.Data.RingArray as RB
import qualified Streamly.Internal.Data.Parser as Parser
-- import qualified Streamly.Internal.Data.ParserK as ParserK
import qualified Streamly.Internal.Data.Stream as D
Expand Down
2 changes: 1 addition & 1 deletion core/src/Streamly/Internal/Data/Array/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import qualified Streamly.Internal.Data.MutArray.Generic as MArray
import qualified Streamly.Internal.Data.Fold.Type as FL
import qualified Streamly.Internal.Data.Producer.Type as Producer
import qualified Streamly.Internal.Data.Producer as Producer
import qualified Streamly.Internal.Data.Ring.Generic as RB
import qualified Streamly.Internal.Data.RingArray.Generic as RB
import qualified Streamly.Internal.Data.Stream.Type as D
import qualified Streamly.Internal.Data.Stream.Generate as D
import qualified Text.ParserCombinators.ReadPrec as ReadPrec
Expand Down
24 changes: 12 additions & 12 deletions core/src/Streamly/Internal/Data/Fold/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Streamly.Internal.Data.Fold.Combinators
-- the 'scanMaybe' combinator. For scanners the result of the fold is
-- usually a transformation of the current element rather than an
-- aggregation of all elements till now.
-- , nthLast -- using Ring array
-- , nthLast -- using RingArray array
, rollingMapM

-- *** Filters
Expand Down Expand Up @@ -233,7 +233,7 @@ import Streamly.Internal.Data.Unbox (Unbox(..))
import Streamly.Internal.Data.MutArray.Type (MutArray(..))
import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
import Streamly.Internal.Data.Pipe.Type (Pipe (..))
import Streamly.Internal.Data.Ring (Ring(..))
import Streamly.Internal.Data.RingArray (RingArray(..))
-- import Streamly.Internal.Data.Scan (Scan (..))
import Streamly.Internal.Data.Stream.Type (Stream)
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
Expand All @@ -244,7 +244,7 @@ import qualified Streamly.Internal.Data.MutArray.Type as MA
import qualified Streamly.Internal.Data.Array.Type as Array
import qualified Streamly.Internal.Data.Fold.Type as Fold
import qualified Streamly.Internal.Data.Pipe.Type as Pipe
import qualified Streamly.Internal.Data.Ring as Ring
import qualified Streamly.Internal.Data.RingArray as RingArray
import qualified Streamly.Internal.Data.Scanl.Combinators as Scanl
import qualified Streamly.Internal.Data.Scanl.Type as Scanl
import qualified Streamly.Internal.Data.Scanl.Window as Scanl
Expand Down Expand Up @@ -1559,17 +1559,17 @@ takeEndBySeq patArr (Fold fstep finitial fextract ffinal) =
res <- fstep s x
case res of
Partial s1 -> do
let rb = Ring
let rb = RingArray
{ ringContents = mba
, ringSize = patBytes
, ringHead = offset
}
(rb1, old :: a) <- liftIO (Ring.replace rb x)
(rb1, old :: a) <- liftIO (RingArray.replace rb x)
let ringHash = deltaCksum cksum old x
let rh1 = ringHead rb1
matches <-
if ringHash == patHash
then liftIO $ Ring.eqArray rb1 patArr
then liftIO $ RingArray.eqArray rb1 patArr
else return False
if matches
then Done <$> ffinal s1
Expand Down Expand Up @@ -1720,20 +1720,20 @@ takeEndBySeq_ patArr (Fold fstep finitial fextract ffinal) =
else return $ Partial $ SplitOnSeqKRLoop s ringHash mba 0
else return $ Partial $ SplitOnSeqKR s (offset + SIZE_OF(a)) mba
step (SplitOnSeqKRLoop s cksum mba offset) x = do
let rb = Ring
let rb = RingArray
{ ringContents = mba
, ringSize = patBytes
, ringHead = offset
}
(rb1, old :: a) <- liftIO (Ring.replace rb x)
(rb1, old :: a) <- liftIO (RingArray.replace rb x)
res <- fstep s old
case res of
Partial s1 -> do
let ringHash = deltaCksum cksum old x
let rh1 = ringHead rb1
matches <-
if ringHash == patHash
then liftIO $ Ring.eqArray rb1 patArr
then liftIO $ RingArray.eqArray rb1 patArr
else return False
if matches
then Done <$> ffinal s1
Expand Down Expand Up @@ -1767,13 +1767,13 @@ takeEndBySeq_ patArr (Fold fstep finitial fextract ffinal) =
Done b -> return b

let consumeRing s orig mba offset = do
let rb :: Ring a = Ring
let rb :: RingArray a = RingArray
{ ringContents = mba
, ringSize = patBytes
, ringHead = offset
}
old <- Ring.unsafeGetHead rb
let rb1 = Ring.moveForward rb
old <- RingArray.unsafeGetHead rb
let rb1 = RingArray.moveForward rb
r <- fstep s old
case r of
Partial s1 ->
Expand Down
26 changes: 13 additions & 13 deletions core/src/Streamly/Internal/Data/Fold/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-- For more advanced statistical measures see the @streamly-statistics@
-- package.

-- XXX A window fold can be driven either using the Ring.slidingWindow
-- XXX A window fold can be driven either using the RingArray.slidingWindow
-- combinator or by zipping nthLast fold and last fold.

-- XXX Deprecate all the functions in this module. These should be scans only.
Expand Down Expand Up @@ -64,7 +64,7 @@ where
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Bifunctor(bimap)
import Data.Proxy (Proxy(..))
import Streamly.Internal.Data.Ring (Ring(..))
import Streamly.Internal.Data.RingArray (RingArray(..))
import Streamly.Internal.Data.Unbox (Unbox(..))

import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..))
Expand All @@ -73,7 +73,7 @@ import Streamly.Internal.Data.Tuple.Strict

import qualified Streamly.Internal.Data.Fold.Type as Fold
import qualified Streamly.Internal.Data.MutArray.Type as MutArray
import qualified Streamly.Internal.Data.Ring as Ring
import qualified Streamly.Internal.Data.RingArray as RingArray
-- import qualified Streamly.Internal.Data.Scanl.Type as Scanl

import Prelude hiding (length, sum, minimum, maximum)
Expand Down Expand Up @@ -258,17 +258,17 @@ windowPowerSumFrac p = windowLmap (** p) windowSum
-------------------------------------------------------------------------------

{-# INLINE ringRange #-}
ringRange :: (MonadIO m, Unbox a, Ord a) => Ring a -> m (Maybe (a, a))
ringRange :: (MonadIO m, Unbox a, Ord a) => RingArray a -> m (Maybe (a, a))
-- Ideally this should perform the same as the implementation below, but it is
-- 2x worse, need to investigate why.
-- ringRange = Ring.fold (Fold.fromScanl Scanl.range)
ringRange rb@Ring{..} = do
-- ringRange = RingArray.fold (Fold.fromScanl Scanl.range)
ringRange rb@RingArray{..} = do
if ringSize == 0
then return Nothing
else do
x <- liftIO $ peekAt 0 ringContents
let accum (mn, mx) a = return (min mn a, max mx a)
in fmap Just $ Ring.foldlM' accum (x, x) rb
in fmap Just $ RingArray.foldlM' accum (x, x) rb

-- | Determine the maximum and minimum in a rolling window.
--
Expand All @@ -282,11 +282,11 @@ ringRange rb@Ring{..} = do
{-# INLINE windowRange #-}
windowRange :: forall m a. (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (Maybe (a, a))
-- windowRange =
-- Fold.fromScanl . Ring.scanFoldRingsBy (Fold.fromScanl Scanl.range)
-- Fold.fromScanl . RingArray.scanFoldRingsBy (Fold.fromScanl Scanl.range)
-- Ideally this should perform the same as the implementation below which is
-- just expanded form of this. Some inlining/exitify optimization makes this
-- perform much worse. Need to investigate and fix that.
-- windowRange = Fold.fromScanl . Ring.scanCustomFoldRingsBy ringRange
-- windowRange = Fold.fromScanl . RingArray.scanCustomFoldRingsBy ringRange
windowRange n = Fold step initial extract extract

where
Expand All @@ -299,7 +299,7 @@ windowRange n = Fold step initial extract extract
return $ Partial $ Tuple3Fused' (MutArray.arrContents arr) 0 0

step (Tuple3Fused' mba rh i) a = do
Ring _ _ rh1 <- Ring.replace_ (Ring mba (n * SIZE_OF(a)) rh) a
RingArray _ _ rh1 <- RingArray.replace_ (RingArray mba (n * SIZE_OF(a)) rh) a
return $ Partial $ Tuple3Fused' mba rh1 (i + 1)

-- XXX exitify optimization causes a problem here when modular folds are
Expand All @@ -312,7 +312,7 @@ windowRange n = Fold step initial extract extract
-- operation.
let rs = min i n * SIZE_OF(a)
rh1 = if i <= n then 0 else rh
in ringRange $ Ring mba rs rh1
in ringRange $ RingArray mba rs rh1

-- | Find the minimum element in a rolling window.
--
Expand All @@ -330,7 +330,7 @@ windowRange n = Fold step initial extract extract
windowMinimum :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (Maybe a)
windowMinimum n = fmap (fmap fst) $ windowRange n
-- windowMinimum =
-- Fold.fromScanl . Ring.scanFoldRingsBy (Fold.fromScanl Scanl.minimum)
-- Fold.fromScanl . RingArray.scanFoldRingsBy (Fold.fromScanl Scanl.minimum)

-- | The maximum element in a rolling window.
--
Expand All @@ -345,7 +345,7 @@ windowMinimum n = fmap (fmap fst) $ windowRange n
windowMaximum :: (MonadIO m, Unbox a, Ord a) => Int -> Fold m a (Maybe a)
windowMaximum n = fmap (fmap snd) $ windowRange n
-- windowMaximum =
-- Fold.fromScanl . Ring.scanFoldRingsBy (Fold.fromScanl Scanl.maximum)
-- Fold.fromScanl . RingArray.scanFoldRingsBy (Fold.fromScanl Scanl.maximum)

-- | Arithmetic mean of elements in a sliding window:
--
Expand Down
Loading

0 comments on commit 3531008

Please sign in to comment.