Skip to content

Generate a destructor function from AsData#7664

Merged
zliu41 merged 1 commit intomasterfrom
zliu41/asdata-destructor
Mar 16, 2026
Merged

Generate a destructor function from AsData#7664
zliu41 merged 1 commit intomasterfrom
zliu41/asdata-destructor

Conversation

@zliu41
Copy link
Copy Markdown
Member

@zliu41 zliu41 commented Mar 14, 2026

For sum types, it is better to use the destructor function than the pattern synonyms to match on them, because it uses caseInteger. It seems difficult if not impossible to compile pattern synonyms to caseInteger.

For example, the destructor for AsData.Budget.Types.TheseD:

    matchTheseD_a5Su ::
      forall a_a5Sj b_a5Sk r_a5Sy. (PlutusTx.UnsafeFromData a_a5Sj,
                                    PlutusTx.UnsafeFromData b_a5Sk) =>
                                   TheseD_a5Sf a_a5Sj b_a5Sk
                                   -> (a_a5Sj -> r_a5Sy)
                                      -> (b_a5Sk -> r_a5Sy)
                                         -> (a_a5Sj -> b_a5Sk -> r_a5Sy) -> r_a5Sy
    {-# INLINE matchTheseD_a5Su #-}
    matchTheseD_a5Su
      (TheseD_6989586621679032375_a5Sl d_a5Sv)
      k0_a5Sz
      k1_a5SA
      k2_a5SB
      = (BI.casePair (wrapUnsafeDataAsConstr d_a5Sv)
           $ (\ idx_a5Sw args_a5Sx
                -> BI.caseInteger
                     idx_a5Sw
                     [k0_a5Sz (PlutusTx.unsafeFromBuiltinData (BI.head args_a5Sx)),
                      k1_a5SA (PlutusTx.unsafeFromBuiltinData (BI.head args_a5Sx)),
                      BI.unsafeCaseList
                        (\ hd0_a5SC tl0_a5SD
                           -> k2_a5SB
                                (PlutusTx.unsafeFromBuiltinData hd0_a5SC)
                                (PlutusTx.unsafeFromBuiltinData (BI.head tl0_a5SD)))
                        args_a5Sx]))

The next step is to drop redundant unsafeCaseList calls, using a similar trick to this.

@zliu41 zliu41 requested review from a team, Unisay and ana-pantilie March 14, 2026 00:49
@github-actions
Copy link
Copy Markdown
Contributor

Execution Budget Golden Diff

b6d66c6 (master) vs 8c87501

output

plutus-tx-plugin/test/AsData/Budget/9.12/destructSum.golden.eval

Metric Old New Δ%
CPU 6_273_583 5_375_771 -14.31%
Memory 24_395 20_728 -15.03%
Flat Size 225 179 -20.44%

plutus-tx-plugin/test/AsData/Budget/9.6/destructSum.golden.eval

Metric Old New Δ%
CPU 6_273_583 5_375_771 -14.31%
Memory 24_395 20_728 -15.03%
Flat Size 225 179 -20.44%

This comment will get updated when changes are made.

Copy link
Copy Markdown
Contributor

@Unisay Unisay left a comment

Choose a reason for hiding this comment

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

Nice work! The CPS destructor approach is clean and the budget improvements are impressive (~14% CPU, ~24% AST size reduction on destructSum).

(y1 `PlutusTx.addInteger` y2)
(z1 `PlutusTx.addInteger` z2)
(w1 `PlutusTx.addInteger` w2)
matchTheseD
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.

Nit/suggestion: The golden test covers TheseD (3-constructor sum) well, and indirectly validates the TH machinery since incorrect code generation would show up in the PIR/UPLC goldens. If you're looking for places to expand coverage in the follow-up (dropping redundant unsafeCaseList), testing a product-type destructor or a zero-field constructor dispatch could be nice additions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The fact that everything compiles is an indication of correctness. There will be more tests once I implement step 2.

@zliu41 zliu41 merged commit f9c03fa into master Mar 16, 2026
10 checks passed
@zliu41 zliu41 deleted the zliu41/asdata-destructor branch March 16, 2026 14:17
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