Skip to content

Cost model for policies (CIP-0168) - #7921

Merged
Unisay merged 2 commits into
masterfrom
yura/issue-2309-policies-outersize
Aug 28, 2026
Merged

Cost model for policies (CIP-0168)#7921
Unisay merged 2 commits into
masterfrom
yura/issue-2309-policies-outersize

Conversation

@Unisay

@Unisay Unisay commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Benchmarks the builtin over 111 values and adds the four resulting parameters to the cost model record, the five JSON variants and the ledger API parameter lists. The policies builtin itself has been in batch7 since 1.67.0.0, so this PR is independent of the rest of the CIP-0168 work. Supersedes #7913, which keyed the cost on the total number of (policy, token) pairs.

The work of policies is linear in the number of policies alone, so a new ValueOuterSize wrapper threads the outer map size through the denotation, the way lookupCoin uses ValueMaxDepth. Map.size reads a field of the root node, so the measure stays O(1) and does not force the inner maps. The benchmark backs the measure empirically: values with fixed policy counts (1000 and 5000) and token counts from 1 to 40 differ by under 5%, with no correlation with the token count. The wrapper is reusable for costing keepPolicies and dropPolicies.

The model is a least-squares linear fit. The number of policies is sampled log-uniformly up to 40000, so every decade gets similar coverage, and the cap sits above the largest value a script can build within the CPU budget (roughly 14000 insertCoin applications). Memory is charged at three words per cons cell, because the result shares its bytestrings with the Value and only the list spine is new.

Implements IntersectMBO/plutus-private#2309.

@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.6.3

🚀 View preview at
https://IntersectMBO.github.io/plutus/pr-preview/cost-models/pr-7921/

Built to branch gh-pages at 2026-08-26 10:07 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@Unisay Unisay self-assigned this Aug 24, 2026
@Unisay Unisay changed the title Cost model for policies keyed on the number of policies (CIP-0168) Cost model for policies (CIP-0168) Aug 25, 2026
Benchmarks the builtin over 111 values and adds the four resulting parameters to
the cost model record, the five JSON variants and the ledger API parameter
lists.

The work is linear in the number of policies alone, so a new `ValueOuterSize`
wrapper threads the outer map size through the denotation, the way `lookupCoin`
uses `ValueMaxDepth`. `Map.size` reads a field of the root node, so the measure
stays O(1) and does not force the inner maps. Values with a fixed policy count
(1000 and 5000) and token counts from 1 to 40 differ by under 5%, with no
correlation with the token count, which is what a measure keyed on the outer map
predicts.

The number of policies is sampled log-uniformly up to 40000, so every decade
gets similar coverage, and the cap sits above the largest value a script can
build within the CPU budget (roughly 14000 `insertCoin` applications). Memory is
charged at three words per cons cell, because the result shares its bytestrings
with the `Value` and only the list spine is new.

Closes IntersectMBO/plutus-private#2309.
@Unisay
Unisay force-pushed the yura/issue-2309-policies-outersize branch from 71b2fbf to 995c2fb Compare August 25, 2026 14:06
@Unisay
Unisay marked this pull request as ready for review August 25, 2026 14:30
@Unisay

Unisay commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author
image

@Unisay
Unisay requested a review from kwxm August 26, 2026 09:29
Both generators dispatch on `TypeRep` and call `error` on a type they do not
know, so a new size wrapper has to be listed alongside `ValueTotalSize` and
`ValueMaxDepth` in each: `genConstant` for the evaluation property tests and
`smallConstant` for the cost model safety tests.
@Unisay
Unisay force-pushed the yura/issue-2309-policies-outersize branch from 6256fdb to c4556e3 Compare August 26, 2026 10:06

@kwxm kwxm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks straightforward: I don't see anything to complain about at all.

@Unisay
Unisay merged commit ca8463f into master Aug 28, 2026
9 checks passed
@Unisay
Unisay deleted the yura/issue-2309-policies-outersize branch August 28, 2026 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants