Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update methods to specialize on mod argument #37838

Merged
merged 3 commits into from
Oct 9, 2020
Merged

Conversation

omus
Copy link
Member

@omus omus commented Oct 1, 2020

While investigating invenia/Intervals.jl#144 I noticed this invalidation:

inserting -(a, b::Interval) in Intervals at /Users/omus/.julia/dev/Intervals/src/interval.jl:314 invalidated:
   mt_backedges: 1: signature Tuple{typeof(-), Module, Any} triggered MethodInstance for _in_range(::Module, ::AbstractRange{Module}) (1 children)
   49 mt_cache

An AbstractRange{Module} seemed strange so I did some digging and noticed m.method.module ∈ mod inside of methods. As far as I can tell nothing is actually creating a AbstractRange{Module} but, possibly due to @nospecialize, the compiler seems to be specializing in calls for all possible arguments. (Note: this is purely theory crafting on my part)

Restricting the argument to Array eliminates the invalidation. We could probably reduce the argument to Vector safely but that would require some changes to the docstring.

@omus omus added the compiler:latency Compiler latency label Oct 1, 2020
@DilumAluthge
Copy link
Member

It seems reasonable to me to further restrict it to Vector{Module}, with an appropriate update to the docstring.

@rfourquet
Copy link
Member

I don't think tricks to reduce invalidations should have such user-facing consequences. IMHO there is no good reason to limit to Vector here (but limiting to AbstractVector is fine). Wouldn't you get the same results if instead you convert mod::AbstractVector at the beginning of the function to a Vector, like mod = Vector(mod) ?

@timholy
Copy link
Member

timholy commented Oct 1, 2020

It's better to do that conversion in a "stub" method and then have the "real" method be picky about its inputs: https://docs.julialang.org/en/v1/manual/style-guide/#Handle-excess-argument-diversity-in-the-caller

"Infer & compile long methods only a few times, infer and compile short methods as many times as you want to."

@omus
Copy link
Member Author

omus commented Oct 1, 2020

Another way to eliminate the invalidation is to remove the @nospecialize from the mod argument. After reviewing the original PR I think the @nospecialize was added to the mod parameter by default and not for any performance reasons.

Additionally, supporting passing in Nothing and Tuple{Module} as the mod parameter seems to just be for internal use and should probably refactored out of the public facing interface. I'll leave that for a separate PR

@omus omus changed the title Update methods to take a Array{Module} Update methods to specialize on mod argument Oct 1, 2020
@omus omus merged commit 189ec8f into master Oct 9, 2020
@omus omus deleted the cv/in-module-invalidation branch October 9, 2020 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:latency Compiler latency
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants