Skip to content

Commit e050973

Browse files
committed
feat: add cost model infrastructure for Value builtins
Extends the cost modeling framework to support lookupCoin, valueContains, valueData, and unValueData builtins. Adds parameter definitions, arity specifications, and integrates with the cost model generation system. Establishes foundation for accurate costing of Value operations in Plutus Core execution.
1 parent e5c82a3 commit e050973

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,10 @@ builtinMemoryModels = BuiltinCostModelBase
176176
, paramLengthOfArray = Id $ ModelOneArgumentConstantCost 10
177177
, paramListToArray = Id $ ModelOneArgumentLinearInX $ OneVariableLinearFunction 7 1
178178
, paramIndexArray = Id $ ModelTwoArgumentsConstantCost 32
179+
-- Builtin values
180+
, paramLookupCoin = Id $ ModelThreeArgumentsConstantCost 1
181+
, paramValueContains = Id $ ModelTwoArgumentsConstantCost 1
182+
, paramValueData = Id $ ModelOneArgumentConstantCost 1
183+
, paramUnValueData = Id $ ModelOneArgumentConstantCost 1
179184
}
180185
where identityFunction = OneVariableLinearFunction 0 1

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ builtinCostModelNames = BuiltinCostModelBase
131131
, paramLengthOfArray = "lengthOfArrayModel"
132132
, paramListToArray = "listToArrayModel"
133133
, paramIndexArray = "indexArrayModel"
134+
-- Builtin values
135+
, paramLookupCoin = "lookupCoinModel"
136+
, paramValueContains = "valueContainsModel"
137+
, paramValueData = "valueDataModel"
138+
, paramUnValueData = "unValueDataModel"
134139
}
135140

136141

@@ -279,6 +284,11 @@ createBuiltinCostModel bmfile rfile = do
279284
paramLengthOfArray <- getParams readCF1 paramLengthOfArray
280285
paramListToArray <- getParams readCF1 paramListToArray
281286
paramIndexArray <- getParams readCF2 paramIndexArray
287+
-- Builtin values
288+
paramLookupCoin <- getParams readCF3 paramLookupCoin
289+
paramValueContains <- getParams readCF2 paramValueContains
290+
paramValueData <- getParams readCF1 paramValueData
291+
paramUnValueData <- getParams readCF1 paramUnValueData
282292

283293
pure $ BuiltinCostModelBase {..}
284294

plutus-core/cost-model/data/models.R

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ arity <- function(name) {
152152
"LengthOfArray" = 1,
153153
"ListToArray" = 1,
154154
"IndexArray" = 2,
155+
"LookupCoin" = 3,
156+
"ValueContains" = 2,
157+
"ValueData" = 1,
158+
"UnValueData" = 1,
155159
-1 ## Default for missing values
156160
)
157161
}
@@ -804,11 +808,28 @@ modelFun <- function(path) {
804808

805809
dropListModel <- linearInX ("DropList")
806810

807-
## Arrays
811+
## Arrays
808812
lengthOfArrayModel <- constantModel ("LengthOfArray")
809813
listToArrayModel <- linearInX ("ListToArray")
810814
indexArrayModel <- constantModel ("IndexArray")
811815

816+
## Values
817+
lookupCoinModel <- linearInZ ("LookupCoin")
818+
819+
## ValueContains is O(n₂ × log max(m₁, k₁)) where n₂ is the total size of the second Value
820+
## We model this as linear in the sum of sizes, which is conservative
821+
valueContainsModel <- {
822+
fname <- "ValueContains"
823+
filtered <- data %>%
824+
filter.and.check.nonempty(fname) %>%
825+
discard.upper.outliers()
826+
m <- lm(t ~ I(x_mem + y_mem), filtered)
827+
mk.result(m, "added_sizes")
828+
}
829+
830+
valueDataModel <- constantModel ("ValueData")
831+
unValueDataModel <- linearInX ("UnValueData")
832+
812833
##### Models to be returned to Haskell #####
813834

814835
models.for.adjustment <-
@@ -902,7 +923,11 @@ modelFun <- function(path) {
902923
dropListModel = dropListModel,
903924
lengthOfArrayModel = lengthOfArrayModel,
904925
listToArrayModel = listToArrayModel,
905-
indexArrayModel = indexArrayModel
926+
indexArrayModel = indexArrayModel,
927+
lookupCoinModel = lookupCoinModel,
928+
valueContainsModel = valueContainsModel,
929+
valueDataModel = valueDataModel,
930+
unValueDataModel = unValueDataModel
906931
)
907932

908933
## The integer division functions have a complex costing behaviour that requires some negative

plutus-core/plutus-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ executable cost-model-budgeting-bench
935935
Benchmarks.Strings
936936
Benchmarks.Tracing
937937
Benchmarks.Unit
938+
Benchmarks.Values
938939
Common
939940
CriterionExtensions
940941
Generators

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ data BuiltinCostModelBase f =
193193
, paramLengthOfArray :: f ModelOneArgument
194194
, paramListToArray :: f ModelOneArgument
195195
, paramIndexArray :: f ModelTwoArguments
196+
-- Builtin values
197+
, paramLookupCoin :: f ModelThreeArguments
198+
, paramValueContains :: f ModelTwoArguments
199+
, paramValueData :: f ModelOneArgument
200+
, paramUnValueData :: f ModelOneArgument
196201
}
197202
deriving stock (Generic)
198203
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
@@ -355,6 +355,11 @@ unitCostBuiltinCostModel = BuiltinCostModelBase
355355
, paramLengthOfArray = unitCostOneArgument
356356
, paramListToArray = unitCostOneArgument
357357
, paramIndexArray = unitCostTwoArguments
358+
-- Builtin values
359+
, paramLookupCoin = unitCostThreeArguments
360+
, paramValueContains = unitCostTwoArguments
361+
, paramValueData = unitCostOneArgument
362+
, paramUnValueData = unitCostOneArgument
358363
}
359364

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

0 commit comments

Comments
 (0)