Skip to content

Conversation

@tobyxdd
Copy link
Contributor

@tobyxdd tobyxdd commented Mar 15, 2022

Copy link
Member

@sr229 sr229 left a comment

Choose a reason for hiding this comment

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

I assume you would want to copy this pointer before you want to pass this to .NET. Has this been tested and memory profiled?

Copy link
Contributor

@Speykious Speykious left a comment

Choose a reason for hiding this comment

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

I'm really not sure of this change.

If anything, providing a function to create a new image frame with a copied pixel data that MediaPipe owns is a very good idea - however, removing the option to move an existing pixel data? I don't know.

@Speykious
Copy link
Contributor

Do you know if it's possible to leave a byte[] to unmanaged memory in C#?

auto-merge was automatically disabled March 17, 2022 01:50

Head branch was pushed to by a user without write access

Co-authored-by: Speykious <speykious@gmail.com>
@tobyxdd
Copy link
Contributor Author

tobyxdd commented Mar 17, 2022

Do you know if it's possible to leave a byte[] to unmanaged memory in C#?

I'm not exactly familiar with C#, but perhaps something like GCHandle.Alloc(someArr, GCHandleType.Pinned) would work?

@tobyxdd
Copy link
Contributor Author

tobyxdd commented Mar 17, 2022

But tbh I think that's beside the point. If pixel_data is not copied, either way you will need some extra work to manually ensure it's not freed before MediaPipe is done using it, and that it's freed afterwards (to prevent memory leaks). Much easier to just make a copy.

@Speykious
Copy link
Contributor

But tbh I think that's beside the point. If pixel_data is not copied, either way you will need some extra work to manually ensure it's not freed before MediaPipe is done using it, and that it's freed afterwards (to prevent memory leaks). Much easier to just make a copy.

It is indeed easier to copy the pixel data, but definitely not more performant. I would prefer, as I said, to have both options available, so going that extra mile to avoid one more O(n) operation is fine by me.

I'm familiar with the notion of ownership and borrowing thanks to Rust, so something we can do is to add a property on ImageFrame that asserts whether or not it owns the pixel data.

Copy link
Member

@sr229 sr229 left a comment

Choose a reason for hiding this comment

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

Considering we're aiming for C99-like ABI compatibility, this is alright. ImageFrame* is easier to interpolate than unique_ptr.

@sr229 sr229 requested a review from Speykious March 20, 2022 14:31
Copy link
Contributor

@Speykious Speykious left a comment

Choose a reason for hiding this comment

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

I'll allow the replacement. I think the only way to make the native side own a pixel data from the C# side is by creating an unmanaged array, which requires to either make our own class or to use the UnmanagedArray library, which is kind of a bit of work compared to normal byte[]s. Not to mention that I think it wouldn't work well with libraries such as SeeShark where we already don't have any problem in the first place because of how we deal with the memory, but where it could still very likely bite us back in the future.

Just change the name of the function and we good :)

sr229 and others added 2 commits March 20, 2022 22:59
@sr229 sr229 requested a review from Speykious March 20, 2022 15:05
Copy link
Contributor

@Speykious Speykious left a comment

Choose a reason for hiding this comment

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

I approb

@sr229 sr229 merged commit bf32687 into cosyneco:master Mar 20, 2022
@sr229
Copy link
Member

sr229 commented Mar 20, 2022

Thanks for your contributions! Next time, we encourage you to sign your commits so we can authenticate it's really yours to prevent commit masquerading.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants