Skip to content

Commit

Permalink
[Evaluation] [Performance] Use 'unsafeShiftL' instead of '*'
Browse files Browse the repository at this point in the history
Added CHANGELOG entry
  • Loading branch information
effectfully authored and bezirg committed Dec 19, 2024
1 parent 8bb1a4b commit df66781
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Changed

- In #6737 made the the CEK creation operation marginally faster by resorting to bit manipulation
12 changes: 7 additions & 5 deletions plutus-core/index-envs/src/Data/RandomAccessList/SkewBinary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Data.RandomAccessList.SkewBinary
, uncons
) where

import Data.Bits (unsafeShiftR)
import Data.Bits (setBit, unsafeShiftL, unsafeShiftR)
import Data.Word
import GHC.Exts

Expand Down Expand Up @@ -51,8 +51,8 @@ null Nil = True
null _ = False
{-# INLINE null #-}

{-# complete Cons, Nil #-}
{-# complete BHead, Nil #-}
{-# COMPLETE Cons, Nil #-}
{-# COMPLETE BHead, Nil #-}

-- /O(1)/
pattern Cons :: a -> RAList a -> RAList a
Expand All @@ -62,8 +62,10 @@ pattern Cons x xs <- (uncons -> Just (x, xs)) where
-- O(1) worst-case
cons :: a -> RAList a -> RAList a
cons x = \case
(BHead w1 t1 (BHead w2 t2 ts')) | w1 == w2 -> BHead (2*w1+1) (Node x t1 t2) ts'
ts -> BHead 1 (Leaf x) ts
(BHead w1 t1 (BHead w2 t2 ts')) | w1 == w2 ->
-- 'unsafeShiftL w1 1 `setBit`0' is supposed to be a faster version of '(2*w1)+1'
BHead (unsafeShiftL w1 1 `setBit` 0) (Node x t1 t2) ts'
ts -> BHead 1 (Leaf x) ts
{-# INLINE cons #-}

-- /O(1)/
Expand Down

0 comments on commit df66781

Please sign in to comment.