Skip to content

Generate a destructor function from AsData#7664

Open
zliu41 wants to merge 1 commit intomasterfrom
zliu41/asdata-destructor
Open

Generate a destructor function from AsData#7664
zliu41 wants to merge 1 commit intomasterfrom
zliu41/asdata-destructor

Conversation

@zliu41
Copy link
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
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.

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.

1 participant