-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
Problem
The pattern matching optimization (implemented to avoid redundant let bindings for Var scrutinees) uses SIR.LetFlags.Lazy to defer evaluation of match scrutinees. However, the SimpleSirToUplcLowering backend ignores lazy flags and always evaluates let bindings eagerly.
Impact
This causes a 10-byte increase in UPLC size (846 → 856 bytes) for the optimized minting policy test when using SimpleSirToUplcLowering backend.
Current Behavior
- Pattern matching optimization creates let bindings with
SIR.LetFlags.Lazywhen scrutinee is not a Var SimpleSirToUplcLoweringignores this flag and evaluates eagerly- Results in slightly larger UPLC bytecode (856 bytes instead of expected 846)
Expected Behavior with V3 Backend
SirToUplcV3Lowering correctly handles lazy flags and produces more optimal code:
- V3 backend: 1652 bytes (properly uses lazy evaluation)
- SimpleSirToUplcLowering: 856 bytes (ignores lazy flags)
Decision
Accept the small size increase for SimpleSirToUplcLowering as it's the legacy backend.
Files Affected
scalus-plugin/src/main/scala/scalus/PatternMatchingCompiler.scala- Creates lazy let bindingsscalus-core/shared/src/main/scala/scalus/sir/lowering/Lowering.scala- SimpleSirToUplcLowering ignores lazy flagsscalus-examples/shared/src/test/scala/scalus/examples/MintingPolicyExampleTest.scala- Test shows 10-byte size increase
Future Work
Consider one of:
- Make the pattern matching optimization backend-aware (don't use lazy flags with SimpleSirToUplcLowering)
- Migrate fully to V3 backend and deprecate SimpleSirToUplcLowering
- Update SimpleSirToUplcLowering to understand lazy flags (significant refactoring)
Related Code
Pattern matching optimization in PatternMatchingCompiler.scala:185-194:
val decisionsWithScrutinee = if needsScrutineeLet then
SIR.Let(
List(Binding(ctx.scrutineeName, parsedMatch.scrutineeTp, parsedMatch.scrutinee)),
decisions,
SIR.LetFlags.Lazy, // <-- SimpleSirToUplcLowering ignores this
AnnotationsDecl.fromSrcPos(ctx.topLevelPos)
)
else
decisionsReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels