1 Jun 2011 08:52
Computing the memory footprint of a HashMap ByteString Int (Was: How on Earth Do You Reason about Space?)
Johan Tibell <johan.tibell <at> gmail.com>
2011-06-01 06:52:04 GMT
2011-06-01 06:52:04 GMT
Hi Aleksandar,
I thought it'd be educational to do some back-of-the-envelope
calculations to see how much memory we'd expect to use to store words
in a HashMap ByteString Int. First, lets start by looking at how much
memory one ByteString uses. Here's the definition of ByteString [1]:
data ByteString = PS {-# UNPACK #-} !(ForeignPtr Word8)
{-# UNPACK #-} !Int -- offset
{-# UNPACK #-} !Int -- length
The two Int fields are used to support O(1) slicing of ByteStrings.
We also need the definitions of ForeignPtr [2] and Int.
data ForeignPtr a = ForeignPtr Addr# ForeignPtrContents
data ForeignPtrContents
= PlainForeignPtr !(IORef (Finalizers, [IO ()]))
| MallocPtr (MutableByteArray# RealWorld) !(IORef
(Finalizers, [IO ()]))
| PlainPtr (MutableByteArray# RealWorld)
data Int = I# Int#
The UNPACK indicates to the compiler that it should unpack the
contents of a constructor field into the constructor itself, removing
a level of indirection. We'll end up with a structure that looks like
this:
(Continue reading)
RSS Feed