-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Fully drop _tuple_any
and improve type-based offset axes check.
#45260
Conversation
8c42ee9
to
8dd18c3
Compare
9d37b62
to
8a02e52
Compare
base/range.jl
Outdated
@@ -687,6 +687,13 @@ step_hp(r::AbstractRange) = step(r) | |||
|
|||
axes(r::AbstractRange) = (oneto(length(r)),) | |||
|
|||
# Needed to ensure `has_offset_axes` can constant-fold. | |||
has_offset_axes(::StepRange) = false | |||
let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} | |
let baseints = BitInteger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BitInteger
is defined later in int.jl
. Maybe we can replace it with Union{bigints, smallints}
base/range.jl
Outdated
has_offset_axes(::StepRange) = false | ||
let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} | ||
global firstindex | ||
firstindex(::StepRange{T,<:baseints}) where {T<:baseints} = sizeof(T) < sizeof(Int) ? 1 : one(T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T
might be a Union and not have a size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we should use sizeof(first(r))
instead.
The new length
has similar problem.
To make sure the result is correct, we'd better split L694 into:
firstindex(r::StepRange{<:bigints,<:BitInteger}) = one(last(r)-first(r))
firstindex(::StepRange{<:smallints,<:BitInteger}) = 1
@@ -799,7 +806,7 @@ let smallints = (Int === Int64 ? | |||
function length(r::OrdinalRange{<:smallints}) | |||
s = step(r) | |||
isempty(r) && return 0 | |||
return div(Int(last(r)) - Int(first(r)), s) + 1 | |||
return Int(div(Int(last(r)) - Int(first(r)), s)) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure it is legal to cast this to an Int
? This seems highly unrelated to the commit message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for length(::StepRange{Int8,Int128})
. Without this, we'll return a Int128
for non-empty r
.
I believe it's safe as we convert the div
result rather than step s
. Int
should be enough to represent the length
987a9b0
to
ab36599
Compare
`_any_tuple` should be functionally consistent. (And has no bootstrap problem.)
And define some "cheap" `firstindex`
This avoid some unneeded `axes` call, thus more possible be folded by the compiler.
Co-Authored-By: Jameson Nash <[email protected]>
And define `firstindex` accordingly. Co-Authored-By: Jameson Nash <[email protected]>
* Follow up to JuliaLang#45236 (make `length(::StepRange{Int8,Int128})` type-stable) * Fully drop `_tuple_any` (unneeded now) * Make sure `has_offset_axes(::StepRange)` could be const folded. And define some "cheap" `firstindex` * Do offset axes check on `A`'s parent rather than itself. This avoid some unneeded `axes` call, thus more possible be folded by the compiler. Co-authored-by: Jameson Nash <[email protected]>
* Follow up to JuliaLang#45236 (make `length(::StepRange{Int8,Int128})` type-stable) * Fully drop `_tuple_any` (unneeded now) * Make sure `has_offset_axes(::StepRange)` could be const folded. And define some "cheap" `firstindex` * Do offset axes check on `A`'s parent rather than itself. This avoid some unneeded `axes` call, thus more possible be folded by the compiler. Co-authored-by: Jameson Nash <[email protected]>
Base._tuple_any
is only used inBase.has_offset_axes
.After #44063, we have a similar method
_any_tuple
, which should be functionally consistent for offset axes check.This PR fully removes
_tuple_any
, and makeBase.has_offset_axes
call_any_tuple
.(Ideally, we'd better call public api
any
instead. But our compiler fails to fold some check as a const.)This PR also omits some
axes
call inhas_offset_axes
by checkingindices
/parent
'saxes
rather than self'saxes
.This helps #44040 a little.
Test added.