Skip to content

Iterating a tuple taints :terminates_locally #57097

Open
@LilithHafner

Description

@LilithHafner

A for loop over a tuple of known length does not infer as "terminates" unless it is annotated with @assume_effects :terminates_locally. This seems like a reasonable thing for effect analysis to figure out.

julia> function f(x...)
           a = 0
           for i in x
               a += i
           end
           a
       end
f (generic function with 1 method)

julia> Base.infer_effects(f, NTuple{2, Int})
(+c,+e,!n,!t,+s,+m,+u,+o,+r)

julia> Base.@assume_effects :terminates_locally function g(x...)
           a = 0
           for i in x
               a += i
           end
           a
       end
g (generic function with 1 method)

julia> Base.infer_effects(g, NTuple{2, Int})
(+c,+e,!n,+t,+s,+m,+u,+o,+r)

julia> @b f(1,2,3)
1.984 ns

julia> @b g(1,2,3)
1.134 ns

Iterating a Vector is also typically guaranteed to terminate locally. However it will be harder to determine when it is not (e.g. push! in the loop), and it may take many more iterations than with a tuple.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:effectseffect analysisfeatureIndicates new feature / enhancement requests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions