Skip to content

Commit 85884d2

Browse files
committed
Add insertCoin and unionValue costing skeleton
1 parent b12c894 commit 85884d2

File tree

9 files changed

+120
-2
lines changed

9 files changed

+120
-2
lines changed

plutus-core/cost-model/budgeting-bench/Common.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,36 @@ createThreeTermBuiltinBenchWithWrappers (wrapX, wrapY, wrapZ) fun tys xs ys zs =
431431
[mkBM x y z | z <- zs] | y <- ys] | x <- xs]
432432
where mkBM x y z = benchDefault (showMemoryUsage (wrapZ z)) $ mkApp3 fun tys x y z
433433

434+
{- See Note [Adjusting the memory usage of arguments of costing benchmarks]. -}
435+
createFourTermBuiltinBenchElementwiseWithWrappers
436+
:: ( fun ~ DefaultFun
437+
, uni ~ DefaultUni
438+
, uni `HasTermLevel` a
439+
, uni `HasTermLevel` b
440+
, uni `HasTermLevel` c
441+
, uni `HasTermLevel` d
442+
, ExMemoryUsage a'
443+
, ExMemoryUsage b'
444+
, ExMemoryUsage c'
445+
, ExMemoryUsage d'
446+
, NFData a
447+
, NFData b
448+
, NFData c
449+
, NFData d
450+
)
451+
=> (a -> a', b -> b', c -> c', d -> d')
452+
-> fun
453+
-> [Type tyname uni ()]
454+
-> [(a,b,c,d)]
455+
-> Benchmark
456+
createFourTermBuiltinBenchElementwiseWithWrappers (wrapW, wrapX, wrapY, wrapZ) fun tys inputs =
457+
bgroup (show fun) $
458+
fmap
459+
(\(w, x, y, z) ->
460+
bgroup (showMemoryUsage $ wrapW w)
461+
[bgroup (showMemoryUsage $ wrapX x)
462+
[bgroup (showMemoryUsage $ wrapY y) [mkBM w x y z]]
463+
]
464+
)
465+
inputs
466+
where mkBM w x y z = benchDefault (showMemoryUsage $ wrapZ z) $ mkApp4 fun tys w x y z

plutus-core/cost-model/create-cost-model/BuiltinMemoryModels.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,8 @@ builtinMemoryModels = BuiltinCostModelBase
176176
, paramLengthOfArray = Id $ ModelOneArgumentConstantCost 10
177177
, paramListToArray = Id $ ModelOneArgumentLinearInX $ OneVariableLinearFunction 7 1
178178
, paramIndexArray = Id $ ModelTwoArgumentsConstantCost 32
179+
-- Builtin values
180+
, paramInsertCoin = Id $ ModelFourArgumentsConstantCost 1
181+
, paramUnionValue = Id $ ModelTwoArgumentsConstantCost 1
179182
}
180183
where identityFunction = OneVariableLinearFunction 0 1

plutus-core/cost-model/create-cost-model/CreateBuiltinCostModel.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ builtinCostModelNames = BuiltinCostModelBase
131131
, paramLengthOfArray = "lengthOfArrayModel"
132132
, paramListToArray = "listToArrayModel"
133133
, paramIndexArray = "indexArrayModel"
134+
-- Builtin values
135+
, paramInsertCoin = "insertCoinModel"
136+
, paramUnionValue = "unionValueModel"
134137
}
135138

136139

@@ -279,6 +282,9 @@ createBuiltinCostModel bmfile rfile = do
279282
paramLengthOfArray <- getParams readCF1 paramLengthOfArray
280283
paramListToArray <- getParams readCF1 paramListToArray
281284
paramIndexArray <- getParams readCF2 paramIndexArray
285+
-- Builtin values
286+
paramInsertCoin <- getParams readCF4 paramInsertCoin
287+
paramUnionValue <- getParams readCF2 paramUnionValue
282288

283289
pure $ BuiltinCostModelBase {..}
284290

@@ -442,6 +448,13 @@ readCF3 e = do
442448
"exp_mod_cost" -> ModelThreeArgumentsExpModCost <$> readExpModCostingFunction "y_mem" "z_mem" e
443449
_ -> error $ "Unknown three-variable model type: " ++ ty
444450

451+
readCF4 :: MonadR m => SomeSEXP (Region m) -> m ModelFourArguments
452+
readCF4 e = do
453+
ty <- getType e
454+
case ty of
455+
"constant_cost" -> ModelFourArgumentsConstantCost <$> getConstant e
456+
_ -> error $ "Unknown four-variable model type: " ++ ty
457+
445458
readCF6 :: MonadR m => SomeSEXP (Region m) -> m ModelSixArguments
446459
readCF6 e = do
447460
ty <- getType e

plutus-core/cost-model/data/builtinCostModelA.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,5 +1205,25 @@
12051205
"arguments": 4,
12061206
"type": "constant_cost"
12071207
}
1208+
},
1209+
"insertCoin": {
1210+
"cpu": {
1211+
"arguments": 0,
1212+
"type": "constant_cost"
1213+
},
1214+
"memory": {
1215+
"arguments": 0,
1216+
"type": "constant_cost"
1217+
}
1218+
},
1219+
"unionValue": {
1220+
"cpu": {
1221+
"arguments": 0,
1222+
"type": "constant_cost"
1223+
},
1224+
"memory": {
1225+
"arguments": 0,
1226+
"type": "constant_cost"
1227+
}
12081228
}
12091229
}

plutus-core/cost-model/data/builtinCostModelB.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,5 +1205,25 @@
12051205
"arguments": 4,
12061206
"type": "constant_cost"
12071207
}
1208+
},
1209+
"insertCoin": {
1210+
"cpu": {
1211+
"arguments": 1000,
1212+
"type": "constant_cost"
1213+
},
1214+
"memory": {
1215+
"arguments": 10,
1216+
"type": "constant_cost"
1217+
}
1218+
},
1219+
"unionValue": {
1220+
"cpu": {
1221+
"arguments": 1000,
1222+
"type": "constant_cost"
1223+
},
1224+
"memory": {
1225+
"arguments": 10,
1226+
"type": "constant_cost"
1227+
}
12081228
}
12091229
}

plutus-core/cost-model/data/builtinCostModelC.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,5 +1223,25 @@
12231223
"arguments": 4,
12241224
"type": "constant_cost"
12251225
}
1226+
},
1227+
"insertCoin": {
1228+
"cpu": {
1229+
"arguments": 1000,
1230+
"type": "constant_cost"
1231+
},
1232+
"memory": {
1233+
"arguments": 10,
1234+
"type": "constant_cost"
1235+
}
1236+
},
1237+
"unionValue": {
1238+
"cpu": {
1239+
"arguments": 1000,
1240+
"type": "constant_cost"
1241+
},
1242+
"memory": {
1243+
"arguments": 10,
1244+
"type": "constant_cost"
1245+
}
12261246
}
12271247
}

plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
20522052
{-# INLINE insertCoinDenotation #-}
20532053
in makeBuiltinMeaning
20542054
insertCoinDenotation
2055-
(runCostingFunFourArguments . unimplementedCostingFun)
2055+
(runCostingFunFourArguments . paramInsertCoin)
20562056

20572057
toBuiltinMeaning _semvar LookupCoin =
20582058
let lookupCoinDenotation :: ByteString -> ByteString -> Value -> Integer
@@ -2068,7 +2068,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
20682068
{-# INLINE unionValueDenotation #-}
20692069
in makeBuiltinMeaning
20702070
unionValueDenotation
2071-
(runCostingFunTwoArguments . unimplementedCostingFun)
2071+
(runCostingFunTwoArguments . paramUnionValue)
20722072

20732073
toBuiltinMeaning _semvar ValueContains =
20742074
let valueContainsDenotation :: Value -> Value -> BuiltinResult Bool

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ data BuiltinCostModelBase f =
193193
, paramLengthOfArray :: f ModelOneArgument
194194
, paramListToArray :: f ModelOneArgument
195195
, paramIndexArray :: f ModelTwoArguments
196+
-- Builtin values
197+
, paramInsertCoin :: f ModelFourArguments
198+
, paramUnionValue :: f ModelTwoArguments
196199
}
197200
deriving stock (Generic)
198201
deriving anyclass (FunctorB, TraversableB, ConstraintsB)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ unitCostTwoArguments = CostingFun (ModelTwoArgumentsConstantCost 1) (ModelTwo
238238
unitCostThreeArguments :: CostingFun ModelThreeArguments
239239
unitCostThreeArguments = CostingFun (ModelThreeArgumentsConstantCost 1) (ModelThreeArgumentsConstantCost 0)
240240

241+
unitCostFourArguments :: CostingFun ModelFourArguments
242+
unitCostFourArguments = CostingFun (ModelFourArgumentsConstantCost 1) (ModelFourArgumentsConstantCost 0)
243+
241244
unitCostSixArguments :: CostingFun ModelSixArguments
242245
unitCostSixArguments = CostingFun (ModelSixArgumentsConstantCost 1) (ModelSixArgumentsConstantCost 0)
243246

@@ -355,6 +358,9 @@ unitCostBuiltinCostModel = BuiltinCostModelBase
355358
, paramLengthOfArray = unitCostOneArgument
356359
, paramListToArray = unitCostOneArgument
357360
, paramIndexArray = unitCostTwoArguments
361+
-- Builtin values
362+
, paramInsertCoin = unitCostFourArguments
363+
, paramUnionValue = unitCostTwoArguments
358364
}
359365

360366
unitCekParameters :: Typeable ann => MachineParameters CekMachineCosts DefaultFun (CekValue DefaultUni DefaultFun ann)

0 commit comments

Comments
 (0)