Skip to content

Zero initialize Vec returned from mp4parse_fallible branch of allocate_read_buf. #173

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

Merged
merged 1 commit into from
Feb 20, 2019

Conversation

kinetiknz
Copy link
Collaborator

Zero initialize Vec returned from mp4parse_fallible branch of allocate_read_buf.

Also delete vec_reserve, which is completely unused.

Closes #172.

r? @SingingTree please

@kinetiknz kinetiknz self-assigned this Feb 19, 2019
unsafe {
buf.set_len(size);
std::ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len() * std::mem::size_of::<u8>());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can do buf.extend(std::iter::repeat(0).take(size)) to avoid unsafe code. A loop on 0..size with Vec::push calls would probably also compile down to the same thing.

Copy link
Contributor

@SingingTree SingingTree Feb 20, 2019

Choose a reason for hiding this comment

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

Possibly done the way it is to avoid needing to reserve more space? Let me know if I'm mistaken, but if we try_reserve(buf, n), buf.set_len(n), then zero the vec with a ptr, we don't trigger further reserves (set_len guarantees it won't modify buffers). If we extend or push we get will get an extra allocation when we push at len == cap.

Edit for further clarification: I believe the further reserve is an issue because it would result in a non-fallible allocation in the context where we only want fallible allocation (there is also performance, but that is a secondary concern).

Copy link
Contributor

Choose a reason for hiding this comment

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

This is incorrect, the Vec API won’t reserve additional space in the case the iteration fits, and it does here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that Vec::push itself will never reallocate as long as it has space, too. In general, don't use unsafe code if you don't need to.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the correction. I was mistaken as to when the new reserves take place (I was under the impression it was at the time len hit cap).

Copy link
Contributor

@SingingTree SingingTree left a comment

Choose a reason for hiding this comment

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

Provided my understanding of why we use the unsafe code is correct, looks good. Looks like we can just use extend safely to drop zeros in.

…e_read_buf.

Also delete vec_reserve, which is completely unused.

Closes mozilla#172.
@kinetiknz
Copy link
Collaborator Author

Thanks for the feedback from both of you. Updated to use extend.

@kinetiknz kinetiknz merged commit 518b082 into mozilla:master Feb 20, 2019
@kinetiknz kinetiknz deleted the issue172 branch February 20, 2019 23:01
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