📑 Table of Contents

In programming languages, monomorphization is a compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation.[1] 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.[2][3][4][5][6][7]

Example

edit

This is an example of a use of a generic identity function in Rust

fn id<T>(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

edit

References

edit
  1. ^ Lutze, Matthew; Schuster, Philipp; Brachthäuser, Jonathan Immanuel (2025). "The Simple Essence of Monomorphization". Proceedings of the ACM on Programming Languages. 9 (OOPSLA1). Association for Computing Machinery. doi:10.1145/3720472.
  2. ^ Hume, Tristan. "Models of Generics and Metaprogramming: Go, Rust, Swift, D and More". Retrieved 27 May 2021.
  3. ^ Tanaka, Akira; Affeldt, Reynald; Garrigue, Jacques (2018). "Safe Low-level Code Generation in Coq Using Monomorphization and Monadification". Journal of Information Processing. 26: 54–72. doi:10.2197/ipsjjip.26.54.
  4. ^ Bonicho, Richard; Déharbe, David; Tavares, Claudia. "Extending Smt-Lib v2 with λ-Terms and Polymorphism" (PDF). Proceedings of the 12th International Workshop on Satisfiability Modulo Theories, (SMT 2014). Archived (PDF) from the original on 2023-10-03.
  5. ^ Cai, Yufei; Giarrusso, Paolo G.; Ostermann, Klaus (2016-01-11). "System f-omega with equirecursive types for datatype-generic programming". Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. POPL '16. St. Petersburg, FL, USA: Association for Computing Machinery. pp. 30–43. doi:10.1145/2837614.2837660. ISBN 978-1-4503-3549-2. S2CID 17566568.
  6. ^ Klabnik, Steve; Nichols, Carol (2019-08-06). The Rust Programming Language (Covers Rust 2018). No Starch Press. ISBN 978-1-7185-0044-0.
  7. ^ Felty, Amy P.; Middeldorp, Aart (2015-07-30). Automated Deduction - CADE-25: 25th International Conference on Automated Deduction, Berlin, Germany, August 1-7, 2015, Proceedings. Springer. ISBN 978-3-319-21401-6.

📚 Artikel Terkait di Wikipedia

Type erasure

definition. Template (C++) Problems with type erasure (in Generics in Java) Monomorphization Type polymorphism Langer, Angelika. "What is reification?". Crary,

Rust (programming language)

the actual type. By default, traits use static dispatch: the compiler monomorphizes the function for each concrete type instance, yielding performance equivalent

Template (C++)

to do operations on types. Template metaprogramming Metaprogramming Monomorphization Generic programming Header-only Substitution failure is not an error

Rust syntax

copy of the code is generated for each instantiation. This is called monomorphization and contrasts with the type erasure scheme typically used in Java and

Polymorphism (programming language theory)

when the shared object is built. While languages like C++ and Rust use monomorphized templates, the Swift programming language makes extensive use of dynamic

Code bloat

binary size. Programming languages like C++ use a "stenciling" or monomorphization approach for templates, where the compiler generates a separate copy

Flix (programming language)

compilation, eliminates polymorphism via monomorphization, and uses tree shaking to remove unreachable code. Monomorphization avoids boxing of primitive values

Static dispatch

Cat::new(String::from("Simba")); talk(pet); } the Rust Compiler will monomorphize the program's code at compile time into: // [...] // struct Cat, Cat's