From d1db7abc3a359e8da72763438266c5896bac589d Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Mon, 18 Nov 2024 08:42:21 -0500 Subject: [PATCH] Benchmark arrayFromListN --- bench/main.hs | 16 ++++++++++++++++ primitive.cabal | 1 + 2 files changed, 17 insertions(+) diff --git a/bench/main.hs b/bench/main.hs index caa3d67..c334e03 100644 --- a/bench/main.hs +++ b/bench/main.hs @@ -9,6 +9,7 @@ import Test.Tasty.Bench import Control.Monad.ST import Data.Primitive import Control.Monad.Trans.State.Strict +import Data.Set (Set) -- These are fixed implementations of certain operations. In the event -- that primitive changes its implementation of a function, these @@ -25,6 +26,8 @@ import qualified ByteArray.Compare import qualified PrimArray.Compare import qualified PrimArray.Traverse +import qualified Data.Set as Set + main :: IO () main = defaultMain [ bgroup "Array" @@ -34,6 +37,9 @@ main = defaultMain , bench "unsafe" (nf (\x -> runST (runStateT (Array.Traverse.Unsafe.traversePoly cheap x) 0)) numbers) ] ] + , bgroup "arrayFromListN" + [ bench "set-to-list-to-array" (whnf arrayFromSet setOfIntegers1024) + ] ] , bgroup "ByteArray" [ bgroup "compare" @@ -62,6 +68,16 @@ main = defaultMain ] ] +setOfIntegers1024 :: Set Integer +{-# noinline setOfIntegers1024 #-} +setOfIntegers1024 = Set.fromList [1..1024] + +-- The performance of this is used to confirm whether or not arrayFromListN is +-- actining as a good consumer for list fusion. +arrayFromSet :: Set Integer -> Array Integer +{-# noinline arrayFromSet #-} +arrayFromSet s = arrayFromListN (Set.size s) (Set.toList s) + cheap :: Int -> StateT Int (ST s) Int cheap i = modify (\x -> x + i) >> return (i * i) diff --git a/primitive.cabal b/primitive.cabal index 80dfa9c..7ee9add 100644 --- a/primitive.cabal +++ b/primitive.cabal @@ -115,6 +115,7 @@ benchmark bench PrimArray.Traverse build-depends: base + , containers , primitive , deepseq , tasty-bench