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

Refactor scalar range getindex #50467

Merged
merged 10 commits into from
Aug 30, 2023
Merged

Conversation

LilithHafner
Copy link
Member

The point of this is to make these internals simpler and easier to edit going forward. I originally wanted to include these changes with #50118, but held back to expedite that bugfix.

Functional changes are

  • unsafe_getindex(::AbstractRange, ::Bool) no longer throws
  • removed @inline annotations, but the functions annotated with @inline should typically be below the inlining threshold anyway.

internal functional changes are
- unsafe_getindex(::AbstractRange, ::Bool) no longer throws
- removed @inline annotations
@LilithHafner LilithHafner added the ranges Everything AbstractRange label Jul 7, 2023
base/range.jl Outdated Show resolved Hide resolved
Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

The general direction SGTM

github-merge-queue bot pushed a commit that referenced this pull request Jul 15, 2023
I noticed
[here](#50467 (comment))
that `lastindex(x::Base.OneTo)` is not simply `x.stop`. This PR performs
that optimization and many more by assuming `size` always returns
positive numbers.
```
julia> @code_native lastindex(Base.OneTo(5)) # master
        .section        __TEXT,__text,regular,pure_instructions
        .build_version macos, 13, 0
        .globl  _julia_lastindex_81             ; -- Begin function julia_lastindex_81
        .p2align        2
_julia_lastindex_81:                    ; @julia_lastindex_81
; ┌ @ abstractarray.jl:423 within `lastindex`
; %bb.0:                                ; %top
; │┌ @ abstractarray.jl:386 within `eachindex`
; ││┌ @ abstractarray.jl:134 within `axes1`
; │││┌ @ range.jl:708 within `axes`
; ││││┌ @ range.jl:471 within `oneto`
; │││││┌ @ range.jl:469 within `OneTo` @ range.jl:454
; ││││││┌ @ promotion.jl:532 within `max`
; │││││││┌ @ int.jl:83 within `<`
        ldr     x8, [x0]
; │││││││└
; │││││││┌ @ essentials.jl:642 within `ifelse`
        cmp     x8, #0
        csel    x0, x8, xzr, gt
; │└└└└└└└
        ret
; └
                                        ; -- End function
.subsections_via_symbols

julia> @code_native lastindex(Base.OneTo(5)) # pr
        .section        __TEXT,__text,regular,pure_instructions
        .build_version macos, 13, 0
        .globl  _julia_lastindex_13253          ; -- Begin function julia_lastindex_13253
        .p2align        2
_julia_lastindex_13253:                 ; @julia_lastindex_13253
; ┌ @ abstractarray.jl:423 within `lastindex`
; %bb.0:                                ; %top
        ldr     x0, [x0]
        ret
; └
                                        ; -- End function
.subsections_via_symbols
```

Also removed `axes(r::AbstractRange) = (oneto(length(r)),)` (added in
#40382, @vtjnash) as redundant with the general `axes` method.

The obvious downside here is that if someone defines an object with
negative size, its axes will include Base.OneTo with negative stop. I
think that is acceptable, but if not, we can gate this optimization to a
set of known types (all AbstractArray types defined in Base should have
non-negative size)
base/range.jl Outdated Show resolved Hide resolved
base/array.jl Outdated Show resolved Hide resolved
Co-authored-by: Denis Barucic <[email protected]>
base/range.jl Show resolved Hide resolved
base/range.jl Show resolved Hide resolved
Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

LGTM

@LilithHafner LilithHafner added the merge me PR is reviewed. Merge when all tests are passing label Aug 30, 2023
@LilithHafner
Copy link
Member Author

Oops, I forgot about this.

@LilithHafner LilithHafner merged commit 6f026e3 into master Aug 30, 2023
7 checks passed
@LilithHafner LilithHafner deleted the range-scalar-getindex-refactor branch August 30, 2023 22:06
@LilithHafner LilithHafner added performance Must go faster regression Regression in behavior compared to a previous version and removed merge me PR is reviewed. Merge when all tests are passing labels Aug 30, 2023
@LilithHafner
Copy link
Member Author

@vtjnash found a performance regression due to this PR

LilithHafner added a commit that referenced this pull request Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster ranges Everything AbstractRange regression Regression in behavior compared to a previous version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants