Skip to content

Monomorphization? #52722

Open
Open
@modulovalue

Description

@modulovalue

Consider, for example, any type-polymorphic construct (e.g. a generic function, method or class).

Is there a way to guarantee that any such construct is monomorphized (i.e. duplicated during compilation and optimized with the assumption that it is not polymorphic) when the type arguments are statically known?

In other words, I am looking for a way to selectively turn the "dial" from "prefer a smaller binary" to "prefer faster execution" for generics. This example might be more illustrative:

class Foo<U, V, W> {
  // ...
}

W bar<U, V, W>(U u, V v, ...) {
  // ...
}

@pragma("vm:always_monomorphize")
typedef IntFoo = Foo<int, int, int>();

@pragma("vm:always_monomorphize")
typedef IntBar = bar<int, int, int>();

void main() {
  // "IntFoo" and "IntBar" are equivalent to a "Foo" and "bar", respectively, with respect to runtime performance, where all type parameters have been manually instantiated to be int, inlined, and removed.
  final foo = IntFoo(...);
  final bar = IntBar(...);
}

I have tried searching for monomorphisation/monomorphization in the language and sdk repos, but I couldn't find much.

A google search for "dart monomorphization" did not bring up anything useful. Dart generics are reified, but, to me, that section doesn't appear to say anything about runtime performance. Rust seems to monomorphize everything by default, Java appears not to monomorphize anything because types are erased at runtime.

How does Dart behave with respect to monomorphization?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions