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

abstractarray: fix append!(::AbstractVector, ...) interface #49754

Merged
merged 1 commit into from
May 12, 2023
Merged

Conversation

aviatesk
Copy link
Sponsor Member

#47154 mistakenly added @_safeindex macro on the _append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter) method, although @_safeindex is only valid for builtin vectors i.e. Vector.

This commit adds isa check so that @_safeindex is only applied to builtin vectors. The isa check should be removed at compile time, so it should not affect the runtime performance.

closes #49748

#47154 mistakenly added `@_safeindex` macro on the
`_append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)` method,
although `@_safeindex` is only valid for builtin vectors i.e. `Vector`.

This commit adds `isa` check so that `@_safeindex` is only applied to
builtin vectors. The `isa` check should be removed at compile time, so
it should not affect the runtime performance.

closes #49748
@@ -1180,18 +1182,21 @@ push!(a::AbstractVector, iter...) = append!(a, iter)

append!(a::AbstractVector, iter...) = foldl(append!, iter, init=a)

function _append!(a, ::Union{HasLength,HasShape}, iter)
function _append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I wonder if we should move these generic definitions into base/abstractarray.jl and define simple definition of append! for Vector-inputs here (same for prepend!). Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Special casing a concrete implementation should IMO always be preferrable over assuming more of a generic version. It's philosophically the same reason why @inbounds in a function accepting AbstractArray is bad - there's no guarantee that the concrete type isn't doing any funny business in indexing; the local information cannot be enough to ensure the use is safe.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

A potential drawback on the design is that there will be multiple potential method matches in type-unstable code for those functions, that may lead to worse inlining and optimization.

Well, we may want to accept that since the type unstable code will be slow anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think prioritizing correctness over potentially broken fallbacks is a better bet any day of the week.

@aviatesk
Copy link
Sponsor Member Author

@nanosoldier runbenchmarks("array" || "sparse", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@aviatesk
Copy link
Sponsor Member Author

Going to merge this. I will refactor the code as suggested above in a separate PR.

@aviatesk aviatesk merged commit 9002d16 into master May 12, 2023
@aviatesk aviatesk deleted the avi/49748 branch May 12, 2023 06:35
@Seelengrab
Copy link
Contributor

Since I looked at this code again for a different reason and noticed this, how did the @_safeindex come up in the first place? This branch should be completely dead, since the specialized append! just above doesn't use _append!, right?

https://github.com/JuliaLang/julia/pull/49754/files#diff-b3ce72015a28ab518a31d9914189e2a17e1d32eb988a0af985a621e023279382R1172-R1178

So just removing the branch entirely ought to be correct!

@aviatesk
Copy link
Sponsor Member Author

No, the specialized append!(::Vector, items::AbstractVector) is dispatched only when items isa AbstractVector.

@Seelengrab
Copy link
Contributor

Ah, of course 🤦

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.

Changes to append! inconpatible with TypedTables.jl
3 participants