Pattern matching on tuples desugaring

Hi :slightly_smiling_face:

I’d like to understand better what’s happening when we pattern match on Tuples like this one, which is only here for the pattern matching:

(a, b) match {
  case (..., ...) => ...
  case (..., ...) => ...

Is the tuple allocated? Is there any compilation desugaring getting rid of it to avoid this allocation?
Where can I find information about that, please?

Jules :slightly_smiling_face:

1 Like

The tuple is almost certainly allocated in the output byte code. The question is whether or not that allocation is elided by the JVMs JITC

1 Like

On Scala 2, -opt:local works but here is a ticket illustrating a limitation.

I want to say I just read a Scala 3 ticket where they hoped for better code generation from pattern matching (but I don’t have a spare day to search for it again).

The overview page for -opt.

Difficult to read here, but the last line of the snippet mentions tuples under the box-unbox suboption.

➜  ~ scalac -opt:help
Enable optimizations: `-opt:local`, `-opt:inline:<pattern>`; `-opt:help` for details.
  unreachable-code             Eliminate unreachable code, exception handlers guarding no instructions, redundant metadata (debug information, line numbers).
  simplify-jumps               Simplify branching instructions, eliminate unnecessary ones.
  compact-locals               Eliminate empty slots in the sequence of local variables.
  copy-propagation             Eliminate redundant local variables and unused values (including closures). Enables unreachable-code.
  redundant-casts              Eliminate redundant casts using a type propagation analysis.
  box-unbox                    Eliminate box-unbox pairs within the same method (also tuples, xRefs, value class instances). Enables unreachable-code.