-
-
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
Fix conversion in getindex #26615
Fix conversion in getindex #26615
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,11 +478,23 @@ end | |
|
||
## indexing | ||
|
||
_in_unit_range(v::UnitRange, val, i::Integer) = i > 0 && val <= v.stop && val >= v.start | ||
|
||
function getindex(v::UnitRange{T}, i::Integer) where T | ||
@_inline_meta | ||
ret = convert(T, first(v) + i - 1) | ||
@boundscheck ((i > 0) & (ret <= v.stop) & (ret >= v.start)) || throw_boundserror(v, i) | ||
ret | ||
val = convert(T, v.start + i - 1) | ||
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i) | ||
val | ||
end | ||
|
||
const OverflowSafe = Union{Bool,Int8,Int16,Int32,Int64,Int128, | ||
UInt8,UInt16,UInt32,UInt64,UInt128} | ||
|
||
function getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was trying that, but it turns out |
||
@_inline_meta | ||
val = v.start + i - 1 | ||
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i) | ||
val % T | ||
end | ||
|
||
function getindex(v::OneTo{T}, i::Integer) where 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.
Ah, yes, I totally believe that this is faster — bounds checks are very friendly to branch prediction and when they miss, well, we really don't care about performance anymore.