Skip to content

Commit 57262d3

Browse files
committed
refactor: simplify memoryUsageInteger by removing MagicHash
Apply the same optimization used in the Logarithmic instance to memoryUsageInteger, using integerLog2 directly instead of unboxed integerLog2# and quotInt# operations. This allows us to remove: - MagicHash language extension - GHC.Exts imports (Int (I#), quotInt#) - GHC.Integer and GHC.Integer.Logarithms imports The refactoring maintains identical functionality while making the code more consistent and simpler.
1 parent 7afcabe commit 57262d3

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

plutus-core/plutus-core/src/PlutusCore/Evaluation/Machine/ExMemoryUsage.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
-- editorconfig-checker-disable-file
22
{-# LANGUAGE FlexibleInstances #-}
3-
{-# LANGUAGE MagicHash #-}
43
{-# LANGUAGE TypeApplications #-}
54
{-# LANGUAGE TypeOperators #-}
65
{-# LANGUAGE UndecidableInstances #-}
@@ -36,9 +35,6 @@ import Data.Text qualified as T
3635
import Data.Vector.Strict (Vector)
3736
import Data.Vector.Strict qualified as Vector
3837
import Data.Word
39-
import GHC.Exts (Int (I#), quotInt#)
40-
import GHC.Integer
41-
import GHC.Integer.Logarithms
4238
import GHC.Natural
4339
import GHC.Num.Integer (integerLog2)
4440
import Universe
@@ -237,12 +233,12 @@ instance ExMemoryUsage () where
237233
64-bit words. This is the default size measure for `Integer`s.
238234
-}
239235
memoryUsageInteger :: Integer -> CostingInteger
240-
-- integerLog2# is unspecified for 0 (but in practice returns -1)
236+
-- integerLog2 is unspecified for 0 (but in practice returns -1)
241237
-- ^ This changed with GHC 9.2: it now returns 0. It's probably safest if we
242238
-- keep this special case for the time being though.
243239
memoryUsageInteger 0 = 1
244-
-- Assume 64 Int
245-
memoryUsageInteger i = fromIntegral $ I# (integerLog2# (abs i) `quotInt#` integerToInt 64) + 1
240+
-- Assume 64-bit words
241+
memoryUsageInteger i = fromIntegral (integerLog2 (abs i) `div` 64 + 1)
246242
-- So that the produced GHC Core doesn't explode in size, we don't win anything by inlining this
247243
-- function anyway.
248244
{-# OPAQUE memoryUsageInteger #-}

0 commit comments

Comments
 (0)