From 4bb2a7ccd431a8655ef96b6fa430e98da9baa7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= <hecate@glitchbra.in> Date: Tue, 21 May 2024 14:50:03 +0200 Subject: [PATCH] [base] Document the memory overhead of ByteArray Add a diagram that shows the constituent parts of a ByteArray and their memory overhead. --- libraries/base/src/Data/Array/Byte.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libraries/base/src/Data/Array/Byte.hs b/libraries/base/src/Data/Array/Byte.hs index f0ca21daf6d8..f954c627e7f1 100644 --- a/libraries/base/src/Data/Array/Byte.hs +++ b/libraries/base/src/Data/Array/Byte.hs @@ -41,6 +41,22 @@ import Prelude -- but wrap and unwrap 'ByteArray' internally as they please -- and use functions from "GHC.Exts". -- +-- The memory representation of a 'ByteArray' is: +-- +-- > â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â•® â•â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â•® +-- > │ Constructor │ * ┼─➤│ Header │ Size │ Payload │ +-- > ╰─────────────┴───╯ ╰────────┴──────┴─────────╯ +-- +-- And its overhead is the following: +-- +-- * 'ByteArray' constructor: 1 word +-- * Pointer to 'ByteArray#': 1 word +-- * 'ByteArray#' Header: 1 word +-- * 'ByteArray#' Size: 1 word +-- +-- Where a word is the unit of heap allocation, +-- measuring 8 bytes on 64-bit systems, and 4 bytes on 32-bit systems. +-- -- @since 4.17.0.0 data ByteArray = ByteArray ByteArray# -- GitLab