https://mfirhas.com/rss.xml

Generics in Java vs Go vs Rust

01 Sep 2022

Comparison of generics feature in Java, Go, and Rust.

Java

  • implements Type Erasure: deleting all types at runtime dan casted into object/bounded class/interface,
  • compile-time type checking,
  • run-time dynamic type(depends on boundaries),
  • no monomorphization,
  • overhead: run-time type casting

Go

  • implements Stenciling: checks compatibility with GCShape(group of similar types), and adds metadata(dictionaries) for each shape(associated types).
  • compile-time GCShape checking,
  • run-time metadata(dictionaries) checking for function invocation
  • partial monomorphization (monomorphized for same GCShape(group of types), not particular type)
  • overhead: run-time metadata checking for generic pointer types dan their methods invocations, because all generic pointer types share same gcshape.

Rust

  • implements Monomorphization(full): generates all form of instructions according to their types and optimize each of them.
  • compile-time type checking
  • compile-time type generation and optimization
  • fully monomorphized
  • overhead: zero-cost at runtime, only impact on slow compile-time for generation and optimization.

CMIIW