Opened 9 years ago

Last modified 4 weeks ago

#3094 new bug

Some GHC.* module should export word size and heap object header size

Reported by: duncan Owned by:
Priority: normal Milestone: 8.6.1
Component: libraries (other) Version: 6.10.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Some code, like bytestring, uses magic constants to help it decide how much memory to allocate. This is not a correctness issue, just an optimisation one. It allows the bytestring package to allocate buffers that make optimal use of ghc's pinned allocator strategy which has to use a whole number of 4k pages. The magic is in subtracting the heap object header size:

-- | Currently set to 32k, less the memory management overhead
defaultChunkSize :: Int
defaultChunkSize = 32 * k - chunkOverhead
   where k = 1024

-- | Currently set to 4k, less the memory management overhead
smallChunkSize :: Int
smallChunkSize = 4 * k - chunkOverhead
   where k = 1024

-- | The memory management overhead. Currently this is tuned for GHC only.
chunkOverhead :: Int
chunkOverhead = 2 * sizeOf (undefined :: Int)

This works for normal builds but for profiling builds it is a couple words off which wastes memory. It would also be be nice if it did not have to use magic constants but if it could import them from GHC.Exts or something.

We would want:

  • Heap object header size, in words (currently 2 normally or 4 for profiling)
  • Word size (4 or 8 bytes)
  • Heap block size (currently 4k)

These should all be actual Haskell constants so that they inline perfectly into calculations with no runtime overhead. The heap object header size is obviously different between profiling and normal, however this works out ok because we have to recompile for profiling anyway.

Change History (7)

comment:1 Changed 9 years ago by igloo

difficulty: Unknown
Milestone: 6.12.1

comment:2 Changed 9 years ago by igloo

Would you be happy with GHC.Constants exporting the same things as Constants in the ghc package?

comment:3 in reply to:  2 Changed 9 years ago by duncan

Replying to igloo:

Would you be happy with GHC.Constants exporting the same things as Constants in the ghc package?


comment:4 Changed 9 years ago by igloo

Resolution: fixed
Status: newclosed


Sun Aug  9 19:32:52 BST 2009  Ian Lynagh <>
  * Add a GHC.Constants module; fixes trac #3094

comment:5 Changed 3 years ago by thomie

Resolution: fixed
Status: closednew
Type of failure: None/Unknown

GHC.Constants doesn't export anything at the moment, but there's a comment that says:

-- TODO: This used to include HaskellConstants.hs, but that has now gone.
-- We probably want to include the constants in platformConstants somehow
-- instead.

comment:6 Changed 12 months ago by dfeuer


comment:7 Changed 4 weeks ago by bgamari


This ticket won't be resolved in 8.4; remilestoning for 8.6. Do holler if you are affected by this or would otherwise like to work on it.

Note: See TracTickets for help on using tickets.