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

Make map() and broadcast() faster for Union{T, Missing/Nothing} element types #25828

Merged
merged 1 commit into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,11 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
i = offs
while !done(itr, st)
el, st = next(itr, st)
S = typeof(el)
if S === T || S <: T
if el isa T || typeof(el) === T
Copy link
Member Author

Choose a reason for hiding this comment

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

The need for typeof(el) === T is a bit of a mystery, but it was weird to need it in the existing code already. See #25799.

Copy link
Member

Choose a reason for hiding this comment

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

I assume that was some attempt at providing a fast-path for uninferred code. But S === T is often much harder to infer than S <: T. And since isconcretetype(S), it won't be faster at runtime either.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yet if I remove that check I get a worse performance than before with map(identity, y) (see what I noted at #25799). That's really weird, but...

Copy link
Member

Choose a reason for hiding this comment

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

Keno recently changed the result of el isa T from Const(true) to Conditional(:el, T, Union{}) – maybe there's was a mistake in the implementation of that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Where's the relevant commit? Can I try reverting it?

@inbounds dest[i] = el::T
Copy link
Member

Choose a reason for hiding this comment

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

if you remove the typeof(el) === T condition above, inference will be able to infer the el::T result and you can remove this redundant type-check

i += 1
else
R = promote_typejoin(T, S)
R = promote_typejoin(T, typeof(el))
new = similar(dest, R)
copyto!(new,1, dest,1, i-1)
@inbounds new[i] = el
Expand Down
5 changes: 2 additions & 3 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,13 @@ end
@nexprs $nargs i->(@inbounds val_i = _broadcast_getindex(A_i, I_i))
# call the function
V = @ncall $nargs f val
S = typeof(V)
# store the result
if S <: eltype(B)
if V isa eltype(B)
Copy link
Member

Choose a reason for hiding this comment

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

+1

This syntax allows the compiler to see / "prove" that S <: T if this branch is taken, and thus likely eliminate dispatch in the assignment below

@inbounds B[I] = V
else
# This element type doesn't fit in B. Allocate a new B with wider eltype,
# copy over old values, and continue
newB = Base.similar(B, promote_typejoin(eltype(B), S))
newB = Base.similar(B, promote_typejoin(eltype(B), typeof(V)))
for II in Iterators.take(iter, count)
newB[II] = B[II]
end
Expand Down