Skip to content

feat: native builtin data vs th builtin data benchmark#7562

Closed
colll78 wants to merge 3 commits intoIntersectMBO:masterfrom
colll78:native-data-vs-th-data
Closed

feat: native builtin data vs th builtin data benchmark#7562
colll78 wants to merge 3 commits intoIntersectMBO:masterfrom
colll78:native-data-vs-th-data

Conversation

@colll78
Copy link
Contributor

@colll78 colll78 commented Feb 4, 2026

Added a benchmark using very crude and barely optimized native builtin data functions to compare against the existing Vesting contract implemented with TH AsData stuff.

Added script context builder for constructing realistic script contexts which obey ledger invariants.

@Unisay Unisay requested review from Unisay and removed request for Unisay February 4, 2026 14:47
(\_ -> True)
BI.unitval
)
BI.unitval
Copy link
Contributor

Choose a reason for hiding this comment

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

I did an experiment and results demonstate that multiway-if compiled by the plugin is actually better than hand-rolled lazy ifthenelse pattern you employ here:
#7578

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I guess similar could be achieved with ifThenElseLazy predicate ~a ~b = BI.ifThenElse predicate a b right? And if the branches don't contain an error and the terms are small (ie. for && which has only True and False as branches, then we would want to use BI.ifThenElse because multiway if isn't smart enough to know whether it can be safely optimized to strict (and whether it should based on the terms in the branches).

I'll update the implementation to use multiway if, I'm sure it could be further optimized, but I just did a rough translated to BuiltinData and didn't put much effort into optimization and it is already optimized to around 35% of the budget of the TH Data version.

Unisay added a commit that referenced this pull request Feb 10, 2026
…udget

Compare 4 patterns for chaining boolean conditions in Plutus Tx:
1. Standard && (lazy, delay/force)
2. builtinAnd (lambda/unit, Philip DiSarro's pattern from PR #7562)
3. Multi-way if (negated guards)
4. Direct BI.ifThenElse chain (manual lambda/unit)

Each pattern tested with 3 scenarios: AllTrue, EarlyFail, LateFail.
Unisay added a commit that referenced this pull request Feb 10, 2026
Extract patterns 2 and 4 into Budget.BuiltinAndLib with the exact GHC
flags used in PR #7562's ValidatorOptimized.hs:
- All -fno-* optimisation flags
- conservative-optimisation plugin option
- INLINE pragmas (not INLINEABLE)

Results unchanged: builtinAnd still doesn't short-circuit (839,970 CPU
in all scenarios) because Bool arguments are evaluated eagerly before
the function body runs, regardless of INLINE/flags.
Unisay added a commit that referenced this pull request Feb 11, 2026
… experiment

Add two more patterns to the && vs builtinAnd comparison:

Pattern 5 (andBuiltinAndIfError): Philip's exact claimed usage -
  if (builtinAnd a $ builtinAnd b c) then ok else error
  Result: 919,970 CPU, no short-circuiting (all conditions evaluated)

Pattern 6 (andBuiltinIfNest): Nested builtinIf with conditions in lambda bodies
  (Philip's actual validator pattern from PR #7562)
  Result: 727,970 CPU AllTrue, with proper short-circuiting
@Unisay Unisay assigned Unisay and colll78 and unassigned Unisay Mar 3, 2026
Unisay added a commit that referenced this pull request Mar 4, 2026
Introduce a ScriptContext builder module in plutus-ledger-api testlib
that provides composable combinators for constructing ScriptContext
values in tests. This replaces hand-crafted script contexts with a
declarative builder pattern using lenses.

Key changes:
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Builder with combinators
  for minting, spending, certifying, rewarding, and proposing contexts
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Lenses with TH-generated
  lenses for all V3 ledger types
- Refactor LinearVesting benchmark tests to use the new builder
- Fix AsData destructSum-manual test to use manual IsData instances
  instead of asData, regenerating golden files

Based on work from PR #7562.

Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>
Unisay added a commit that referenced this pull request Mar 5, 2026
Introduce a ScriptContext builder module in plutus-ledger-api testlib
that provides composable combinators for constructing ScriptContext
values in tests. This replaces hand-crafted script contexts with a
declarative builder pattern using lenses.

Key changes:
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Builder with combinators
  for minting, spending, certifying, rewarding, and proposing contexts
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Lenses with TH-generated
  lenses for all V3 ledger types
- Refactor LinearVesting benchmark tests to use the new builder
- Fix AsData destructSum-manual test to use manual IsData instances
  instead of asData, regenerating golden files

Based on work from PR #7562.

Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>
Unisay added a commit that referenced this pull request Mar 10, 2026
…7643)

* Add ScriptContext Builder testlib and refactor LinearVesting tests

Introduce a ScriptContext builder module in plutus-ledger-api testlib
that provides composable combinators for constructing ScriptContext
values in tests. This replaces hand-crafted script contexts with a
declarative builder pattern using lenses.

Key changes:
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Builder with combinators
  for minting, spending, certifying, rewarding, and proposing contexts
- Add PlutusLedgerApi.Test.ScriptContextBuilder.Lenses with TH-generated
  lenses for all V3 ledger types
- Refactor LinearVesting benchmark tests to use the new builder
- Fix AsData destructSum-manual test to use manual IsData instances
  instead of asData, regenerating golden files

Based on work from PR #7562.

Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>

* Replace redundant helpers with existing API functions

- Replace local isPubKeyAddress/isScriptAddress with
  toPubKeyHash/toScriptHash from PlutusLedgerApi.V1.Address
- Replace negateValue with PlutusTx.negate (Value has AdditiveGroup)
- Export and relocate currencySymbolFromHex and singleCurrencySymbol
  to a dedicated Helpers section at the bottom of Builder.hs

Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>

* Regenerate golden files for LinearVesting and CallTrace tests

---------

Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>
@Unisay
Copy link
Contributor

Unisay commented Mar 10, 2026

Optimized linear vesting validator #7658

@Unisay Unisay closed this Mar 10, 2026
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