Skip to content

All the getindex implementations should use view, not slice #26

Open

Description

@jpivarski

I just noticed that slices copy! They were supposed to be views.

Activity

  1. Moelf commented on Aug 31, 2023

    @Moelf
    Member

    hmm, I'm pretty sure you're not supposed to make a default slice "view"

    JuliaLang/julia#47078

  2. jpivarski commented on Aug 31, 2023

    @jpivarski
    MemberAuthor

    This is what I'm seeing, with the concrete Vector class, at least:

    julia> original = Vector([1.1, 2.2, 3.3, 4.4, 5.5])
    5-element Vector{Float64}:
     1.1
     2.2
     3.3
     4.4
     5.5
    
    julia> sliced = original[2:4]
    3-element Vector{Float64}:
     2.2
     3.3
     4.4
    
    julia> viewed = view(original, 2:4)
    3-element view(::Vector{Float64}, 2:4) with eltype Float64:
     2.2
     3.3
     4.4
    
    julia> original[3] = 9999
    9999
    
    julia> sliced
    3-element Vector{Float64}:
     2.2
     3.3
     4.4
    
    julia> viewed
    3-element view(::Vector{Float64}, 2:4) with eltype Float64:
        2.2
     9999.0
        4.4

    Oh, but maybe this means that the AwkwardArray getindex is correct because it copies, and I should additionally provide a view override.

    (In Python, Content instances are considered immutable, so they view everywhere. But AwkwardArray.jl implements Base.push! and Base.append! on its Content instances, so perhaps they should not be presumed immutable. It's also less necessary to require immutability, since typical usage in Julia will be to iterate over arrays, rather than call functions that replace arrays with new arrays.)

  3. Moelf commented on Aug 31, 2023

    @Moelf
    Member

    you don't need to add view override yourself. All AbstractArray which has getindex() correctly implemented has view() by default.

    You can use @view on one expression, or @views on a whole lexical code block to recover Numpy default slicing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions