Are there additional subroutine calls with polymorphic derived types when the type is known at compile time?

I have two derived types (child1 and child2) that extend from the same abstract type ( type, abstract :: parent). An abstract type has a deferred related procedure.

I want to call a subroutine that does some things (critical to performance) depending on the type of child passed as input. I can imagine two options:

  • The routine accepts the input class(parent), intent(inout) :: type_in. Implementations for descendants are performed within the construct select type (type_in).
  • I am writing two routines: one s type(child1), intent(inout) :: type_inand one s type(child2), intent(inout) :: type_inand providing an explicit interface for overloading the name of the subroutine.

The first option allows you to implement implementations in which the parent extension is unknown at compile time, but this is not necessary in my case. It also saves some lines of code because only part of it is different for children.

My question is: is there any additional overhead in the first option, because I implemented the input as polymorphic data when the type is known at compile time?

+4
source share
1 answer

Yes, there is the added cost of a virtual call. The virtual method table (as it is called in other languages) is used and looks for the correct procedure to call. The cost is likely to be similar to the cost of a virtual function call in C ++, see fooobar.com/questions/60035 / ...

, . , . GCC -fdevirtualize -fdevirtualize-speculatively ( -O2, -O3, -Os), . , .

+4

Source: https://habr.com/ru/post/1610011/


All Articles