Skip to content

Commit bfdb60d

Browse files
committed
Add R modeling and verification tests for lookupCoin and valueContains
Extends the cost modeling infrastructure to support the new functions: - Adds R modeling code to analyze benchmark data and fit cost functions - Includes test cases to verify agreement between R models and Haskell implementation - Supports both ModelTwoArguments and ModelThreeArguments cost function types - Enables automated validation of cost model accuracy This completes the cost modeling pipeline allowing benchmark data to be processed into production-ready cost model coefficients.
1 parent 3cc23c2 commit bfdb60d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ arity <- function(name) {
152152
"LengthOfArray" = 1,
153153
"ListToArray" = 1,
154154
"IndexArray" = 2,
155+
"LookupCoin" = 3,
156+
"ValueContains" = 2,
155157
-1 ## Default for missing values
156158
)
157159
}
@@ -804,11 +806,28 @@ modelFun <- function(path) {
804806

805807
dropListModel <- linearInX ("DropList")
806808

807-
## Arrays
809+
## Arrays
808810
lengthOfArrayModel <- constantModel ("LengthOfArray")
809811
listToArrayModel <- linearInX ("ListToArray")
810812
indexArrayModel <- constantModel ("IndexArray")
811813

814+
## Values
815+
## LookupCoin is O(log n + log m) where n is outer map size, m is inner map size
816+
## Since we can't model logarithmic functions directly, we use linear approximation
817+
## which should be conservative (overestimates cost)
818+
lookupCoinModel <- linearInZ ("LookupCoin")
819+
820+
## ValueContains is O(n₂ × log max(m₁, k₁)) where n₂ is the total size of the second Value
821+
## We model this as linear in the sum of sizes, which is conservative
822+
valueContainsModel <- {
823+
fname <- "ValueContains"
824+
filtered <- data %>%
825+
filter.and.check.nonempty(fname) %>%
826+
discard.upper.outliers()
827+
m <- lm(t ~ I(x_mem + y_mem), filtered)
828+
mk.result(m, "added_sizes")
829+
}
830+
812831
##### Models to be returned to Haskell #####
813832

814833
models.for.adjustment <-
@@ -902,7 +921,9 @@ modelFun <- function(path) {
902921
dropListModel = dropListModel,
903922
lengthOfArrayModel = lengthOfArrayModel,
904923
listToArrayModel = listToArrayModel,
905-
indexArrayModel = indexArrayModel
924+
indexArrayModel = indexArrayModel,
925+
lookupCoinModel = lookupCoinModel,
926+
valueContainsModel = valueContainsModel
906927
)
907928

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

plutus-core/cost-model/test/TestCostModels.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ main =
387387
, $(genTest 1 "listToArray")
388388
, $(genTest 2 "indexArray") Everywhere
389389

390+
-- Builtin Values
391+
, $(genTest 3 "lookupCoin")
392+
, $(genTest 2 "valueContains") Everywhere
393+
390394
-- Data
391395
, $(genTest 6 "chooseData")
392396
, $(genTest 2 "constrData") Everywhere

0 commit comments

Comments
 (0)