opshin icon indicating copy to clipboard operation
opshin copied to clipboard

Optimizer: Expanding Union Types

Open nielstron opened this issue 6 months ago • 0 comments

Is your feature request related to a problem? Please describe. Currently functions have to decide between performance and generalizability. I.e. a function foo(Union[A, B]) that does internal isinstance checks to distinguish the two types has an inherently higher cost that writing manually foo_A(A) and foo_B(B). However this would lead to code duplication and more difficult mainenance.

Describe the solution you'd like For any function foo that accepts union type arguments, we automatically generate foo_A and foo_B as described above. All calls to foo where the type of the argument is known at compilation time are remapped to foo_A and foo_B. Inside foo_A and foo_B a simple truth analysis + dead code analysis can remove the redundant isinstance checks. This is an O3 optimization and can come with significant increase in the script size.

Describe alternatives you've considered None

Additional context Would i.e. allow more performant Union[int, Fraction] add and mul implementations.

Bug bounty: 750 ADA

nielstron avatar Aug 23 '24 10:08 nielstron