From c8d543711e53ff2ed4b9530f2f9983e7d30cc405 Mon Sep 17 00:00:00 2001 From: teodanciu Date: Tue, 24 May 2022 02:16:28 +0100 Subject: [PATCH] Introduce `HeapWords` package copied from `cardano-prelude` with slightly modified imports in order to not depend on cardano-prelude package --- cabal.project | 1 + heapwords/LICENSE | 53 +++ heapwords/NOTICE | 13 + heapwords/heapwords.cabal | 43 ++ heapwords/src/Cardano/HeapWords.hs | 649 +++++++++++++++++++++++++++++ hie-cabal.yaml | 3 + 6 files changed, 762 insertions(+) create mode 100644 heapwords/LICENSE create mode 100644 heapwords/NOTICE create mode 100644 heapwords/heapwords.cabal create mode 100644 heapwords/src/Cardano/HeapWords.hs diff --git a/cabal.project b/cabal.project index fbd053013c..817d8a3d25 100644 --- a/cabal.project +++ b/cabal.project @@ -10,6 +10,7 @@ packages: cardano-crypto-class cardano-crypto-praos cardano-crypto-tests + heapwords measures orphans-deriving-via slotting diff --git a/heapwords/LICENSE b/heapwords/LICENSE new file mode 100644 index 0000000000..0c8a80022e --- /dev/null +++ b/heapwords/LICENSE @@ -0,0 +1,53 @@ +Apache License + +Version 2.0, January 2004 + +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of this License; and +You must cause any modified files to carry prominent notices stating that You changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS diff --git a/heapwords/NOTICE b/heapwords/NOTICE new file mode 100644 index 0000000000..c022073680 --- /dev/null +++ b/heapwords/NOTICE @@ -0,0 +1,13 @@ +Copyright 2021 Input Output (Hong Kong) Ltd. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/heapwords/heapwords.cabal b/heapwords/heapwords.cabal new file mode 100644 index 0000000000..01c33dfde9 --- /dev/null +++ b/heapwords/heapwords.cabal @@ -0,0 +1,43 @@ +cabal-version: >=1.10 + +name: heapwords +version: 0.1.0.0 +synopsis: Heapwords +license: Apache-2.0 +license-files: + LICENSE + NOTICE +author: IOHK +maintainer: operations@iohk.io +copyright: IOHK +build-type: Simple + +flag development + description: Disable `-Werror` + default: False + manual: True + +library + default-language: Haskell2010 + hs-source-dirs: src + ghc-options: + -Wall + -Wcompat + -Wincomplete-record-updates + -Wincomplete-uni-patterns + -Wredundant-constraints + -Wunused-packages + + if (!flag(development)) + ghc-options: + -Werror + exposed-modules: Cardano.HeapWords + build-depends: array + , base >= 4.14 + , bytestring + , containers + , ghc-prim + , integer-gmp + , text + , time + , vector diff --git a/heapwords/src/Cardano/HeapWords.hs b/heapwords/src/Cardano/HeapWords.hs new file mode 100644 index 0000000000..52df3e4d86 --- /dev/null +++ b/heapwords/src/Cardano/HeapWords.hs @@ -0,0 +1,649 @@ +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} + +module Cardano.HeapWords + ( HeapWords(..) + , heapSizeMb + , heapSizeKb + , heapWords0 + , heapWords1 + , heapWords2 + , heapWords3 + , heapWords4 + , heapWords5 + , heapWords6 + , heapWords7 + , heapWords8 + , heapWords9 + , heapWords10 + , heapWords11 + , heapWords12 + , heapWords13 + , heapWordsUArray + , heapWordsUVector + , heapWordsUnpacked + ) +where + +import qualified Data.Array.Unboxed as A +import qualified Data.ByteString as BS +import qualified Data.ByteString.Lazy as LBS +import qualified Data.ByteString.Short as BSS +import qualified Data.IntMap.Strict as IntMap +import qualified Data.IntSet as IntSet +import Data.Sequence (Seq) +import Data.Foldable (toList) +import Data.Ix +import qualified Data.Map.Strict as Map +import qualified Data.Set as Set +import Data.Word (Word8, Word32, Word64) +import Data.Text as Text +import Data.Time (Day, UTCTime) +import qualified Data.Vector as V +import qualified Data.Vector.Unboxed as V.U +import GHC.Integer.GMP.Internals (BigNat(BN#), Integer(S#, Jn#, Jp#)) +import GHC.Natural (Natural(NatS#, NatJ#)) +import GHC.Prim (ByteArray#, sizeofByteArray#) +import GHC.Types (Int(I#)) + +-------------------------------------------------------------------------------- + +-- Module copied from Cardano.Prelude + +-- HeapWords class and instances +-------------------------------------------------------------------------------- + +-- | These functions assume a 64-bit architecture +heapSizeMb, heapSizeKb :: Int -> Int +heapSizeMb w = wordSize * w `div` (1024 * 1024) +heapSizeKb w = wordSize * w `div` 1024 + +wordSize :: Int +wordSize = 8 + +-- | Size in the heap of values, in words (to get the size in bytes multiply by +-- 4 on a 32-bit machine or 8 on a 64-bit machine) +class HeapWords a where + heapWords :: a -> Int + +heapWords0 :: Int +heapWords1 :: HeapWords a => a -> Int +heapWords2 :: (HeapWords a1, HeapWords a) => a -> a1 -> Int +heapWords3 :: (HeapWords a2, HeapWords a1, HeapWords a) => a -> a1 -> a2 -> Int +heapWords4 + :: (HeapWords a3, HeapWords a2, HeapWords a1, HeapWords a) + => a + -> a1 + -> a2 + -> a3 + -> Int +heapWords5 + :: (HeapWords a4, HeapWords a3, HeapWords a2, HeapWords a1, HeapWords a) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> Int +heapWords6 + :: ( HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> Int +heapWords7 + :: ( HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> Int +heapWords8 + :: ( HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> Int +heapWords9 + :: ( HeapWords a8 + , HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> a8 + -> Int +heapWords10 + :: ( HeapWords a9 + , HeapWords a8 + , HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> a8 + -> a9 + -> Int +heapWords11 + :: ( HeapWords a10 + , HeapWords a9 + , HeapWords a8 + , HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> a8 + -> a9 + -> a10 + -> Int +heapWords12 + :: ( HeapWords a11 + , HeapWords a10 + , HeapWords a9 + , HeapWords a8 + , HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> a8 + -> a9 + -> a10 + -> a11 + -> Int +heapWords13 + :: ( HeapWords a12 + , HeapWords a11 + , HeapWords a10 + , HeapWords a9 + , HeapWords a8 + , HeapWords a7 + , HeapWords a6 + , HeapWords a5 + , HeapWords a4 + , HeapWords a3 + , HeapWords a2 + , HeapWords a1 + , HeapWords a + ) + => a + -> a1 + -> a2 + -> a3 + -> a4 + -> a5 + -> a6 + -> a7 + -> a8 + -> a9 + -> a10 + -> a11 + -> a12 + -> Int + + +heapWords0 = 0 +heapWords1 a = 2 + heapWords a +heapWords2 a b = 3 + heapWords a + heapWords b +heapWords3 a b c = 4 + heapWords a + heapWords b + heapWords c +heapWords4 a b c d = 5 + heapWords a + heapWords b + heapWords c + heapWords d +heapWords5 a b c d e = + 6 + heapWords a + heapWords b + heapWords c + heapWords d + heapWords e +heapWords6 a b c d e f = + 7 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f +heapWords7 a b c d e f g = + 8 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g +heapWords8 a b c d e f g h = + 9 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h +heapWords9 a b c d e f g h i = + 10 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h + + heapWords i +heapWords10 a b c d e f g h i j = + 11 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h + + heapWords i + + heapWords j +heapWords11 a b c d e f g h i j k = + 12 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h + + heapWords i + + heapWords j + + heapWords k +heapWords12 a b c d e f g h i j k l = + 13 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h + + heapWords i + + heapWords j + + heapWords k + + heapWords l +heapWords13 a b c d e f g h i j k l m = + 14 + + heapWords a + + heapWords b + + heapWords c + + heapWords d + + heapWords e + + heapWords f + + heapWords g + + heapWords h + + heapWords i + + heapWords j + + heapWords k + + heapWords l + + heapWords m + + +instance HeapWords (a -> b) where + heapWords _ = 0 + +instance HeapWords Int where + heapWords _ = 2 + +instance HeapWords Word where + heapWords _ = 2 + +instance HeapWords Word8 where + heapWords _ = 2 + +instance HeapWords Word32 where + heapWords _ = 2 + +instance HeapWords Word64 where + heapWords _ = 2 + +instance HeapWords Char where + -- For 'Char' there is a special case, where for chars <= 255 the garbage + -- collector will replace them with pointers to statically allocated ones. + -- Bigger 'Char's remain as a 2-word heap object. However, if we assume that + -- 'Char's are mainly ASCII, then these pointers will be in the pointers + -- section of the containing object, and therefore the storage required by + -- this will be accounted for elsewhere. + heapWords _ = 0 + +instance HeapWords Bool where + -- There's a special optimization in GHC for nullary constructors, such that + -- they get allocated once and shared. So if we consider the amortized size + -- over all occurrences in a program, @heapWords@ for these constructors + -- tends to 0. + heapWords _ = 0 + +instance HeapWords Integer where + heapWords (S# _) + -- We have + -- + -- > S# !Int# + -- + -- so @(S# !Int)@ requires: + -- + -- - 1 word for the 'S#' object header + -- - 1 word for the single 'S#' unboxed field, of type 'Int#' + -- + -- ┌──┬──────┐ + -- │S#│ Int# │ + -- └──┴──────┘ + -- + = 2 + heapWords (Jp# bigNat) + -- We have + -- + -- > Jp# {-# UNPACK #-} !BigNat + -- > data BigNat = BN# ByteArray# + -- + -- so @Jp# {-# UNPACK #-} !BigNat@ requires: + -- + -- - 1 word for the 'Jp#' object header + -- - 1 word for the pointer to the byte array object + -- - 1 word for the byte array object header + -- - 1 word for the size of the byte array payload in bytes + -- - the heap words required for the byte array payload + -- + -- Note that for the sake of uniformity, we use 'heapWordsUnpacked' to + -- account for the level of indirection removed by the @UNPACK@ pragma. + -- + -- ┌───┬───┐ + -- │Jp#│ ◉ │ + -- └───┴─╂─┘ + -- ▼ + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + -- + = 2 + heapWordsUnpacked bigNat + heapWords (Jn# bigNat) + -- We have + -- + -- > Jn# {-# UNPACK #-} !BigNat + -- > data BigNat = BN# ByteArray# + -- + -- so @Jn# {-# UNPACK #-} !BigNat@ requires: + -- + -- - 1 word for the 'Jn#' object header + -- - 1 word for the pointer to the byte array object + -- - 1 word for the byte array object header + -- - 1 word for the size of the byte array payload in bytes + -- - the heap words required for the byte array payload + -- + -- Note that for the sake of uniformity, we use 'heapWordsUnpacked' to + -- account for the level of indirection removed by the @UNPACK@ pragma. + -- + -- ┌───┬───┐ + -- │Jn#│ ◉ │ + -- └───┴─╂─┘ + -- ▼ + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + -- + = 2 + heapWordsUnpacked bigNat + +instance HeapWords Float where + heapWords _ = 2 + +instance HeapWords UTCTime where + heapWords _ = 7 + +instance HeapWords Day where + heapWords _ = 2 + +instance HeapWords a => HeapWords [a] where + heapWords [] = heapWords0 + heapWords (x:xs) = heapWords2 x xs + +instance (HeapWords a, HeapWords b) => HeapWords (a,b) where + heapWords (a,b) = heapWords2 a b + +instance (HeapWords a, HeapWords b, HeapWords c) => HeapWords (a,b,c) where + heapWords (a,b,c) = heapWords3 a b c + +instance (HeapWords a, HeapWords b, HeapWords c, HeapWords d) => HeapWords (a,b,c,d) where + heapWords (a,b,c,d) = heapWords4 a b c d + +instance HeapWords a => HeapWords (Maybe a) where + heapWords Nothing = heapWords0 + heapWords (Just a) = heapWords1 a + +instance (HeapWords a, HeapWords b) => HeapWords (Either a b) where + heapWords (Left a) = heapWords1 a + heapWords (Right b) = heapWords1 b + +instance (HeapWords a, HeapWords b) => HeapWords (Map.Map a b) where + heapWords m = sum [ 6 + heapWords k + heapWords v | (k,v) <- Map.toList m ] + +instance HeapWords a => HeapWords (IntMap.IntMap a) where + heapWords m = sum [ 8 + heapWords v | v <- IntMap.elems m ] + +instance HeapWords a => HeapWords (Set.Set a) where + heapWords m = sum [ 5 + heapWords v | v <- Set.elems m ] + +instance HeapWords IntSet.IntSet where + heapWords s = 4 * IntSet.size s --estimate + +instance HeapWords a => HeapWords (Seq a) where + heapWords s = sum [ 5 + heapWords v | v <- toList s ] --estimate + +instance HeapWords BS.ByteString where + heapWords s = let (w,t) = divMod (BS.length s) wordSize + in 5 + w + signum t + +instance HeapWords BSS.ShortByteString where + heapWords s + -- We have + -- + -- > data ShortByteString = SBS ByteArray# + -- + -- so @SBS ByteArray#@ requires: + -- + -- - 1 word for the 'SBS' object header + -- - 1 word for the pointer to the byte array object + -- - 1 word for the byte array object header + -- - 1 word for the size of the byte array payload in bytes + -- - the heap words required for the byte array payload + -- + -- ┌───┬───┐ + -- │SBS│ ◉ │ + -- └───┴─╂─┘ + -- ▼ + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + -- + = let (w,t) = divMod (BSS.length s) wordSize + in 4 + w + signum t + +instance HeapWords LBS.ByteString where + heapWords s = sum [ 1 + heapWords c | c <- LBS.toChunks s ] + +instance HeapWords Text.Text where + heapWords s = let (w,t) = divMod (Text.length s) (wordSize `div` 2) + in 5 + w + signum t + +heapWordsUArray :: (Ix i, A.IArray a e) => Int -> a i e -> Int +heapWordsUArray sz a = 13 + (rangeSize (A.bounds a) * sz) `div` wordSize + +instance HeapWords e => HeapWords (V.Vector e) where + heapWords a = 5 + V.length a + V.foldl' (\s e -> s + heapWords e) 0 a + +heapWordsUVector :: V.U.Unbox e => Int -> V.U.Vector e -> Int +heapWordsUVector sz a = 5 + (V.U.length a * sz) `div` wordSize + +instance HeapWords Natural where + heapWords (NatS# _) + -- We have + -- + -- > NatS# GmpLimb# + -- > type GmpLimb# = Word# + -- + -- so @(NatS# n)@ requires: + -- + -- - 1 word for the header 'NatS#' object header + -- - 1 word for the single 'NatS#' unboxed field, of type 'Word#' + -- + -- ┌─────┬───────┐ + -- │NatS#│ Word#'│ + -- └─────┴───────┘ + -- + = 1 + 1 + heapWords (NatJ# bn) + -- We have + -- + -- > NatJ# {-# UNPACK #-} !BigNat + -- + -- so @NatJ# bn@ requires: + -- + -- - 1 word for the 'NatJ#' object header + -- - 1 word for the pointer to the byte array object + -- - the heap words required by the byte array object + -- + -- Note that for the sake of uniformity, we use 'heapWordsUnpacked' to + -- account for the level of indirection removed by the @UNPACK@ pragma. + -- + -- ┌─────┬───┐ + -- │NatJ#│ ◉ │ + -- └─────┴─╂─┘ + -- ▼ + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + -- + = 1 + 1 + heapWordsUnpacked bn + +instance HeapWords BigNat where + heapWords (BN# arr) = + -- We have + -- + -- > data BigNat = BN# ByteArray# + -- + -- so @BN# ByteArray#@ requires: + -- + -- - 1 word for the @BN#@ object header + -- - 1 word for the pointer to the byte array + -- - the words used by the byte array (see 'heapWordsByteArray#'). + -- + -- ┌──────┬───┐ + -- │BigNat│ ◉ │ + -- └──────┴─╂─┘ + -- ▼ + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + 1 + 1 + heapWordsByteArray# arr + +-- | Calculate the heap words required to store a 'ByteArray#' object. +-- +heapWordsByteArray# :: ByteArray# -> Int +heapWordsByteArray# ba# = 2 + n + -- We require: + -- + -- - 2 for the 'ByteArray#' heap object (1 for header, and 1 for storing its + -- size) + -- - @n@ for the variable sized part + -- + -- ┌───┬───┬───┬─┈ ┈─┬───┐ + -- │BA#│ sz│ │ │ │ 2 + n Words + -- └───┴───┴───┴─┈ ┈─┴───┘ + where + n = 1 + ((nbytes - 1) `div` wordSize) + nbytes = I# (sizeofByteArray# ba#) + +-- | Calculate the number of heap words used by a field unpacked within another +-- constructor. +-- +-- This function simply subtracts 2 from the 'heapWords' result of its +-- parameter, since in the case of an unpacked field we _do not_ have to use: +-- +-- - a word for the pointer to the inner structure. +-- - a word for the constructor that is being unpacked. +-- +heapWordsUnpacked :: HeapWords a => a -> Int +heapWordsUnpacked x = heapWords x - 2 diff --git a/hie-cabal.yaml b/hie-cabal.yaml index 071f2696fc..7516a42abf 100644 --- a/hie-cabal.yaml +++ b/hie-cabal.yaml @@ -23,6 +23,9 @@ cradle: - path: "cardano-crypto-tests/test" component: "cardano-crypto-tests:test:test-crypto" + + - path: "heapwords" + component: "lib:heapwords" - path: "measures/src" component: "lib:measures"