-
Notifications
You must be signed in to change notification settings - Fork 11
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
We can possibly get rid of a lot of generics through Cow<'_, [u8]> #49
Comments
IIRC the point of using Would using |
@Gnurou Hi Alex! My idea is cros-codecs/src/codec/av1/parser.rs Line 107 in f15db06
By switching to Cow, we remove the T as you did in #51, but users can switch to an owned variant of the slice/frame/obu etc through Note that the input is still &[u8], so they can still use the borrowed versions cost free, which is what we currently do anyways. cros-codecs/src/codec/av1/parser.rs Line 1001 in f15db06
This will also remove this
To summarize: a) we keep the number of copies equal |
Thanks for the clarifications. A few remarks: The current AV1 parser does not need to use
And the code builds just as fine. Users that need their own version of the data can use e.g. That being said, the base idea is still valid and we should consider using it in other parsers - only we probably just need to replace Both |
So I tried to implement this a bit and I think I understand what you want - I have updated PR #51 to include the change to Introducing |
@Gnurou in terms of removing the generics, that was mostly what I had in mind, yes. As for Cow:
Oh it doesn't need to, yes. It's just an added convenience for users. I explain better below.
Not sure I understand. For example:
You can't have an owned version of a) that will create an owned version of the bitstream, not an owned version of The above assumes that you mean using
True you can't get rid of lifetimes, but you can get owned Frames/Slices/Tiles etc with Cow if you want through To make my point a bit more clear (sorry for being a bit verbose here)
With the design I introduced in the AV1 parser, this will also only clone the part of the bitstream that generated the parsed Frame/slice/tile/etc, as we subslice the bitstream when creating the Cow:
|
Mmm one thing I don't quite understand is, since the lifetime remain (and thus an owned |
Correct me if I am wrong, but if you call Playground link to corroborate my point above: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=950ef57fd86788da7a22901353696124
Our code still creates a borrowed version e.g.:
So no copies are being made. Again, a mere convenience if anyone wants to have an owned Frame/slice/tile, at the cost of copying the data. Frankly, #51 also works, because it does away with the generic parameter already. If you want to keep it at that (i.e.: if you feel Cow is not worth the trouble here), then I am also OK with that. One thing that comes to mind is this excerpt from the h.265 VAAPI code:
Look how we have to keep a Vec instead of the slice, which is very cumbersome, e.g.:
Which, again, stems from our inability to have an owned Slice type. I think that would benefit from the Cow approach, for example. |
Addresses partially chromeos#49
Addresses partially chromeos#49
This changes will enable the decoder to copy Nalu/Slice structs to internal state instead of using Vec. Addresses partially chromeos#49
This changes will enable the decoder to copy Nalu/Slice structs to internal state instead of using Vec. Addresses partially #49
I am experimenting with a new design for the AV1 code. One that uses
Cow<'_, [u8]>
in place ofT: AsRef<[u8]>
. This means that we can remove thisT: AsRef<[u8]>
noise from a lot of functions and traits, while making it possible to still buildSlices
,Frames
and etc out of both borrowed and owned data without copying.What's more,
Cow<'_, [u8]>
dereferences to&[u8]
so nothing fundamentally changes in the code as far as I can see. Not only that, but we de facto do not even support anything other than&[u8]
in the decoders, as introducingT
in our backend traits would make them not object safe. UsingCow<'_, [u8]>
circumvents this problem, as there's no generics involved and lifetimes do not break object safety.If the current AV1 design proves successful, we should maybe backport these changes to the other codecs as a way to clean up the codebase by a considerable amount.
The text was updated successfully, but these errors were encountered: