Support empty AvailableData structs#138
Conversation
…vailableDataContext.__enter__.
|
@nclack @andy-sweet This PR is ready to go, but Ghanima needs to update |
andy-sweet
left a comment
There was a problem hiding this comment.
Python code looks much better now.
But I'm less confident on the Rust changes. Here's what I understand.
RawAvailableDataunmaps frame data when it's dropped.AvailableDatajust wrapsRawAvailableDataas an optional and implements its methods appropriately.AvailableDataContextmaps frame data it's entered and will triggerRawAvailableDatato be dropped when exited.
Based on this, as a Python client, I assume that the only way to access frame data safely is to use with runtime.get_available_data(...) and once that has exited, it is no longer safe to refer to any frames or their data unless i made a copy of that data.
Therefore, I think removing _store and the subsequent changes seems safe. Previously I understood that existed to share ownership of RawAvailableData and thus keep it alive (and its data mapped) while any of those objects (e.g. VideoFrame) continued to exist. That ownership model seems more complicated and possibly buggy (based on your comments about the failing tests) - it also contrasts with the paragraph above.
So, I roughly approve this. But it definitely needs a look from @nclack before merging.
There was a problem hiding this comment.
We want to hide the invalidate method.
There is a lifetime issue with the VideoFrame - it would be nice if it were invalidated outside of the AvailableDataContext. Adding back the Arc<AvailableData> and generating an informative error should be fine.
That doesn't help with invalidated numpy arrays. I think we can address that in a separate issue. That probably requires making a custom object that implements the numpy array interface protocol. In the meantime we should document that the numpy array has a lifetime limited to the context.
nclack
left a comment
There was a problem hiding this comment.
lgtm. I left a comment/question but I don't think it's blocking
src/runtime.rs
Outdated
| pub(crate) struct AvailableData { | ||
| inner: Option<Arc<RawAvailableData>>, | ||
| inner: Option<RawAvailableData>, | ||
| valid: Arc<Mutex<bool>>, |
There was a problem hiding this comment.
This works but the valid flag is redundant with the Option on inner - they serve the same purpose. So I might have done Arc<Mutex<Option<RawAvailableData> here.
I'm wondering if the Arc<Mutex<...>> is really necessary though. I believe AvailableData is always accessed with the GIL.
There was a problem hiding this comment.
I started out with store: Arc<AvailableData> as a member in both VideoFrameIterator and VideoFrame, but I couldn't create one in AvailableData::frames() since that only has a reference to self. So I went with this Arc<Mutex<bool>> approach, which unfortunately needs to have this member here if only as a way to signal downstream VideoFrameIterator and VideoFrame objects, since they can't get an Arc of the AvailableData itself.
But actually typing this out has made me think to try with store: Arc<&AvailableData>, but now I have to figure out how to satisfy the borrow checker. My Rust is weak or I'd have thought of it sooner.
There was a problem hiding this comment.
Trying it out I pretty quickly ran into this problem. (Explanation from PyO3 here.) Because we have these pyclasses, I can't specify lifetimes on the AvailableData reference. So unfortunately I think the ugly Arc<Mutex<bool>> option is the only way to go. We can discuss at standup.
Closes #137.
AvailableDatastruct on every call toruntime.get_available_data(). It's no longerOptional, i.e., will never beNone.🚨🚨 Potentially controversial decisions below 🚨🚨
_storefield fromVideoFramestruct. This was causingtest_setup()to fail when I did not explicitly delete frames returned by the video frame iterator.storefield ofVideoFrameIteratorInnerstruct was unused, so I removed this as well.RawAvailableDatais used is as a memberinnerofAvailableData, so I madeAvailableData.inneranOption<RawAvailableData>, where it was previously anOption<Arc<RawAvailableData>>.