Skip to content
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

Treat some with_capacity calls as a hint. #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions mp4parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ struct HashMap;
#[allow(dead_code)]
struct String;

// Arbitrary 1MB limit.
const CAPACITY_HINT_LIMIT: usize = 1024 * 1024;

/// Returns a `TryVec<T>` with preallocated capacity for up to `expected_items`.
/// `expected_items` is treated as a hint only and the returned `TryVec<T>`'s
/// capacity may be less than `expected_items`.
fn vec_with_capacity_hint<T>(expected_items: usize) -> Result<TryVec<T>, TryReserveError> {
let reserved_items = expected_items.min(CAPACITY_HINT_LIMIT / std::mem::size_of::<T>());
TryVec::with_capacity(reserved_items)
}

/// Returns a `TryHashMap<T>` with preallocated capacity for up to `expected_items`.
/// `expected_items` is treated as a hint only and the returned `TryHashMap<T>`'s
/// capacity may be less than `expected_items`.
fn hashmap_with_capacity_hint<K, V>(
expected_items: usize,
) -> Result<TryHashMap<K, V>, TryReserveError>
where
K: Eq + std::hash::Hash,
{
let reserved_items = expected_items
.min(CAPACITY_HINT_LIMIT / (std::mem::size_of::<K>() + std::mem::size_of::<V>()));
TryHashMap::with_capacity(reserved_items)
}

/// The return value to the C API
/// Any detail that needs to be communicated to the caller must be encoded here
/// since the [`Error`] type's associated data is part of the FFI.
Expand Down Expand Up @@ -2962,7 +2987,7 @@ fn read_iinf<T: Read>(
} else {
be_u32(src)?.to_usize()
};
let mut item_infos = TryVec::with_capacity(entry_count)?;
let mut item_infos = vec_with_capacity_hint(entry_count)?;

let mut iter = src.box_iter();
while let Some(mut b) = iter.next_box()? {
Expand Down Expand Up @@ -4071,7 +4096,7 @@ fn read_iloc<T: Read>(src: &mut BMFFBox<T>) -> Result<TryHashMap<ItemId, ItemLoc
IlocVersion::Two => iloc.read_u32(32)?,
};

let mut items = TryHashMap::with_capacity(item_count.to_usize())?;
let mut items = hashmap_with_capacity_hint(item_count.to_usize())?;

for _ in 0..item_count {
let item_id = ItemId(match version {
Expand Down Expand Up @@ -4691,7 +4716,7 @@ fn read_tkhd<T: Read>(src: &mut BMFFBox<T>) -> Result<TrackHeaderBox> {
fn read_elst<T: Read>(src: &mut BMFFBox<T>) -> Result<EditListBox> {
let (version, flags) = read_fullbox_extra(src)?;
let edit_count = be_u32(src)?;
let mut edits = TryVec::with_capacity(edit_count.to_usize())?;
let mut edits = vec_with_capacity_hint(edit_count.to_usize())?;
for _ in 0..edit_count {
let (segment_duration, media_time) = match version {
1 => {
Expand Down Expand Up @@ -4770,7 +4795,7 @@ fn read_mdhd<T: Read>(src: &mut BMFFBox<T>) -> Result<MediaHeaderBox> {
fn read_stco<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
let (_, _) = read_fullbox_extra(src)?;
let offset_count = be_u32(src)?;
let mut offsets = TryVec::with_capacity(offset_count.to_usize())?;
let mut offsets = vec_with_capacity_hint(offset_count.to_usize())?;
for _ in 0..offset_count {
offsets.push(be_u32(src)?.into())?;
}
Expand All @@ -4786,7 +4811,7 @@ fn read_stco<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
fn read_co64<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
let (_, _) = read_fullbox_extra(src)?;
let offset_count = be_u32(src)?;
let mut offsets = TryVec::with_capacity(offset_count.to_usize())?;
let mut offsets = vec_with_capacity_hint(offset_count.to_usize())?;
for _ in 0..offset_count {
offsets.push(be_u64(src)?)?;
}
Expand All @@ -4802,7 +4827,7 @@ fn read_co64<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
fn read_stss<T: Read>(src: &mut BMFFBox<T>) -> Result<SyncSampleBox> {
let (_, _) = read_fullbox_extra(src)?;
let sample_count = be_u32(src)?;
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
let mut samples = vec_with_capacity_hint(sample_count.to_usize())?;
for _ in 0..sample_count {
samples.push(be_u32(src)?)?;
}
Expand All @@ -4818,7 +4843,7 @@ fn read_stss<T: Read>(src: &mut BMFFBox<T>) -> Result<SyncSampleBox> {
fn read_stsc<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleToChunkBox> {
let (_, _) = read_fullbox_extra(src)?;
let sample_count = be_u32(src)?;
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
let mut samples = vec_with_capacity_hint(sample_count.to_usize())?;
for _ in 0..sample_count {
let first_chunk = be_u32(src)?;
let samples_per_chunk = be_u32(src)?;
Expand Down Expand Up @@ -4850,7 +4875,7 @@ fn read_ctts<T: Read>(src: &mut BMFFBox<T>) -> Result<CompositionOffsetBox> {
return Status::CttsBadSize.into();
}

let mut offsets = TryVec::with_capacity(counts.to_usize())?;
let mut offsets = vec_with_capacity_hint(counts.to_usize())?;
for _ in 0..counts {
let (sample_count, time_offset) = match version {
// According to spec, Version0 shoule be used when version == 0;
Expand Down Expand Up @@ -4904,7 +4929,7 @@ fn read_stsz<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleSizeBox> {
fn read_stts<T: Read>(src: &mut BMFFBox<T>) -> Result<TimeToSampleBox> {
let (_, _) = read_fullbox_extra(src)?;
let sample_count = be_u32(src)?;
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
let mut samples = vec_with_capacity_hint(sample_count.to_usize())?;
for _ in 0..sample_count {
let sample_count = be_u32(src)?;
let sample_delta = be_u32(src)?;
Expand Down Expand Up @@ -5892,7 +5917,7 @@ fn read_stsd<T: Read>(src: &mut BMFFBox<T>, track: &mut Track) -> Result<SampleD
}

let description_count = be_u32(src)?.to_usize();
let mut descriptions = TryVec::with_capacity(description_count)?;
let mut descriptions = vec_with_capacity_hint(description_count)?;

let mut iter = src.box_iter();
while descriptions.len() < description_count {
Expand Down