Skip to content

Commit b6388b2

Browse files
committed
Inline throwIf* and clear (fixes #180)
Before: Case Allocated GCs pollEvent 1 1,600 0 pollEvent 10 1,728 0 pollEvent 100 1,728 0 pollEvent 1000 1,728 0 pollEvent 10000 1,728 0 pollEvent + clear 1 2,864 0 pollEvent + clear 10 6,448 0 pollEvent + clear 100 35,968 0 pollEvent + clear 1000 331,168 0 pollEvent + clear 10000 3,283,336 6 After: Case Allocated GCs pollEvent 1 1,496 0 pollEvent 10 1,624 0 pollEvent 100 1,624 0 pollEvent 1000 1,624 0 pollEvent 10000 1,624 0 pollEvent + clear 1 2,072 0 pollEvent + clear 10 2,704 0 pollEvent + clear 100 2,704 0 pollEvent + clear 1000 2,704 0 pollEvent + clear 10000 2,872 0
1 parent 2bd91b1 commit b6388b2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

bench/Space.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ main =
2525
"Allocated >2KB! Allocations should be constant."
2626
else Nothing)
2727
| i <- [1, 10, 100, 1000, 10000]
28+
]
29+
sequence_
30+
[ validateAction
31+
("pollEvent + clear " ++ show i)
32+
pollEventClearTest
33+
i
34+
(\weight ->
35+
if weightGCs weight > 0
36+
then Just "Non-zero number of garbage collections!"
37+
else if weightAllocatedBytes weight > 3000
38+
then Just
39+
"Allocated >3KB! Allocations should be constant."
40+
else Nothing)
41+
| i <- [1, 10, 100, 1000, 10000]
2842
])
2943

3044
-- | Test that merely polling does not allocate or engage the GC.
@@ -38,3 +52,18 @@ pollEventTest iters = do
3852
_ <- pollEvent
3953
go (i - 1)
4054
go iters
55+
56+
-- | Test that merely polling and clearing the screen does not
57+
-- allocate or engage the GC.
58+
pollEventClearTest :: Int -> IO ()
59+
pollEventClearTest iters = do
60+
initializeAll
61+
window <- createWindow "pollEventClearTest" defaultWindow
62+
renderer <- createRenderer window (-1) defaultRenderer
63+
let go :: Int -> IO ()
64+
go 0 = pure ()
65+
go i = do
66+
_ <- pollEvent
67+
clear renderer
68+
go (i - 1)
69+
go iters

src/SDL/Internal/Exception.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,39 @@ getError = liftIO $ do
3333
cstr <- Raw.getError
3434
Text.decodeUtf8 <$> BS.packCString cstr
3535

36+
{-# INLINE throwIf #-}
3637
throwIf :: MonadIO m => (a -> Bool) -> Text -> Text -> m a -> m a
3738
throwIf f caller funName m = do
3839
a <- m
3940
liftIO $ when (f a) $
4041
(SDLCallFailed caller funName <$> getError) >>= throwIO
4142
return a
4243

44+
{-# INLINE throwIf_ #-}
4345
throwIf_ :: MonadIO m => (a -> Bool) -> Text -> Text -> m a -> m ()
4446
throwIf_ f caller funName m = throwIf f caller funName m >> return ()
4547

48+
{-# INLINE throwIfNeg #-}
4649
throwIfNeg :: (MonadIO m, Num a, Ord a) => Text -> Text -> m a -> m a
4750
throwIfNeg = throwIf (< 0)
4851

52+
{-# INLINE throwIfNeg_ #-}
4953
throwIfNeg_ :: (MonadIO m, Num a, Ord a) => Text -> Text -> m a -> m ()
5054
throwIfNeg_ = throwIf_ (< 0)
5155

56+
{-# INLINE throwIfNull #-}
5257
throwIfNull :: (MonadIO m) => Text -> Text -> m (Ptr a) -> m (Ptr a)
5358
throwIfNull = throwIf (== nullPtr)
5459

60+
{-# INLINE throwIf0 #-}
5561
throwIf0 :: (Eq a, MonadIO m, Num a) => Text -> Text -> m a -> m a
5662
throwIf0 = throwIf (== 0)
5763

64+
{-# INLINE throwIfNot0 #-}
5865
throwIfNot0 :: (Eq a, MonadIO m, Num a) => Text -> Text -> m a -> m a
5966
throwIfNot0 = throwIf (/= 0)
6067

68+
{-# INLINE throwIfNot0_ #-}
6169
throwIfNot0_ :: (Eq a, MonadIO m, Num a) => Text -> Text -> m a -> m ()
6270
throwIfNot0_ = throwIf_ (/= 0)
6371

src/SDL/Video/Renderer.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ clear :: (Functor m, MonadIO m) => Renderer -> m ()
651651
clear (Renderer r) =
652652
throwIfNeg_ "SDL.Video.clear" "SDL_RenderClear" $
653653
Raw.renderClear r
654+
{-# INLINE clear #-}
654655

655656
-- | Get or set the drawing scale for rendering on the current target.
656657
--

0 commit comments

Comments
 (0)