Skip to content

Faster promoting arithmetic #21067

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

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 18 additions & 4 deletions base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ promote_result{T,S}(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) = (@_pure
promote() = ()
promote(x) = (x,)
function promote{T,S}(x::T, y::S)
@_inline_meta
(convert(promote_type(T,S),x), convert(promote_type(T,S),y))
end
promote_typeof(x) = (@_pure_meta; typeof(x))
promote_typeof(x, xs...) = (@_pure_meta; promote_type(typeof(x), promote_typeof(xs...)))
function promote(x, y, z)
@_inline_meta
(convert(promote_typeof(x,y,z), x),
convert(promote_typeof(x,y,z), y),
convert(promote_typeof(x,y,z), z))
Expand Down Expand Up @@ -243,10 +245,22 @@ function sametype_error(input...)
" failed to change any input types")
end

+(x::Number, y::Number) = +(promote(x,y)...)
*(x::Number, y::Number) = *(promote(x,y)...)
-(x::Number, y::Number) = -(promote(x,y)...)
/(x::Number, y::Number) = /(promote(x,y)...)
function +(x::Number, y::Number)
xp, yp = promote(x, y)
Copy link
Member

Choose a reason for hiding this comment

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

Are these changes necessary in addition to the inlining of promote?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I really don't like having to expand these like this – it's very ugly and we've never had to do so before.

Copy link
Member Author

Choose a reason for hiding this comment

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

Certainly much less important with #21069. The main problem is that as we implement more abstractions, the inlining heuristics sometimes seem to fail to realize how simple something really is. I don't think we can exclude the possibility that we're noticing problems that we didn't notice before.

xp + yp
end
function *(x::Number, y::Number)
xp, yp = promote(x, y)
xp * yp
end
function -(x::Number, y::Number)
xp, yp = promote(x, y)
xp - yp
end
function /(x::Number, y::Number)
xp, yp = promote(x, y)
xp / yp
end

"""
^(x, y)
Expand Down