|
| 1 | +{-# LANGUAGE BangPatterns #-} |
| 2 | +{-# LANGUAGE CPP #-} |
| 3 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 4 | +{-# LANGUAGE TypeApplications #-} |
| 5 | +{- | |
| 6 | +Here we test that GHC is able to optimize well construction of vector |
| 7 | +using monadic\/applicative actions. Well is understood as able to |
| 8 | +generate code which does not allocate except for buffer and some |
| 9 | +constant overhead. |
| 10 | +-} |
| 11 | +module Inspect.Alloc where |
| 12 | + |
| 13 | +import Control.Monad.ST |
| 14 | +import Data.Int |
| 15 | +-- import Data.Monoid |
| 16 | +import Data.Functor.Identity |
| 17 | +import Test.Tasty |
| 18 | +import Test.Tasty.HUnit |
| 19 | +import System.Mem |
| 20 | +import Test.Alloc |
| 21 | + |
| 22 | +import qualified Data.Vector.Unboxed as VU |
| 23 | + |
| 24 | + |
| 25 | +tests :: TestTree |
| 26 | +tests = testGroup "allocations" |
| 27 | + [ testGroup "traversable" |
| 28 | + [ testCase "IO" |
| 29 | + $ checkAllocations (linear 8) |
| 30 | + $ whnfIO (VU.traverse (\_ -> getAllocationCounter) vector) |
| 31 | + |
| 32 | +#if MIN_VERSION_base(4,17,0) |
| 33 | + -- GHC<9.4 doesn't optimize well. |
| 34 | + , testCase "ST" |
| 35 | + $ checkAllocations (linear 8) |
| 36 | + $ (\v -> runST $ VU.traverse (pureST . fromIntegral) v) `whnf` vector |
| 37 | +#endif |
| 38 | + |
| 39 | +#if MIN_VERSION_base(4,15,0) |
| 40 | + -- GHC<9.0 doesn't optimize this well. And there's no appetite |
| 41 | + -- for finding out why. Thus it's disabled for them. We'll still |
| 42 | + -- catch regression going forward. |
| 43 | + , testCase "Identity" |
| 44 | + $ checkAllocations (linear 8) |
| 45 | + $ VU.traverse (\n -> Identity (10*n)) `whnf` vector |
| 46 | +#endif |
| 47 | + |
| 48 | + -- NOTE: Naive traversal is lazy and allocated 2 words per element |
| 49 | + -- |
| 50 | + -- , testCase "Const Sum" |
| 51 | + -- $ checkAllocations constant |
| 52 | + -- $ whnf (VU.traverse (Const @_ @() . Sum)) vector |
| 53 | + ] |
| 54 | + , testGroup "unstreamM" |
| 55 | + [ testCase "IO" |
| 56 | + $ checkAllocations (linear 8) |
| 57 | + $ whnfIO (VU.replicateM size getAllocationCounter) |
| 58 | + |
| 59 | +#if MIN_VERSION_base(4,17,0) |
| 60 | + -- GHC<9.4 doesn't optimize well. |
| 61 | + , testCase "ST" |
| 62 | + $ checkAllocations (linear 8) |
| 63 | + $ (\sz -> runST $ VU.generateM sz pureST) `whnf` size |
| 64 | +#endif |
| 65 | + |
| 66 | + -- , testCase "Identity" |
| 67 | + -- $ checkAllocations (linear 8) |
| 68 | + -- $ (\sz -> VU.generateM sz (\n -> Identity (fromIntegral n :: Int64))) `whnf` size |
| 69 | + ] |
| 70 | + ] |
| 71 | + |
| 72 | + |
| 73 | +pureST :: Int -> ST s Int64 |
| 74 | +{-# NOINLINE pureST #-} |
| 75 | +pureST i = pure $! fromIntegral i |
| 76 | + |
| 77 | +-- | Constant overhead. Measurement precision is 4k |
| 78 | +overhead :: Int64 |
| 79 | +overhead = 4096*2 |
| 80 | + |
| 81 | +-- | Vector size. It should be large so 1byte per element will be |
| 82 | +-- large than page. |
| 83 | +size :: Int |
| 84 | +size = 100000 |
| 85 | + |
| 86 | +vector :: VU.Vector Int64 |
| 87 | +{-# NOINLINE vector #-} |
| 88 | +vector = VU.generate size fromIntegral |
| 89 | + |
| 90 | +-- | N bytes per element + constant overhead. We also check that bound |
| 91 | +-- is tight. |
| 92 | +linear :: Int -> Range |
| 93 | +linear n = Range |
| 94 | + { allocHi = fromIntegral (n * size) + overhead |
| 95 | + , allocLo = fromIntegral (n * size) |
| 96 | + } |
| 97 | + |
| 98 | +-- | Only constant overhead |
| 99 | +constant :: Range |
| 100 | +constant = Range { allocHi = overhead |
| 101 | + , allocLo = 0 |
| 102 | + } |
0 commit comments