What is Monomorphization?

Asked

Viewed 100 times

4

I was reading that posting and I came across that term monomorphization

I’d like to know:

  • What is its meaning?
  • When this process occurs?
  • What performance gain/loss is obtained by this process?

1 answer

4


Sometimes we write polymorphic codes. One of the most commonly used polymorphisms is virtualization where the pointer will decide at runtime what to do in a specialized way, but the written code is generic, you say you need to do an operation that a base object is capable of, but objects derived from it also know how to do it and can do it in a more specific way. I won’t talk about it much, see another answer if not mastered the subject.

But there is another very interesting polymorphism that is the parametric. Some very powerful like C++, others less like Java or C# which are Generics. I suggest reading more in Parametric polymorphism and overload in Java and C++.

They can be used in various ways, among them exactly what is said above. You write a generic code, a template for the actual code and on execution the actual code specific to the situation will be executed. The difference in the decision whether to execute a base or derivative method is that this mechanism allows the choice to be made at compile time.

If this is done, monomorphism is performed. Note that some mechanisms, especially of Generics, do not do this whenever it has a type parameterization and the decision of what to use can be performed at runtime, so the code remains polymorphic in the executable.

Monomorphism is to take that polymorphic written code and turn it into a monomorphic code, so a code could have several forms and has a specialized form for each situation used.

For example, if the code of a class is polymorphic and it accepts anything that is comparable (it has ability to compare objects), so what you will use there just needs to call a comparison, the object needs to be able to do that. And let’s say you called what is polymorphic in three situations, with a int, with a String and with a Date. The compiler will create three methods equal to that written, each of these methods will use one of these types to make the comparison.

This is very interesting because it doesn’t have virtualization, doesn’t have to decide at runtime what to do, gets faster and can even allow certain optimizations.

But it’s not always good. Let’s say you used this polymorphic method in 200 different types. 200 different methods will be generated.

I’ll compare it to a code everyone understands easy:

for (int i = 0; i < 2; i++) print(i);

is the same as:

print(0)
print(1)

In fact a compiler that does optimizations will probably turn the first into the second and will be faster.

But what if the code is:

for (int i = 0; i < 200; i++) print(i);

pays to do the same?

And if so:

for (int i = 0; i < user_entry; i++) print(i);

understood that it has a variable that is only known at runtime, right? It can be optimized, even if the value is 0, 1, 2 or other low?

What is its meaning?

The act of transforming a polymorphic generic code into a monomorphic code.

When this process occurs?

In C++, during build always.

What performance gain/loss is obtained by this process?

That of not needing the indirect to access a method and possible optimizations because of this. Loses to stay with a large executable too.

Real example:

template <class T>
    T max(T a, T b) { return (a > b ? a : b); }

When you call it:

max(1, 2)

compiler will generate a method like this:

int max(int a, int b) { return (a > b ? a : b); }

And for each type that is consumed it will generate a method like this. That method of template, which is polymorphic by definition, will never exist, it is only a template for creating the real methods that are monomorphic.

Note that this type of code is old for C++ and would give error in objects that cannot make a comparison, today would normally use a concept to restrict what type can be used there and get more robust, but this is another matter.

Without monomorphism I’d have to do something like this:

void *max(void *a, void *b) { return (*a > *b ? *a : *b); }

And then I use:

*(max(&1, &2))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.