-
-
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
SubArray bounds checking (or not): bug or feature? #4044
Comments
I should also clarify that the issue of bounds-checking upon construction is separate from the issue of bounds-checking on
That does not depend on the fact that
In other words, So, to summarize: my recommendation is for no bounds-checking on construction, but have bounds-checking on |
Ah, I've realized that we can't currently add |
The idea of SubArrays that overlap regions outside an array is kind of neat. |
I see this as a feature, not a bug. It is still "safe" in the sense of throwing an exception of the bounds of the original array are exceeded. |
Can you give an example of where this behavior is useful? |
Here's an example from what I'm doing right now (this isn't quite accurate, but explaining the precise analysis would take a lot of time...): I have some movies, and I want to track movement of objects during the course of the movie. The movie is a big array, and I track an object by drawing a box around it (a SubArray). What happens when the object moves near the edge of the frame, so that my centered box would stick out beyond the edge? If the SubArray barfs on construction, then I find that I needed a fair bit more code to handle special cases. If it doesn't barf on construction, then I'm finding that it reduces the number of places in the code that I have to explicitly think about boundaries. So, there's a real-world application showing where it's useful. That said, a person could rationally argue that such facilities don't belong in SubArray. If that's how this discussion works out, I can easily go off in my own direction with my own custom types (it may happen anyway, of course). There are pluses and minuses, for both me and Julia, either way. |
Thanks for the example. How would/do you handle the |
Yes, in general this is useful for defining a |
Yep, our The other nice property about not being picky at construction time is that shifts become commutative. Here's a 1d example:
Now:
yields no shift, as does
However, this second one would give an error if we were picky at construction time. |
OK, it sounds like this is a feature. I'll add a brief note pointing to this issue in the subarray.jl code, and fix the |
I wonder if the code in |
It's now intentional and well-defined in the sense that I've set up tests that will fail if this behavior changes. |
I believe this feature means that a loop from |
I panicked about this a month or two ago, and then convinced myself that it isn't a problem (but that there's another, much smaller, problem). As far as I can tell---and I could easily be wrong--- One might complain about not being able to turn off bounds checking "all the way down" (and I have practical cases of abstraction nesting 4 deep, e.g., a Or, is layer-crossing of |
(My example assumed that SubArrays index their parent in a cartesian manner, which they don't currently. But someday they will.) |
Incorrect --- there is no check for The bug here is real:
|
Yes, I knew that, I was imagining you brought this up because you were in the middle of trying to fix that problem 😄. Now I see you were visiting @lindahua's But I somehow misdiagnosed the sloppiness problem---I am almost sure I had explicitly tested this very scenario, though perhaps I failed to wrap it inside a function. So yes, this is a big problem. The issue, as you say, is that there isn't a good way to do this unless the type encodes whether it's safe to be sloppy. Given how I see two ways forward:
The easy one seems inevitable, and fairly urgent. Sigh. |
Just had to test it:
No wonder I thought this wasn't a problem. But of course your analysis is the correct one. |
I don't fully understand how The alternative, I presume, is to write bounds-checking into |
Is this still 0.3 blocking? Maybe, we should tackle this together with the array view stuff in the 0.4 cycle? |
I don't think it should block 0.3. |
Ok, let's leave this for the array view change. |
…ction This may change someday (#4044), but for now it's best to not check.
BTW, here's my personal plan for fixing this in a way that lets us have our cake and eat it too:
I think this plan will let us still do the fun things at the top of this issue, would furthermore enable fun stuff like this: a = rand(5)
b = sub(a, 2:4) # provides "padding"
c = similar(b)
for i = 1:length(b)
@inbounds c[i] = b[i-1] - 2*b[i] + b[i+1]
end and yet resolve the concern that @JeffBezanson raised. But all this awaits #8227. |
SubArrays currently do not check bounds upon construction:
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:
A.indexes[i][j]
needs to becomefirst(A.indexes[i]) + (j-1)*step(A.indexes[i])
, which is mathematically equivalent except for bounds-checking.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.
The text was updated successfully, but these errors were encountered: