Skip to content

Remove method overloads that duplicate Base/LinearAlgebra generics - #443

Open
JamesWrigley wants to merge 1 commit into
JuliaArrays:masterfrom
JamesWrigley:extra-methods
Open

Remove method overloads that duplicate Base/LinearAlgebra generics#443
JamesWrigley wants to merge 1 commit into
JuliaArrays:masterfrom
JamesWrigley:extra-methods

Conversation

@JamesWrigley

Copy link
Copy Markdown

These all have generic definitions upstream that do the same thing. Using Base's unary - also fixes a wee bug by preserving the axes of the inputs such that -Fill(2, (1:3,)) == Fill(-2, (1:3,)). The old implementation used size() so the result would be -Fill(2, (3,)).

Discovered with help from Claude 🤖

@jishnub

jishnub commented Jul 24, 2026

Copy link
Copy Markdown
Member

Some of these are for performance and dead-code elimination, so it's worth checking that

@dlfivefifty

Copy link
Copy Markdown
Member

I will be surprised if the downstream tests pass

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.91%. Comparing base (7b96cc2) to head (23f248c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #443      +/-   ##
==========================================
- Coverage   99.91%   99.91%   -0.01%     
==========================================
  Files           9        9              
  Lines        1238     1235       -3     
==========================================
- Hits         1237     1234       -3     
  Misses          1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JamesWrigley

Copy link
Copy Markdown
Author

CI looks good. Do you know which methods are there for performance? isassigned() and count() are the ones that jump out at me.

Comment thread src/FillArrays.jl
@inline function Base.isassigned(F::AbstractFill, i::Integer...)
@boundscheck checkbounds(Bool, F, to_indices(F, i)...) || return false
return true
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default isassigned uses a slow try-catch block. Please restore these.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 23f248c.

Comment thread src/FillArrays.jl
fval = f(getindex_value(x))
return all((fval,))
end
any(x::AbstractFill) = any(identity, x)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any/all will iterate over all the entries. Please restore these

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow, don't these also iterate over all the entries and are the same definitions as in Base? https://github.com/JuliaLang/julia/blob/0fe418d406adbaea5d9b28a5a8410f9e2522d028/base/reducedim.jl#L991

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would they???? The point of fill arrays is you know all the entries:

function any(f::Function, x::AbstractFill)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I'm blind, didn't see the any(::Function, x) definitions above it 🤦

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 23f248c.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm on second thoughts aren't these ok to be deleted since both they and the Base versions will end up calling any(::Function, ::AbstractFill)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One beautiful thing about julia is you can easily see what is happening, using @which or the debugger. The answer: no, Base does not call any(identity, ::AbstractFill) but instead calls _any:

[1/2] any(a; dims) at /Users/solver/.julia/juliaup/julia-1.12.6+0.aarch64.apple.darwin14/Julia-1.12.app/Contents/Resources/julia/share/julia/base/reducedim.jl:988
>988  any(a::AbstractArray; dims=:)              = _any(a, dims)

This eventually iterates over a loop:

[1/4] _any(f, itr, #unused#) at /Users/solver/.julia/juliaup/julia-1.12.6+0.aarch64.apple.darwin14/Julia-1.12.app/Contents/Resources/julia/share/julia/base/anyall.jl:121
 120  @eval function _any(f, itr::$ItrT, ::Colon)
>121      $(ItrT === Tuple ? :(@_terminates_locally_meta) : :nothing)
 122      anymissing = false
 123      for x in itr

I appreciate your desire to remove code but please make sure you double check that that the default actually avoids iteration.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I missed that.

Comment thread src/FillArrays.jl
any(x::AbstractFill) = any(identity, x)
all(x::AbstractFill) = all(identity, x)

count(x::AbstractOnes{Bool}) = length(x)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 23f248c.

@dlfivefifty

Copy link
Copy Markdown
Member

Most of these overloads are very clearly needed to avoid looping over all entries.

These all have generic definitions upstream that do the same thing. Using Base's
unary `-` also fixes a wee bug by preserving the axes of the inputs such that
`-Fill(2, (1:3,)) == Fill(-2, (1:3,))`. The old implementation used `size()` so
the result would be `-Fill(2, (3,))`.
@JamesWrigley

Copy link
Copy Markdown
Author

I also restored the count(::AbstractZeros) method since that's an optimization too. So now the remaining deleted methods are:

  • vec() (definition in Base calls reshape() internally which ends up calling fillsimilar() too)
  • permutedims() (upstream definition does the same thing, and we didn't have a two-arg definition anyway)
  • Unary -, which fixes the multidimensional case and calls broadcastable internally.

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

Successfully merging this pull request may close these issues.

3 participants