# Invalidation from extending \`Base.reduce\_first\`

**URL:** <https://discourse.julialang.org/t/invalidation-from-extending-base-reduce-first/103146>\
**Category:** Performance\
**Tags:** method-invalidation\
**Created:** [August 24, 2023, 8:40am UTC](https://discourse.julialang.org/t/invalidation-from-extending-base-reduce-first/103146 "2023-08-24T08:40:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 24, 2023, 8:40am UTC](https://discourse.julialang.org/t/invalidation-from-extending-base-reduce-first/103146/1 "2023-08-24T08:40:30Z")

</div>

`FillArrays` has recently introduced a specialization

```julia
Base.reduce_first(::typeof(+), x::Ones) = Fill(Base.reduce_first(+, getindex_value(x)), axes(x))

```

On v1.10.0-beta2, this seems to introduce invalidations (although not on v1.9.2)

```julia
 inserting reduce_first(::typeof(+), x::Ones) @ FillArrays ~/Dropbox/JuliaPackages/FillArrays.jl/src/fillalgebra.jl:357 invalidated:
   backedges: 1: superseding reduce_first(op, x) @ Base reduce.jl:403 with MethodInstance for Base.reduce_first(::typeof(+), ::Any) (6 children)

```

I’m unsure how to resolve this. Looking into the backedges, I find

```julia
julia> show(root; minchildren=0)
MethodInstance for Base.reduce_first(::typeof(+), ::Any) (6 children)
 MethodInstance for Base.reduce_first(::typeof(Base.add_sum), ::Any) (5 children)
  MethodInstance for (::Base.BottomRF{typeof(Base.add_sum)})(::Base._InitialValue, ::Any) (4 children)
   MethodInstance for (::Base.MappingRF{Base.var"#349#350", Base.BottomRF{typeof(Base.add_sum)}})(::Any, ::Any) (0 children)
   MethodInstance for Base.afoldl(::Base.BottomRF{typeof(Base.add_sum)}, ::Base._InitialValue, ::Vararg{Any}) (0 children)
   MethodInstance for Base._foldl_impl(::Base.BottomRF{typeof(Base.add_sum)}, ::Base._InitialValue, ::Tuple) (1 children)
    MethodInstance for Base._foldl_impl(::Base.BottomRF{typeof(Base.add_sum)}, ::Base._InitialValue, ::Tuple) (0 children)

```

From this, it appears that somewhere there’s a poorly inferred call involving `afoldl` and one involving `Base._foldl_impl`. Any suggestions on what to do next to resolve this?

---

<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:** [August 24, 2023, 9:09am UTC](https://discourse.julialang.org/t/invalidation-from-extending-base-reduce-first/103146/2 "2023-08-24T09:09:37Z")

</div>

It’s probably missing a forced-specialization, e.g., `foo(f, args...)` rather than `foo(f::F, args...) where F`. (function and type args are often despecialized)
