summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Meredith <joshmeredith2008@gmail.com>2023-04-26 14:42:20 +0000
committerJosh Meredith <joshmeredith2008@gmail.com>2023-05-02 11:54:40 +0000
commit5b8f25c466bdc0f5345cbc020c207d94540d84f8 (patch)
tree26bc6534edeb59cefba3768fe32f0c44e629653c
parent052e2bb629abc97b394b9de2394eb36cbed9385f (diff)
downloadhaskell-wip/codebuffer-perftest.tar.gz
base/encoding: add an allocations performance test (#22946)wip/codebuffer-perftest
-rw-r--r--libraries/base/tests/perf/all.T9
-rwxr-xr-xlibraries/base/tests/perf/encodingAllocations.hs30
2 files changed, 39 insertions, 0 deletions
diff --git a/libraries/base/tests/perf/all.T b/libraries/base/tests/perf/all.T
index 61f42f5420..9cc1d8a128 100644
--- a/libraries/base/tests/perf/all.T
+++ b/libraries/base/tests/perf/all.T
@@ -1,5 +1,14 @@
+# .stats files aren't yet supported in the JS backend
+setTestOpts(js_skip)
+
#--------------------------------------
# Check specialization of elem via rules
#--------------------------------------
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', [only_ways(['normal']), collect_stats('bytes allocated', 1)], compile_and_run, ['-O2'])
diff --git a/libraries/base/tests/perf/encodingAllocations.hs b/libraries/base/tests/perf/encodingAllocations.hs
new file mode 100755
index 0000000000..cd136963cb
--- /dev/null
+++ b/libraries/base/tests/perf/encodingAllocations.hs
@@ -0,0 +1,30 @@
+{-# 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
+import Distribution.Simple.Utils
+
+
+main :: IO ()
+main = withTempFile "." "encodingAllocations.tmp" (const $ 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)