You can edit almost every page by Creating an account and confirming your email.

Monomorphization

From EverybodyWiki Bios & Wiki



In programming languages, Monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation.[1] This transformation is desirable since then the output Intermediate representation will have concrete types and can be optimized better. Furthermore, most IR's are designed to be low-level and do not support polymorphism. Code generated this way is typically faster than Boxing types, but may compile slower and take more space due to duplicating the function body.[2]

Example

For example, this is an example of a use of a generic identity function

fn id<T>(x: T) -> T {
    return x;
}

fn main() {
    let int = id(10);
    let string = id("some text");
    println!("{0}, {1}", int, string);
}

After monomorphization, this would become

fn id_i32(x: i32) -> i32 {
    return x;
}

fn id_str(x: &str) -> &str {
    return x;
}

fn main() {
    let int = id_i32(10);
    let string = id_str("some text");
    println!("{0}, {1}", int, string);
}

See also

References

  1. "Generic Data Types - The Rust Programming Language". Retrieved 27 May 2021.
  2. Hume, Tristan. "Models of Generics and Metaprogramming: Go, Rust, Swift, D and More". Retrieved 27 May 2021.


This article "Monomorphization" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Monomorphization. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.

Page kept on Wikipedia This page exists already on Wikipedia.