plutus icon indicating copy to clipboard operation
plutus copied to clipboard

refactor(cost-model): use linear costing for valueContains

Open Unisay opened this issue 3 weeks ago • 2 comments

Summary

  • Change valueContains costing from multiplied_sizes to const_above_diagonal with linear_in_x_and_y inner model
  • Add LinearInXAndY constructor to SimpleJSON.hs for JSON parsing
  • Update cost model visualization to support new model types

Motivation

The true complexity of valueContains is O(m*log(n/m+1)) where n is the container size and m is the contained size. Kenneth MacKenzie showed that this is bounded above by a linear function (m+n)/k for some constant k.

The new model better reflects this:

  • Above diagonal (y > x): constant cost (early exit, returns False immediately)
  • Below diagonal (x >= y): intercept + slope1*x + slope2*y

Changes

File Change
models.R Update valueContainsModel to use const_above_diagonal with linear_in_x_and_y
SimpleJSON.hs Add LinearInXAndY constructor for JSON parsing
utils.js Add evaluators for linear_in_x_and_y and const_above_diagonal
builtinCostModel{A,B,C}.json Regenerated with new model type

New Cost Model Structure

"valueContains": {
  "cpu": {
    "arguments": {
      "constant": 284518,
      "model": {
        "arguments": {
          "intercept": 1000,
          "slope1": 158792,
          "slope2": 24659
        },
        "type": "linear_in_x_and_y"
      }
    },
    "type": "const_above_diagonal"
  },
  "memory": {
    "arguments": 32,
    "type": "constant_cost"
  }
}

Test plan

  • [x] Build passes
  • [x] Conformance tests pass (will need budget expectation updates)
  • [x] Cost model visualization works with new model type

Closes https://github.com/IntersectMBO/plutus-private/issues/1984

Unisay avatar Dec 04 '25 13:12 Unisay

PR Preview Action v1.6.2 :---: |

:rocket: View preview at
https://IntersectMBO.github.io/plutus/pr-preview/cost-models/pr-7476/

|
Built to branch gh-pages at 2025-12-04 16:06 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

github-actions[bot] avatar Dec 04 '25 13:12 github-actions[bot]

This PR replaces https://github.com/IntersectMBO/plutus/pull/7476

Unisay avatar Dec 05 '25 10:12 Unisay