Skip to content

Commit 4d053a2

Browse files
committed
[AST] [Performance] Use 'Array' instead of 'Vector'
1 parent 2596b5b commit 4d053a2

File tree

14 files changed

+43
-47
lines changed

14 files changed

+43
-47
lines changed

plutus-core/plutus-core/src/PlutusCore/Compiler/Erase.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module PlutusCore.Compiler.Erase (eraseTerm, eraseProgram) where
22

3-
import Data.Vector (fromList)
3+
import GHC.IsList (fromList)
44
import PlutusCore.Core
55
import PlutusCore.Name.Unique
66
import UntypedPlutusCore.Core qualified as UPLC

plutus-core/plutus-ir/src/PlutusIR/Purity.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ termEvaluationOrder binfo vinfo = goTerm
194194
-- first the body
195195
goTerm b
196196
-- then the whole term, but this is erased so it is work-free
197-
<> evalThis (EvalTerm Pure WorkFree t)
197+
<> evalThis (EvalTerm MaybeImpure WorkFree t)
198198
t@(Constr _ _ _ ts) ->
199199
-- first the arguments, in left-to-right order
200200
foldMap goTerm ts

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Instance/Eq.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import PlutusCore.Rename.Monad
2020
import Universe
2121

2222
import Data.Hashable
23-
import Data.Vector qualified as V
23+
import Data.Primitive.Array (Array)
2424

2525
instance (GEq uni, Closed uni, uni `Everywhere` Eq, Eq fun, Eq ann) =>
2626
Eq (Term Name uni fun ann) where
@@ -37,7 +37,7 @@ type HashableTermConstraints uni fun ann =
3737

3838
-- This instance is the only logical one, and exists also in the package `vector-instances`.
3939
-- Since this is the same implementation as that one, there isn't even much risk of incoherence.
40-
instance Hashable a => Hashable (V.Vector a) where
40+
instance Hashable a => Hashable (Array a) where
4141
hashWithSalt s = hashWithSalt s . toList
4242

4343
instance HashableTermConstraints uni fun ann => Hashable (Term Name uni fun ann)

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Instance/Flat.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import UntypedPlutusCore.Core.Type
1919

2020
import Control.Lens
2121
import Control.Monad
22-
import Data.Vector qualified as V
2322
import Flat
2423
import Flat.Decoder
2524
import Flat.Encoder
2625
import Flat.Encoder.Strict (sizeListWith)
26+
import GHC.IsList (fromList)
2727
import Universe
2828

2929
{-
@@ -123,7 +123,7 @@ encodeTerm = \case
123123
Error ann -> encodeTermTag 6 <> encode ann
124124
Builtin ann bn -> encodeTermTag 7 <> encode ann <> encode bn
125125
Constr ann i es -> encodeTermTag 8 <> encode ann <> encode i <> encodeListWith encodeTerm es
126-
Case ann arg cs -> encodeTermTag 9 <> encode ann <> encodeTerm arg <> encodeListWith encodeTerm (V.toList cs)
126+
Case ann arg cs -> encodeTermTag 9 <> encode ann <> encodeTerm arg <> encodeListWith encodeTerm (toList cs)
127127

128128
decodeTerm
129129
:: forall name uni fun ann
@@ -160,7 +160,7 @@ decodeTerm version builtinPred = go
160160
Constr <$> decode <*> decode <*> decodeListWith go
161161
handleTerm 9 = do
162162
unless (version >= PLC.plcVersion110) $ fail $ "'case' is not allowed before version 1.1.0, this program has version: " ++ (show $ pretty version)
163-
Case <$> decode <*> go <*> (V.fromList <$> decodeListWith go)
163+
Case <$> decode <*> go <*> (fromList <$> decodeListWith go)
164164
handleTerm t = fail $ "Unknown term constructor tag: " ++ show t
165165

166166
sizeTerm
@@ -188,7 +188,7 @@ sizeTerm tm sz =
188188
Error ann -> size ann sz'
189189
Builtin ann bn -> size ann $ size bn sz'
190190
Constr ann i es -> size ann $ size i $ sizeListWith sizeTerm es sz'
191-
Case ann arg cs -> size ann $ sizeTerm arg $ sizeListWith sizeTerm (V.toList cs) sz'
191+
Case ann arg cs -> size ann $ sizeTerm arg $ sizeListWith sizeTerm (toList cs) sz'
192192

193193
-- | An encoder for programs.
194194
--

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Instance/Scoping.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import PlutusCore.Name.Unique
1515
import PlutusCore.Quote
1616

1717
import Data.Proxy
18-
import Data.Vector qualified as Vector
18+
import GHC.IsList (fromList)
1919

2020
firstBound :: Term name uni fun ann -> [name]
2121
firstBound (Apply _ (LamAbs _ name body) _) = name : firstBound body
@@ -41,15 +41,15 @@ instance name ~ Name => EstablishScoping (Term name uni fun) where
4141
establishScoping (Constr _ i es) = Constr NotAName <$> pure i <*> traverse establishScoping es
4242
establishScoping (Case _ a es) = do
4343
esScoped <- traverse establishScoping es
44-
let esScopedPoked = addTheRest . map (\e -> (e, firstBound e)) $ Vector.toList esScoped
44+
let esScopedPoked = addTheRest . map (\e -> (e, firstBound e)) $ toList esScoped
4545
branchBounds = map (snd . fst) esScopedPoked
4646
referenceInBranch ((branch, _), others) = referenceOutOfScope (map snd others) branch
4747
aScoped <- establishScoping a
4848
-- For each of the branches reference (as out-of-scope) the variables bound in that branch
4949
-- in all the other ones, as well as outside of the whole case-expression. This is to check
5050
-- that none of the transformations leak variables outside of the branch they're bound in.
5151
pure . referenceOutOfScope branchBounds $
52-
Case NotAName aScoped . Vector.fromList $ map referenceInBranch esScopedPoked
52+
Case NotAName aScoped . fromList $ map referenceInBranch esScopedPoked
5353

5454
instance name ~ Name => EstablishScoping (Program name uni fun) where
5555
establishScoping (Program _ ver term) = Program NotAName ver <$> establishScoping term

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Type.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ module UntypedPlutusCore.Core.Type
2828
import Control.Lens
2929
import PlutusPrelude
3030

31-
import Data.Vector
31+
import Data.Primitive.Array (Array)
3232
import Data.Word
33+
import GHC.IsList (fromList)
3334
import PlutusCore.Builtin qualified as TPLC
3435
import PlutusCore.Core qualified as TPLC
3536
import PlutusCore.MkPlc
@@ -86,7 +87,7 @@ data Term name uni fun ann
8687
-- TODO: try spine-strict list or strict list or vector
8788
-- See Note [Constr tag type]
8889
| Constr !ann !Word64 ![Term name uni fun ann]
89-
| Case !ann !(Term name uni fun ann) !(Vector (Term name uni fun ann))
90+
| Case !ann !(Term name uni fun ann) {-# UNPACK #-} !(Array (Term name uni fun ann))
9091
deriving stock (Functor, Generic)
9192

9293
deriving stock instance (Show name, GShow uni, Everywhere uni Show, Show fun, Show ann, Closed uni)

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Zip.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module UntypedPlutusCore.Core.Zip
1010

1111
import Control.Monad (void, when)
1212
import Control.Monad.Except (MonadError, throwError)
13-
import Data.Vector
13+
import GHC.IsList (fromList, toList)
1414
import UntypedPlutusCore.Core.Instance.Eq ()
1515
import UntypedPlutusCore.Core.Type
1616

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Evaluation/Machine/Cek/Internal.hs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ import Data.DList qualified as DList
9696
import Data.Functor.Identity
9797
import Data.Hashable (Hashable)
9898
import Data.Kind qualified as GHC
99+
import Data.Primitive.Array (Array, indexArray, sizeofArray)
99100
import Data.Proxy
100101
import Data.Semigroup (stimes)
101102
import Data.Text (Text)
102-
import Data.Vector qualified as V
103103
import Data.Word
104104
import GHC.Generics
105105
import GHC.TypeLits
106106
import Prettyprinter
107107
import Universe
108+
import Unsafe.Coerce (unsafeCoerce)
108109

109110
{- Note [Compilation peculiarities]
110111
READ THIS BEFORE TOUCHING ANYTHING IN THIS FILE
@@ -600,7 +601,7 @@ data Context uni fun ann
600601
-- See Note [Accumulators for terms]
601602
| FrameConstr !(CekValEnv uni fun ann) {-# UNPACK #-} !Word64 ![NTerm uni fun ann] !(ArgStack uni fun ann) !(Context uni fun ann)
602603
-- ^ @(constr i V0 ... Vj-1 _ Nj ... Nn)@
603-
| FrameCases !(CekValEnv uni fun ann) !(V.Vector (NTerm uni fun ann)) !(Context uni fun ann)
604+
| FrameCases !(CekValEnv uni fun ann) {-# UNPACK #-} !(Array (NTerm uni fun ann)) !(Context uni fun ann)
604605
-- ^ @(case _ C0 .. Cn)@
605606
| NoFrame
606607

@@ -775,16 +776,11 @@ enterComputeCek = computeCek
775776
[] -> returnCek ctx $ VConstr i done'
776777
-- s , case _ (C0 ... CN, ρ) ◅ constr i V1 .. Vm ↦ s , [_ V1 ... Vm] ; ρ ▻ Ci
777778
returnCek (FrameCases env cs ctx) e = case e of
778-
-- If the index is larger than the max bound of an Int, or negative, then it's a bad index
779-
-- As it happens, this will currently never trigger, since i is a Word64, and the largest
780-
-- Word64 value wraps to -1 as an Int64. So you can't wrap around enough to get an
781-
-- "apparently good" value.
782-
(VConstr i _) | fromIntegral @_ @Integer i > fromIntegral @Int @Integer maxBound ->
783-
throwingDischarged _MachineError (MissingCaseBranchMachineError i) e
784-
-- Otherwise, we can safely convert the index to an Int and use it
785-
(VConstr i args) -> case (V.!?) cs (fromIntegral i) of
786-
Just t -> computeCek (transferArgStack args ctx) env t
787-
Nothing -> throwingDischarged _MachineError (MissingCaseBranchMachineError i) e
779+
VConstr i args
780+
| i < unsafeCoerce (sizeofArray cs) ->
781+
computeCek (transferArgStack args ctx) env . indexArray cs $ unsafeCoerce i
782+
| otherwise ->
783+
throwingDischarged _MachineError (MissingCaseBranchMachineError i) e
788784
_ -> throwingDischarged _MachineError NonConstrScrutinizedMachineError e
789785

790786
-- | Evaluate a 'HeadSpine' by pushing the arguments (if any) onto the stack and proceeding with

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Evaluation/Machine/SteppableCek/Internal.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ import UntypedPlutusCore.Evaluation.Machine.Cek.StepCounter
5757
import Control.Lens hiding (Context)
5858
import Control.Monad
5959
import Control.Monad.Primitive
60+
import Data.Primitive.Array (Array)
6061
import Data.Proxy
6162
import Data.RandomAccessList.Class qualified as Env
6263
import Data.Semigroup (stimes)
6364
import Data.Text (Text)
64-
import Data.Vector qualified as V
6565
import Data.Word (Word64)
6666
import GHC.TypeNats
6767
import Universe
@@ -100,7 +100,7 @@ data Context uni fun ann
100100
| FrameAwaitFunValue ann !(CekValue uni fun ann) !(Context uni fun ann)
101101
| FrameForce ann !(Context uni fun ann) -- ^ @(force _)@
102102
| FrameConstr ann !(CekValEnv uni fun ann) {-# UNPACK #-} !Word64 ![NTerm uni fun ann] !(ArgStack uni fun ann) !(Context uni fun ann)
103-
| FrameCases ann !(CekValEnv uni fun ann) !(V.Vector (NTerm uni fun ann)) !(Context uni fun ann)
103+
| FrameCases ann !(CekValEnv uni fun ann) !(Array (NTerm uni fun ann)) !(Context uni fun ann)
104104
| NoFrame
105105

106106
deriving stock instance (GShow uni, Everywhere uni Show, Show fun, Show ann, Closed uni)
@@ -206,7 +206,7 @@ returnCek (FrameCases ann env cs ctx) e = case e of
206206
-- "apparently good" value.
207207
(VConstr i _) | fromIntegral @_ @Integer i > fromIntegral @Int @Integer maxBound ->
208208
throwingDischarged _MachineError (MissingCaseBranchMachineError i) e
209-
(VConstr i args) -> case (V.!?) cs (fromIntegral i) of
209+
(VConstr i args) -> case toList cs ^? ix (fromIntegral i) of
210210
Just t ->
211211
let ctx' = transferArgStack ann args ctx
212212
in computeCek ctx' env t

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Parser.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import UntypedPlutusCore.Core.Type qualified as UPLC
2828
import UntypedPlutusCore.Rename (Rename (rename))
2929

3030
import Data.Text (Text)
31-
import Data.Vector qualified as V
31+
import GHC.IsList (fromList)
3232
import PlutusCore.Error (AsParserErrorBundle)
3333
import PlutusCore.MkPlc (mkIterApp)
3434
import PlutusCore.Parser hiding (parseProgram, parseTerm, program)
@@ -82,7 +82,7 @@ constrTerm = withSpan $ \sp ->
8282
caseTerm :: Parser PTerm
8383
caseTerm = withSpan $ \sp ->
8484
inParens $ do
85-
res <- UPLC.Case sp <$> (symbol "case" *> term) <*> (V.fromList <$> many term)
85+
res <- UPLC.Case sp <$> (symbol "case" *> term) <*> (fromList <$> many term)
8686
whenVersion (\v -> v < plcVersion110) $ fail "'case' is not allowed before version 1.1.0"
8787
pure res
8888

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Transform/CaseReduce.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import UntypedPlutusCore.Core
99
import UntypedPlutusCore.Transform.Simplifier (SimplifierStage (CaseReduce), SimplifierT,
1010
recordSimplification)
1111

12-
import Control.Lens (transformOf)
13-
import Data.Vector qualified as V
12+
import Control.Lens (ix, transformOf, (^?))
13+
import Data.Foldable (toList)
1414

1515
caseReduce
1616
:: Monad m
@@ -23,6 +23,6 @@ caseReduce term = do
2323

2424
processTerm :: Term name uni fun a -> Term name uni fun a
2525
processTerm = \case
26-
Case ann (Constr _ i args) cs | Just c <- (V.!?) cs (fromIntegral i) ->
26+
Case ann (Constr _ i args) cs | Just c <- toList cs ^? ix (fromIntegral i) ->
2727
mkIterApp c ((ann,) <$> args)
2828
t -> t

plutus-core/untyped-plutus-core/test/Generators.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-- | UPLC property tests (pretty-printing\/parsing and binary encoding\/decoding).
77
module Generators where
88

9-
import PlutusPrelude (display, fold, on, void, zipExact, (&&&))
9+
import PlutusPrelude (display, fold, on, toList, void, zipExact, (&&&))
1010

1111
import PlutusCore (Name, _nameText)
1212
import PlutusCore.Annotation
@@ -28,7 +28,6 @@ import UntypedPlutusCore.Parser (parseProgram, parseTerm)
2828
import Control.Lens (view)
2929
import Data.Text (Text)
3030
import Data.Text qualified as T
31-
import Data.Vector qualified as V
3231

3332
import Hedgehog (annotate, annotateShow, failure, property, tripping, (===))
3433
import Hedgehog.Gen qualified as Gen
@@ -61,7 +60,7 @@ compareTerm (Delay _ t ) (Delay _ t') = compareTerm t t'
6160
compareTerm (Constant _ x) (Constant _ y) = x == y
6261
compareTerm (Builtin _ bi) (Builtin _ bi') = bi == bi'
6362
compareTerm (Constr _ i es) (Constr _ i' es') = i == i' && maybe False (all (uncurry compareTerm)) (zipExact es es')
64-
compareTerm (Case _ arg cs) (Case _ arg' cs') = compareTerm arg arg' && maybe False (all (uncurry compareTerm)) (zipExact (V.toList cs) (V.toList cs'))
63+
compareTerm (Case _ arg cs) (Case _ arg' cs') = compareTerm arg arg' && maybe False (all (uncurry compareTerm)) (zipExact (toList cs) (toList cs'))
6564
compareTerm (Error _ ) (Error _ ) = True
6665
compareTerm _ _ = False
6766

plutus-core/untyped-plutus-core/test/Transform/CaseOfCase/Test.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Transform.CaseOfCase.Test where
66

77
import Data.ByteString.Lazy qualified as BSL
88
import Data.Text.Encoding (encodeUtf8)
9-
import Data.Vector qualified as V
9+
import GHC.IsList (fromList)
1010
import PlutusCore qualified as PLC
1111
import PlutusCore.Evaluation.Machine.BuiltinCostModel (BuiltinCostModel)
1212
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults (defaultBuiltinCostModelForTesting,
@@ -45,7 +45,7 @@ caseOfCase1 = runQuote do
4545
let ite = Force () (Builtin () PLC.IfThenElse)
4646
true = Constr () 0 []
4747
false = Constr () 1 []
48-
alts = V.fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
48+
alts = fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
4949
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
5050

5151
{- | This should not simplify, because one of the branches of `ifThenElse` is not a `Constr`.
@@ -59,7 +59,7 @@ caseOfCase2 = runQuote do
5959
let ite = Force () (Builtin () PLC.IfThenElse)
6060
true = Var () t
6161
false = Constr () 1 []
62-
alts = V.fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
62+
alts = fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
6363
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
6464

6565
{- | Similar to `caseOfCase1`, but the type of the @true@ and @false@ branches is
@@ -76,7 +76,7 @@ caseOfCase3 = runQuote do
7676
false = Constr () 1 []
7777
altTrue = Var () f
7878
altFalse = mkConstant @Integer () 2
79-
alts = V.fromList [altTrue, altFalse]
79+
alts = fromList [altTrue, altFalse]
8080
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
8181

8282
{- |
@@ -107,7 +107,7 @@ caseOfCaseWithError =
107107
, ((), Constr () 1 []) -- False
108108
]
109109
)
110-
(V.fromList [mkConstant @() () (), Error ()])
110+
(fromList [mkConstant @() () (), Error ()])
111111

112112
testCaseOfCaseWithError :: TestTree
113113
testCaseOfCaseWithError =

plutus-core/untyped-plutus-core/test/Transform/Simplify.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module Transform.Simplify where
55

66
import Data.Text (Text)
7-
import Data.Vector qualified as V
7+
import GHC.IsList (fromList)
88
import PlutusCore qualified as PLC
99
import PlutusCore.MkPlc (mkConstant, mkIterApp, mkIterAppNoAnn)
1010
import PlutusCore.Quote (Quote, freshName, runQuote)
@@ -34,7 +34,7 @@ caseOfCase1 = runQuote $ do
3434
let ite = Force () (Builtin () PLC.IfThenElse)
3535
true = Constr () 0 []
3636
false = Constr () 1 []
37-
alts = V.fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
37+
alts = fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
3838
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
3939

4040
{- | This should not simplify, because one of the branches of `ifThenElse` is not a `Constr`.
@@ -48,7 +48,7 @@ caseOfCase2 = runQuote $ do
4848
let ite = Force () (Builtin () PLC.IfThenElse)
4949
true = Var () t
5050
false = Constr () 1 []
51-
alts = V.fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
51+
alts = fromList [mkConstant @Integer () 1, mkConstant @Integer () 2]
5252
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
5353

5454
{- | Similar to `caseOfCase1`, but the type of the @true@ and @false@ branches is
@@ -65,7 +65,7 @@ caseOfCase3 = runQuote $ do
6565
false = Constr () 1 []
6666
altTrue = Var () f
6767
altFalse = mkConstant @Integer () 2
68-
alts = V.fromList [altTrue, altFalse]
68+
alts = fromList [altTrue, altFalse]
6969
pure $ Case () (mkIterApp ite [((), Var () b), ((), true), ((), false)]) alts
7070

7171
-- | The `Delay` should be floated into the lambda.
@@ -408,7 +408,7 @@ cse1 = runQuote $ do
408408
branch1 = plus onePlusTwoPlusX threePlusX
409409
branch2 = plus twoPlusX threePlusX
410410
branch3 = fourPlusX
411-
caseExpr = Case () (Var () y) (V.fromList [branch1, branch2, branch3])
411+
caseExpr = Case () (Var () y) (fromList [branch1, branch2, branch3])
412412
pure $ LamAbs () x (LamAbs () y body)
413413

414414
-- | This is the second example in Note [CSE].

0 commit comments

Comments
 (0)