Skip to content

Commit 55cba54

Browse files
committed
Add insertCoin and unionValue costing skeleton
1 parent 149c182 commit 55cba54

File tree

9 files changed

+115
-2
lines changed

9 files changed

+115
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,7 @@ builtinMemoryModels = BuiltinCostModelBase
181181
, paramValueContains = Id $ ModelTwoArgumentsConstantCost 32
182182
, paramValueData = Id $ ModelOneArgumentConstantCost 32
183183
, paramUnValueData = Id $ ModelOneArgumentConstantCost 32
184+
, paramInsertCoin = Id $ ModelFourArgumentsConstantCost 1
185+
, paramUnionValue = Id $ ModelTwoArgumentsConstantCost 1
184186
}
185187
where identityFunction = OneVariableLinearFunction 0 1

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ builtinCostModelNames = BuiltinCostModelBase
136136
, paramValueContains = "valueContainsModel"
137137
, paramValueData = "valueDataModel"
138138
, paramUnValueData = "unValueDataModel"
139+
, paramInsertCoin = "insertCoinModel"
140+
, paramUnionValue = "unionValueModel"
139141
}
140142

141143

@@ -289,6 +291,8 @@ createBuiltinCostModel bmfile rfile = do
289291
paramValueContains <- getParams readCF2 paramValueContains
290292
paramValueData <- getParams readCF1 paramValueData
291293
paramUnValueData <- getParams readCF1 paramUnValueData
294+
paramInsertCoin <- getParams readCF4 paramInsertCoin
295+
paramUnionValue <- getParams readCF2 paramUnionValue
292296

293297
pure $ BuiltinCostModelBase {..}
294298

@@ -452,6 +456,13 @@ readCF3 e = do
452456
"exp_mod_cost" -> ModelThreeArgumentsExpModCost <$> readExpModCostingFunction "y_mem" "z_mem" e
453457
_ -> error $ "Unknown three-variable model type: " ++ ty
454458

459+
readCF4 :: MonadR m => SomeSEXP (Region m) -> m ModelFourArguments
460+
readCF4 e = do
461+
ty <- getType e
462+
case ty of
463+
"constant_cost" -> ModelFourArgumentsConstantCost <$> getConstant e
464+
_ -> error $ "Unknown four-variable model type: " ++ ty
465+
455466
readCF6 :: MonadR m => SomeSEXP (Region m) -> m ModelSixArguments
456467
readCF6 e = do
457468
ty <- getType e

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,5 +1254,25 @@
12541254
"arguments": 1,
12551255
"type": "constant_cost"
12561256
}
1257+
},
1258+
"insertCoin": {
1259+
"cpu": {
1260+
"arguments": 0,
1261+
"type": "constant_cost"
1262+
},
1263+
"memory": {
1264+
"arguments": 0,
1265+
"type": "constant_cost"
1266+
}
1267+
},
1268+
"unionValue": {
1269+
"cpu": {
1270+
"arguments": 0,
1271+
"type": "constant_cost"
1272+
},
1273+
"memory": {
1274+
"arguments": 0,
1275+
"type": "constant_cost"
1276+
}
12571277
}
12581278
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,5 +1254,25 @@
12541254
"arguments": 1,
12551255
"type": "constant_cost"
12561256
}
1257+
},
1258+
"insertCoin": {
1259+
"cpu": {
1260+
"arguments": 1000,
1261+
"type": "constant_cost"
1262+
},
1263+
"memory": {
1264+
"arguments": 10,
1265+
"type": "constant_cost"
1266+
}
1267+
},
1268+
"unionValue": {
1269+
"cpu": {
1270+
"arguments": 1000,
1271+
"type": "constant_cost"
1272+
},
1273+
"memory": {
1274+
"arguments": 10,
1275+
"type": "constant_cost"
1276+
}
12571277
}
12581278
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,5 +1272,25 @@
12721272
"arguments": 1,
12731273
"type": "constant_cost"
12741274
}
1275+
},
1276+
"insertCoin": {
1277+
"cpu": {
1278+
"arguments": 1000,
1279+
"type": "constant_cost"
1280+
},
1281+
"memory": {
1282+
"arguments": 10,
1283+
"type": "constant_cost"
1284+
}
1285+
},
1286+
"unionValue": {
1287+
"cpu": {
1288+
"arguments": 1000,
1289+
"type": "constant_cost"
1290+
},
1291+
"memory": {
1292+
"arguments": 10,
1293+
"type": "constant_cost"
1294+
}
12751295
}
12761296
}

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

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

20592059
toBuiltinMeaning _semvar LookupCoin =
20602060
let lookupCoinDenotation :: ByteString -> ByteString -> ValueLogOuterSizeAddLogMaxInnerSize -> Integer
@@ -2070,7 +2070,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
20702070
{-# INLINE unionValueDenotation #-}
20712071
in makeBuiltinMeaning
20722072
unionValueDenotation
2073-
(runCostingFunTwoArguments . unimplementedCostingFun)
2073+
(runCostingFunTwoArguments . paramUnionValue)
20742074

20752075
toBuiltinMeaning _semvar ValueContains =
20762076
let valueContainsDenotation :: ValueLogOuterSizeAddLogMaxInnerSize -> ValueTotalSize -> BuiltinResult Bool

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ data BuiltinCostModelBase f =
198198
, paramValueContains :: f ModelTwoArguments
199199
, paramValueData :: f ModelOneArgument
200200
, paramUnValueData :: f ModelOneArgument
201+
, paramInsertCoin :: f ModelFourArguments
202+
, paramUnionValue :: f ModelTwoArguments
201203
}
202204
deriving stock (Generic)
203205
deriving anyclass (FunctorB, TraversableB, ConstraintsB)

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

Lines changed: 5 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

@@ -360,6 +363,8 @@ unitCostBuiltinCostModel = BuiltinCostModelBase
360363
, paramValueContains = unitCostTwoArguments
361364
, paramValueData = unitCostOneArgument
362365
, paramUnValueData = unitCostOneArgument
366+
, paramInsertCoin = unitCostFourArguments
367+
, paramUnionValue = unitCostTwoArguments
363368
}
364369

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

0 commit comments

Comments
 (0)