+ 3 overloads - What would that be?

Asked

Viewed 540 times

20

In some functions appears such a quantity of overloads, That means the higher that number, the slower it is?

Note: I used tag C++ and C# why I saw these overloads in these languages. Microsoft Visual Studio 2015 Usage.

2 answers

24


In C there is no overloads (had it ma original question). Only in C++ and C#.

This is the overload of methods or functions. Roughly it consists of having a function with the same name but with different type parameters. I won’t go into detail because you already have answer on the subject, there shows the internal functioning.

Although the resolution criterion of which method to use is different, C++ works essentially the same as C#, or Java, for example. Most modern statically typed languages have something like this.

This does not affect performance negatively at all. On the contrary, one of the most used alternatives (solve, probably with if or switch, what the method should actually do, since there are alternative execution paths) would affect the performance by delegating the decision to the execution time.

Another way would be to use different names for similar functions, as C does. What can affect readability (some people disagree), but not performance.

What the IDE shows is that you have alternatives of which signature to use (without the Overload cannot do this). In the specific case there are 4 different methods that can be applied there. If it has multiple parameters as new arguments In the call, it goes tapering and decreasing, until it will no longer show this tip because you already have a unique signature and there is no confusion with others. In the end you’ll always have to reduce ambiguity to zero.

Practical example with 2 overload.

  • Thanks for the reply, translating on google seems to be something quite different, but I got it! For example argument 0 of the str.find function can be either a char character or a string :D

  • No, this is incorrect.

  • Oush, is it not? It is not when a given function accepts more arguments and even different types of arguments?

  • They are completely different functions, not the same.

17

Why overloads have no cost of performance?

Complementing the response of Maniero, the compiler or virtual machine is able to solve the overloads statically, identifying only the methods not only by their name, but by their name and the types of the parameters. It is as if the methods metodo(String), metodo(int) and metodo(String, int) didn’t just call themselves metodo, and yes metodo_String, metodo_int and metodo_String_int, which makes each of them have a different identification. And because of that, there is no weight in performance relative to that.

And the overrides?

As to the overrides, the function is different, the called method depends on the instance class, which is information that will only be present at runtime.

A simple way to implement a override virtual methods (i.e., methods that can be overwritten) are implemented internally with a if or switch to look at the guy from this calling the proper concrete method, which would have some reasonable weight in terms of performance. It turns out that this is not the way the override is implemented.

In fact, the overrides are solved by means of function pointers and vtables. Each vtable is a table that each concrete class has function pointers for the methods implemented in that class. Therefore, when calling a method, what occurs internally is that a reference to the class is obtained from the reference to the class each instance has, and from that reference to the class (which contains the beginning of the vtable in a offset fixed memory), the function pointer for the method to be invoked is obtained, and the reference to the instance is passed as a parameter, along with the other parameters. And before anyone asks, yes, it means that the compiler or virtual machine adds a hidden parameter that becomes the reference this in each non-static method.

As for performance, there is a minimum cost associated yes to the invocation of non-static methods, which consists of an indirect operation and a sum of integers, and is done by de-referencing the pointer to the instance, reading an integer from the obtained memory location (which is the class address, which will always be the first available information in the memory area allocated to each object) and add to that address, the offset of the method within the vtable, then obtain the pointer for the corresponding method. This contrasts with the case of static methods, where the pointer to the method is already known beforehand. References to interface methods may require one or two more machine instructions to be resolved, since the location of the method in the vtable it’s gonna be a little more complicated.

On the other hand, people who design compilers and virtual machines try to optimize this cost of performance so that calls for virtual methods can be determined statically (for example, when the instance type is a concrete class for which there are no subclasses) it can be simplified by eliminating the need to search for the method in vtable at runtime when doing this at compile time, also eliminating the associated cost. In addition, other types of more complex optimizations with compilation just-in-time are possible to eliminate these costs in several cases.

  • Oops! There is a lot there that is not part of the answer but thank you!

Browser other questions tagged

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