Monomorphization

Uses your system voice; voice and rate in settings.

Snapshot

≈ 2 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In programming languages, monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation. It is considered beneficial to undergo the mentioned transformation because it results in the output intermediate representation (IR) having specific types, which allows for more effective optimization. Additionally, many IRs are intended to be low-level and do not accommodate polymorphism. The resulting code is generally faster than dynamic dispatch, but may require more compilation time and storage space due to duplicating the function body.

In programming languages, monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation. It is considered beneficial to undergo the mentioned transformation because it results in the output intermediate representation (IR) having specific types, which allows for more effective optimization. Additionally, many IRs are intended to be low-level and do not accommodate polymorphism. The resulting code is generally faster than dynamic dispatch, but may require more compilation time and storage space due to duplicating the function body. ==Example== This is an example of a use of a generic identity function in Rust fn id (x: T) -> T { return x; } fn main() { let int = id(10); let string = id("some text"); println!("{int}, {string}"); } After monomorphization, this would become equivalent to 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!("{int}, {string}"); } ==See also== Parametric polymorphism Type erasure Template (C++)

Text derived from https://en.wikipedia.org/w/index.php?oldid=1375966464 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001375966464 · captured 1789957220.0 · version history

Cite this page as: encyclopegia.org, “Monomorphization”, snapshot enwiki-001375966464. https://encyclopegia.org/a/monomorphization