From 394f953b843fe3a13f1eff575c9d4c46e8fa93e7 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Mon, 25 Sep 2023 19:35:50 +1300 Subject: [PATCH] Upload missing file. --- Data/Text/Glyphize/Array.hs | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Data/Text/Glyphize/Array.hs diff --git a/Data/Text/Glyphize/Array.hs b/Data/Text/Glyphize/Array.hs new file mode 100644 index 0000000..5f8173f --- /dev/null +++ b/Data/Text/Glyphize/Array.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE MagicHash, UnboxedTuples #-} +-- | Published almost entirely for benchmarks, comparing to stdlib! +-- Should have little direct interest to Harfbuzz callers, & I'm not promising a stable API. +-- This is here because, as it turns out, Harfbuzz can return a lot of output! +module Data.Text.Glyphize.Array where + +import Foreign.Storable (Storable(..)) +import Foreign.ForeignPtr (ForeignPtr, plusForeignPtr, withForeignPtr, mallocForeignPtrArray) +import Foreign.Ptr +import Foreign.Marshal.Array (copyArray) + +import GHC.IO (IO(IO)) +import GHC.Exts (realWorld#) + +clonePtr ptr l = do + ret <- mallocForeignPtrArray l + withForeignPtr ret $ \ptr' -> copyArray ptr' ptr l + return ret +peekLazy :: Storable a => ForeignPtr a -> Int -> [a] +peekLazy fp 0 = [] +peekLazy fp n + | n <= chunkSize = withFP $ peekEager [] n + | otherwise = withFP $ peekEager (plusForeignPtr fp chunkSize `peekLazy` (-) n chunkSize) chunkSize + where withFP = accursedUnutterablePerformIO . withForeignPtr fp +peekEager acc 0 ptr = return acc +peekEager acc n ptr = let n' = pred n in do + e <- peekElemOff ptr n' + peekEager (e:acc) n' ptr +chunkSize = 1024 -- 4k, benchmarks seem to like it! +iterateLazy :: Storable a => Ptr a -> Int -> IO [a] +iterateLazy ptr l = do + fp <- clonePtr ptr l + return $ peekLazy fp $ fromEnum l + +-- | This \"function\" has a superficial similarity to 'System.IO.Unsafe.unsafePerformIO' but +-- it is in fact a malevolent agent of chaos. It unpicks the seams of reality +-- (and the 'IO' monad) so that the normal rules no longer apply. It lulls you +-- into thinking it is reasonable, but when you are not looking it stabs you +-- in the back and aliases all of your mutable buffers. The carcass of many a +-- seasoned Haskell programmer lie strewn at its feet. +-- +-- Witness the trail of destruction: +-- +-- * +-- +-- * +-- +-- * +-- +-- * +-- +-- * +-- +-- * +-- +-- * +-- +-- Do not talk about \"safe\"! You do not know what is safe! +-- +-- Yield not to its blasphemous call! Flee traveller! Flee or you will be +-- corrupted and devoured! +-- +{-# INLINE accursedUnutterablePerformIO #-} +accursedUnutterablePerformIO :: IO a -> a +accursedUnutterablePerformIO (IO m) = case m realWorld# of (# _, r #) -> r -- 2.30.2