Why is this type-bound procedure causing a slow-down?
15:21 30 Jan 2026

I am working on some simulation code in Fortran. Part of that are potential cells that induce velocity.

Here is the definition for the cell type:

    type CELL
        doubleprecision :: xmin,ymin,zmin,xmax,ymax,zmax
        doubleprecision :: G

        contains

        procedure :: get_centroid => cell_get_centroid
        procedure :: get_induced_velocity => cell_get_induced_velocity
    endtype

Here are the definitions for the two type-bound procedures:

function cell_get_centroid(cell) result(centroid)
    class(CELL), intent(in) :: cell

    doubleprecision :: centroid(3)

    centroid(1) = 0.5d0 * (cell%xmin + cell%xmax)
    centroid(2) = 0.5d0 * (cell%ymin + cell%ymax)
    centroid(3) = 0.5d0 * (cell%zmin + cell%zmax)
    
end function cell_get_centroid
    
function cell_get_induced_velocity(cell, P) result(v_induced)
    class(CELL), intent(in) :: cell
    doubleprecision, intent(in) :: P(3)

    doubleprecision, dimension(3) :: v_induced

    doubleprecision :: dx, dy, dz, centroid(3)
    
    ! Get geometric properties
    !centroid = cell%get_centroid()
    centroid = cell_get_centroid(cell)
    dx = (cell%xmax-cell%xmin)
    dy = (cell%ymax-cell%ymin)
    dz = (cell%zmax-cell%zmin)
                
    call get_velocity_induced(centroid(1),centroid(2),centroid(3),dx,dy,dz,P(1),P(2),P(3),cell%G,v_induced(1),v_induced(2),v_induced(3))

end function field_cell_get_induced_velocity

I'm getting a very strange result. With the code written as is, my performance is good (cell_get_induced_velocity gets called a lot of times). But, if I uncomment the line centroid = cell%get_centroid() (which is the proper way, I would think) and commend out the line after it, my code slows down significantly (total computations take about 6X longer). Why is this? Shouldn't it not matter whether I'm passing cell as the object the procedure is bound to or as an argument?

performance oop optimization fortran