Skip to content

SubArray bounds checking (or not): bug or feature? #4044

Closed
@timholy

Description

@timholy

SubArrays currently do not check bounds upon construction:

julia> A = randn(4,8)
4x8 Float64 Array:
 -0.661697  0.150336   -0.979774   0.350783   1.97253   -0.255409   -0.464847   -0.785631
 -0.389022  0.0357723  -2.96341    0.285687   0.446166  -0.0916746  -0.64864     0.476115
 -0.972825  0.037842    0.456445   0.752897  -0.733183   0.320432   -1.00636     2.2454  
  0.964869  0.467261   -0.564566  -2.38089    1.70834    0.0517955  -0.0813409   0.302723

julia> As = sub(A, 1:3, 0:3)
3x4 SubArray of 4x8 Float64 Array:
 #undef  -0.661697  0.150336   -0.979774
 #undef  -0.389022  0.0357723  -2.96341 
 #undef  -0.972825  0.037842    0.456445

julia> As[1,1]
ERROR: BoundsError()
 in getindex at subarray.jl:172

julia> As[1,2]
-0.6616974979624464

This lack of bounds-checking turns out to be incredibly useful for certain applications, and I'm starting to write more and more code that takes advantage of it. However, I also recognize that this may be unintended behavior.

Hence, I'd like to request that we make a decision: is this a bug or a feature? I see three paths:

  • It's a feature; keep it as it is. There are a couple of lines of code that will need fixing. Specifically, in a few places A.indexes[i][j] needs to become first(A.indexes[i]) + (j-1)*step(A.indexes[i]), which is mathematically equivalent except for bounds-checking.
  • It should be an optional feature (introduce an extra parameter into the type definition)
  • It's a bug. We will check bounds on construction, but then we also need an AbstractStridedArray type from which "private" variants can descend.

Left to my own devices I would choose the first. But I'll be happy to implement whichever of these achieves some reasonable facsimile of consensus.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorneeds decisionA decision on this change is needed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions