ghc-datasize 0.1.2 → 0.2.0
raw patch · 2 files changed
+29/−9 lines, 2 filesdep +deepseqPVP ok
version bump matches the API change (PVP)
Dependencies added: deepseq
API changes (from Hackage documentation)
+ GHC.DataSize: recursiveSizeNF :: NFData a => a -> IO Word
- GHC.DataSize: closureSize :: Num b => a -> IO b
+ GHC.DataSize: closureSize :: a -> IO Word
- GHC.DataSize: recursiveSize :: Num b => a -> IO b
+ GHC.DataSize: recursiveSize :: a -> IO Word
Files
- ghc-datasize.cabal +4/−4
- src/GHC/DataSize.hs +25/−5
ghc-datasize.cabal view
@@ -1,5 +1,5 @@ name: ghc-datasize-version: 0.1.2+version: 0.2.0 license: BSD3 license-file: LICENSE category: GHC, Debug, Development@@ -15,17 +15,17 @@ recursive data structures is supported. All sizes are in Bytes. -tested-with: GHC == 7.4.2, GHC == 7.6.1, GHC == 7.8.3+tested-with: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.1 Library Exposed-modules: GHC.DataSize Default-Language: Haskell2010 Build-depends: base == 4.*,- --containers,+ deepseq >= 1.3 && < 1.5, ghc-heap-view >= 0.5 Hs-source-dirs: src/ Ghc-options: -Wall source-repository head type: git- location: git://r0q.ath.cx/ghc-datasize+ location: https://github.com/def-/ghc-datasize.git
src/GHC/DataSize.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {- | Module : GHC.DataSize@@ -7,11 +8,17 @@ -} module GHC.DataSize ( closureSize,- recursiveSize- --mSize+ recursiveSize,+ recursiveSizeNF ) where +import Control.DeepSeq (NFData, force)++#if __GLASGOW_HASKELL < 708+import Data.Word (Word)+#endif+ import GHC.HeapView hiding (size) import Control.Monad@@ -33,7 +40,7 @@ -- | Calculate size of GHC objects in Bytes. Note that an object may not be -- evaluated yet and only the size of the initial closure is returned.-closureSize :: Num b => a -> IO b+closureSize :: a -> IO Word closureSize x = do (_,y,_) <- getClosureRaw x return . fromIntegral $ length y * wORD_SIZE@@ -56,12 +63,17 @@ -- -- A garbage collection is performed before the size is calculated, because -- the garbage collector would make heap walks difficult.+--+-- This function works very quickly on small data structures, but can be slow+-- on large and complex ones. If speed is an issue it's probably possible to+-- get the exact size of a small portion of the data structure and then+-- estimate the total size from that. -recursiveSize :: Num b => a -> IO b+recursiveSize :: a -> IO Word recursiveSize x = do performGC liftM snd $ go ([], 0) $ asBox x- where go (vs, acc) b@(Box y) = do+ where go (!vs, !acc) b@(Box y) = do isElem <- liftM or $ mapM (areBoxesEqual b) vs if isElem then return (vs, acc)@@ -69,3 +81,11 @@ size <- closureSize y closure <- getClosureData y foldM go (b : vs, acc + size) $ allPtrs closure++-- | Calculate the recursive size of GHC objects in Bytes after calling+-- Control.DeepSeq.force on the data structure to force it into Normal Form.+-- Using this function requires that the data structure has an `NFData`+-- typeclass instance.++recursiveSizeNF :: NFData a => a -> IO Word+recursiveSizeNF = recursiveSize . force