I don't know whether this is possible to achieve, but simple PURE functions
should sometimes be inlined. Please consider:

$> cat inline.f90
MODULE foomod
TYPE foo
  PRIVATE          ! change to PUBLIC to run the "inlined" loop
  INTEGER :: value
END TYPE

INTERFACE OPERATOR(==)
  MODULE PROCEDURE foo_equal
END INTERFACE

CONTAINS
  PURE FUNCTION foo_new(value) RESULT (this)
    INTEGER, INTENT(in)          :: value
    TYPE(foo) :: this
    this = foo(value)
  END FUNCTION

  PURE FUNCTION foo_equal(this, other)
    TYPE(foo), INTENT(in) :: this, other
    LOGICAL  :: foo_equal

    foo_equal = (this%value == other%value)
  END FUNCTION
END MODULE

USE foomod
INTEGER :: i, j

TYPE(foo) :: x(50000)
DO i = 1, SIZE(x)
  x(i) = foo_new(i)
END DO

DO i = 1, SIZE(x)
  DO j = i + 1, SIZE(x)
    IF (x(i) == x(j)) EXIT    ! call foo_equal
  END DO
END DO

!DO i = 1, SIZE(x)
!  DO j = i + 1, SIZE(x)
!    IF (x(i)%value == x(j)%value) EXIT  ! inlined
!  END DO
!END DO
END


Using comparison function foo_equal
$> gfortran-svn -O3 inline.f90 && time ./a.out
real    0m6.818s
user    0m6.772s
sys     0m0.036s

Inlined access:
$> gfortran-svn -O3 inline.f90 && time ./a.out

real    0m1.242s
user    0m1.236s
sys     0m0.008s


The example above was taken from a real-world application that (currently)
spends about 10% of its 3 hour runtime in accessor/comparison functions like
the one shown. In general, I'd prefer the OO'ish approach over PUBLIC access to
the components.


-- 
           Summary: inline (pure) accessor functions
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dfranke at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32817

Reply via email to