# A playful competition: who can define a method that invalidates the most code?

**URL:** <https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543>\
**Category:** Tooling\
**Tags:** competition\
**Created:** [September 30, 2020, 3:21pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543 "2020-09-30T15:21:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 30, 2020, 3:21pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/1 "2020-09-30T15:21:54Z")

</div>

You may know that several of us have been hard at work stamping out [invalidations](https://julialang.org/blog/2020/08/invalidations/) as a way of reducing Julia’s latency. Kristoffer Carlsson and I thought it might be fun to have a contest to do the opposite: see who can come up with truly horrendous methods that obliterate almost everything 😛. There are no prizes other than the fame and glory that come with impressing your colleagues at your devilishness.

For those who haven’t read the blog post, very briefly the goal is to come up with a method definition that would force Julia to recompile a bunch of already-compiled code. Generally, this means creating a method that is more specific than the one that got compiled in. You can read about how you measure invalidations [here](https://timholy.github.io/SnoopCompile.jl/stable/snoopr/).

Here are the competition rules:

- the winners will be announced on Friday, Oct 2.
- you must use a released “nightly” binary ([Julia Downloads (nightly binaries)](https://julialang.org/downloads/nightlies/))
- when we verify your submission, we’ll start from a fresh Julia session that’s only loading `SnoopCompileCore`: no cheating by loading a lot of other packages and invalidating their methods, too.
- scoring will be based solely on `length(uinvalidated(invalidations))`
- you’re allowed to define new `struct`s, but your submission rests on the number of invalidations triggered by a single method definition.
- we will award separate prizes for “non-pirating” and “pirating” method definitions. (See definition of [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy).)

For context, a fresh Julia session contains about 48,000 MethodInstances. See how many of them you can obliterate!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 30, 2020, 3:27pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/2 "2020-09-30T15:27:03Z")

</div>

As a small example, this causes a meager ~100 invalidations:

```julia
julia> using SnoopCompileCore

julia> struct MyCustomString
           s::Vector{UInt8}
       end

julia> invalidations = @snoopr Base.convert(::Type{String}, m::MyCustomString) = String(m.s);

julia> using SnoopCompile

julia> length(uinvalidated(invalidations))
108

```

Looking at the output of

```julia
trees = invalidation_trees(invalidations)

```

and figuring out why a certain invalidation happen via e.g.

```julia
trees[end].mt_backedges[end][2] |> ascend

```

can be pretty interesting. Perhaps some of the invalidations in my example can even be fixed (maybe by you!).

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 30, 2020, 3:34pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/3 "2020-09-30T15:34:06Z")

</div>

You should add phase 2, where in the week after submissions are due, you get 10 points per competitor method that you can get to not cause invalidations by improving base.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 30, 2020, 3:44pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/4 "2020-09-30T15:44:06Z")

</div>

😆 I think we just **need** to add that. So, call it official: week 2 is “sabotage week”! Undermine your competitors by submitting a PR (doesn’t yet have to be accepted) to Julia or its standard libraries that prevents their submission from taking the trophy! We’ll offer recognition on Friday, Oct 9 for the person who _most reduced_ the number of invalidations of one of the official submissions.

This one is only for the non-pirating submissions (there’s nothing you can do about the pirating ones).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 30, 2020, 3:45pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/5 "2020-09-30T15:45:27Z")

</div>

How do we submit?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 30, 2020, 3:47pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/6 "2020-09-30T15:47:33Z")

</div>

Just post here? Does that seem fair? First to post a solution gets the credit…we’ll make a subjective decision about things that are too close to count as novel.

If that doesn’t seem fair, I’m open to alternatives…but I thought it would be kind of fun to marvel at the submissions as they roll in.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 30, 2020, 3:47pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/7 "2020-09-30T15:47:43Z")

</div>

This is allowed under those rules, right? 😉

```nohighlight
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0-DEV.1090 (2020-09-30)
 _/ |\ __'_|_|_|\__'_| | Commit 395e47f4da* (0 days old master)
|__/ |

julia> using SnoopCompile

julia> inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y));

julia> length(uinvalidated(inv))
16956

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 30, 2020, 3:48pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/8 "2020-09-30T15:48:45Z")

</div>

Wow, we already have an incredibly impressive contestant for the “piracy” category. Approximately one-third of all MethodInstances…

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 30, 2020, 4:17pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/10 "2020-09-30T16:17:24Z")

</div>

I think this competition is a great idea! A really good and fun way to get everyone active in the fight against invalidation!

Can anyone explain why it takes a number of method definitions in a row for the number of invalidations to “settle”? See:

```julia
julia> using SnoopCompile

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
17002

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
1694

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
1590

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
1458

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
1455

julia> begin 
           inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y))
           length(uinvalidated(inv))
       end
1455

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 30, 2020, 4:30pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/11 "2020-09-30T16:30:41Z")

</div>

Slight problem: the method for measuring invalidations only works on 1.6-DEV which isn’t released, so the rules of the contest seem to be impossible to meet.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 30, 2020, 4:31pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/12 "2020-09-30T16:31:08Z")

</div>

> [@simeonschaub](#):
>
> ```julia
> julia> inv = @snoopr Base.:+(x::Int, y::Int) = Base.inferencebarrier(Base.add_int(x, y));
> 
> ```

Turns out the `inferencebarrier` isn’t needed:

```julia
julia> using SnoopCompile

julia> inv = @snoopr Base.:+(x::Int, y::Int) = Base.add_int(x, y);

julia> length(uinvalidated(inv))
16987

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 30, 2020, 4:36pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/13 "2020-09-30T16:36:21Z")

</div>

Slightly better:

```julia
julia> using SnoopCompileCore

julia> invalidations = @snoopr Base.:+(x::T, y::T) where {T<:Base.BitInteger} = Base.add_int(x, y);

julia> using SnoopCompile

julia> length(uinvalidated(invalidations))
17117

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 30, 2020, 4:42pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/14 "2020-09-30T16:42:28Z")

</div>

Along a similar vein:

```julia
julia> using SnoopCompile

julia> invalidations = @snoopr Base.convert(::Type{T}, x::Number) where T<:Base.BitInteger = T(x);

julia> length(uinvalidated(invalidations))
18180

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 30, 2020, 4:43pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/15 "2020-09-30T16:43:34Z")

</div>

This one might deserve its own category for underhandedness (i.e. cheating):

```julia
julia> using SnoopCompileCore

julia> invalidations = @snoopr Base.length(::Set{Core.MethodInstance}) = Inf
Any[]

julia> using SnoopCompile

julia> length(uinvalidated(invalidations))
Inf

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 30, 2020, 5:03pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/16 "2020-09-30T17:03:44Z")

</div>

An entry for the non-piracy category.  
(Since the playing feild seems more open)

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0-DEV.1073 (2020-09-29)
 _/ |\ __'_|_|_|\__'_| | Commit 4f0145b7b0* (1 day old master)
|__/ |

julia> using SnoopCompile

julia> struct DoNotCare<:Real end

julia> inv = @snoopr Base.:(==)(x::DoNotCare, ::Any) = true;

julia> length(uinvalidated(inv))
282

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 30, 2020, 5:15pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/17 "2020-09-30T17:15:40Z")

</div>

Does this count in the non-piracy category?

```julia
julia> using SnoopCompileCore

julia> struct X end

julia> inv = @snoopr Base.convert(::Union{}, ::X) = nothing;

julia> using SnoopCompile

julia> length(uinvalidated(inv))
1177

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 30, 2020, 5:19pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/18 "2020-09-30T17:19:49Z")

</div>

A direct rip-off of @mbauman for nonpiracy:

```julia
julia> using SnoopCompile

julia> struct X end

julia> inv = @snoopr Base.convert(::Any, ::X) = nothing;

julia> length(uinvalidated(inv))
2203

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 30, 2020, 5:25pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/19 "2020-09-30T17:25:53Z")

</div>

The only way to improve on this is to make a type that can store hyper-real numbers efficiently so you can get a bigger infinity (assuming Inf is aleph null)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 30, 2020, 5:52pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/20 "2020-09-30T17:52:00Z")

</div>

> [@StefanKarpinski](#):
>
> Slight problem: the method for measuring invalidations only works on 1.6-DEV which isn’t released, so the rules of the contest seem to be impossible to meet.

Aren’t the rules to use the nightly release or am I missing something?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 30, 2020, 6:05pm UTC](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/21 "2020-09-30T18:05:23Z")

</div>

Ah yes, I misread that as using a released version, not a nightly release!

[Next page](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543.md?page=2)
