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

iszero type piracy causes a lot of invalidations #51

Closed
KristofferC opened this issue Sep 30, 2020 · 21 comments
Closed

iszero type piracy causes a lot of invalidations #51

KristofferC opened this issue Sep 30, 2020 · 21 comments

Comments

@KristofferC
Copy link

KristofferC commented Sep 30, 2020

The definition here:

iszero(x::Number) = x == 0

changes the definition of iszero for all numbers (type piracy) and causes a huge number of invalidations

 inserting iszero(x::Number) in ApproxFunBase at /home/kc/.julia/packages/ApproxFunBase/r0W9A/src/Fun.jl:506 invalidated:
   backedges: 1: superseding iszero(x) in Base at number.jl:40 with MethodInstance for iszero(::UInt32) (1 children)
              2: superseding iszero(x) in Base at number.jl:40 with MethodInstance for iszero(::UInt64) (2 children)
              3: superseding iszero(x) in Base at number.jl:40 with MethodInstance for iszero(::Int64) (6236 children) <---------------

You can see the effect of this by loading ApproxFunBase and notice that the whole julia session becomes sluggish while it is recompiling everything.

@dlfivefifty
Copy link
Member

Thanks! That’s a bizarre one how that got there

@KristofferC
Copy link
Author

@dlfivefifty
Copy link
Member

Thanks! It looks like I didn't import iszero when I added the function (so was meant to be ApproxFun.iszero), and later added import Base: iszero.

I'll fix it now.

@KristofferC
Copy link
Author

Never use import Base: foo :P

@dlfivefifty
Copy link
Member

I always use import Base: foo 🤣 That way when Base: foo becomes LinearAlgebra: foo its a one-line change.

@dlfivefifty
Copy link
Member

@KristofferC
Copy link
Author

I'm also following the official convention advocated by StdLib:

Most of the code in the stdlibs were written at a time when people didn't know better 🤣

@dlfivefifty
Copy link
Member

I think introspection tools are a better way to go, say a GitHub action that does whatever it is you did to find these types of errors. Expecting developer style (and more importantly refactoring 100k's of existing Julia code) to prevent this is unrealistic.

@KristofferC
Copy link
Author

Accidental method extension is a problem So I stand by my advice to not use a way of coding which makes that easy.

@dlfivefifty
Copy link
Member

You might be right, but it's probably easier to get Julia to spit out a warning if a package overloads functions with types that do not belong to that package than it is to re-write every single Julia package in that style...

(Yes I'm aware it's not so simple because of "sanctioned" overloading like OffsetArrays.jl, but I'm sure some macro could be introduced or something to stop the warning.)

@timholy
Copy link

timholy commented Sep 30, 2020

We're about to announce a competition on discourse...shhh, don't tell, this one is the current record-holder for the largest number of invalidations caused by a single method definition. A whopping 13% of all MethodInstances on Julia 1.6!

No one who has read this is allowed to submit it, that honor goes to @KristofferC.

@dlfivefifty
Copy link
Member

I had no idea back in 2014 that my hacked together package would go on to cause so much damage to the Julia ecosystem... (And I guess given energy usage, the real ecosystem as well!) 🤯

@timholy
Copy link

timholy commented Sep 30, 2020

No worries, we've all learned a lot in the last few months about this stuff.

@timholy
Copy link

timholy commented Sep 30, 2020

ImageCore had these: JuliaImages/ImageCore.jl#131. A mere 200ish invalidations, so I bow before your greatness, but no better from a piracy perspective.

@dlfivefifty
Copy link
Member

The worst part is I've spent literally weeks trying to debug why ApproxFun.jl loading was so slow via code-bisection.... was probably very close to finding this culprit

@timholy
Copy link

timholy commented Sep 30, 2020

How much did it change with this PR?

@dlfivefifty
Copy link
Member

Oh actually not that much, 15.1s v 15.3s...

@KristofferC
Copy link
Author

I think the biggest difference will be in sluggishness in the REPL and package manager etc after loading the package.

@KristofferC
Copy link
Author

Also, even with this fixed there is still a large number of invalidations going on (e.g. invenia/Intervals.jl#144).

@timholy
Copy link

timholy commented Oct 1, 2020

Yeah, in general when you fix an invalidation problem in PkgA, it's PkgB (which depends on PkgA) that will thank you. So if you're still thinking this package is really slow to load, start using @snoopr on the packages it depends on.

@dlfivefifty
Copy link
Member

I’m in the process of completely redesigning it via ContinuumArrays.jl which is much snappier already, so I won’t be looking into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants