[Git][ghc/ghc][wip/codebuffer-perftest] base/encoding: add an allocations performance test (#22946)
Josh Meredith (@JoshMeredith)
gitlab at gitlab.haskell.org
Thu Apr 27 09:18:49 UTC 2023
Josh Meredith pushed to branch wip/codebuffer-perftest at Glasgow Haskell Compiler / GHC
Commits:
a622488d by Josh Meredith at 2023-04-27T09:18:36+00:00
base/encoding: add an allocations performance test (#22946)
- - - - -
2 changed files:
- libraries/base/tests/perf/all.T
- + libraries/base/tests/perf/encodingAllocations.hs
Changes:
=====================================
libraries/base/tests/perf/all.T
=====================================
@@ -3,3 +3,9 @@
#--------------------------------------
test('T17752', [only_ways(['normal'])] , makefile_test, ['T17752'])
+
+#--------------------------------------
+
+# We don't expect the code in test to vary at all, but the variance is set to
+# 1% in case the constant allocations increase by other means.
+test('encodingAllocations', collect_stats('bytes allocated', 1), compile_and_run, ['-O2'])
=====================================
libraries/base/tests/perf/encodingAllocations.hs
=====================================
@@ -0,0 +1,35 @@
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE CPP #-}
+{-# OPTIONS_GHC -dno-typeable-binds -O2 #-}
+
+module Main (main) where
+
+import System.IO
+import Data.Bits
+import GHC.Int
+import GHC.Exts
+import System.Environment
+
+devnull :: FilePath
+#if defined(mingw32_HOST_OS)
+devnull = "NUL"
+#else
+devnull = "/dev/null"
+#endif
+
+main :: IO ()
+main = withFile devnull WriteMode (loop 1000000)
+
+loop :: Int -> Handle -> IO ()
+loop 0 !_ = pure ()
+loop !n !h = do
+ hPutChar h $! dummy_char n
+ loop (n-1) h
+
+-- unsafe efficient version of `chr`
+my_chr :: Int -> Char
+my_chr (I# i) = C# (chr# i)
+
+-- return either a or b
+dummy_char :: Int -> Char
+dummy_char !i = my_chr ((i .&. 1) + 97)
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/a622488d8168cf9223cd705cac73ad115f095b2f
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/a622488d8168cf9223cd705cac73ad115f095b2f
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230427/cd88ad8e/attachment-0001.html>
More information about the ghc-commits
mailing list