Add EntityMap::with_capacity.

Create a secondary entity map with space reserved for a known range of entity
references.

Add dfg.num_ebbs() and dfg.num_insts() methods to provide capacities.
This commit is contained in:
Jakob Stoklund Olesen
2016-07-29 15:59:09 -07:00
parent 0ef28f5bde
commit dae349371f
2 changed files with 31 additions and 0 deletions

View File

@@ -102,6 +102,21 @@ impl<K, V> EntityMap<K, V>
where K: EntityRef,
V: Clone + Default
{
/// Create a new secondary `EntityMap` that is prepared to hold `n` elements.
///
/// Use this when the length of the primary map is known:
/// ```
/// let secondary_map = EntityMap::with_capacity(primary_map.len());
/// ```
pub fn with_capacity(n: usize) -> Self {
let mut map = EntityMap {
elems: Vec::with_capacity(n),
unused: PhantomData,
};
map.elems.resize(n, V::default());
map
}
/// Ensure that `k` is a valid key but adding default entries if necesssary.
///
/// Return a mutable reference to the corresponding entry.

View File

@@ -42,6 +42,22 @@ impl DataFlowGraph {
extended_values: Vec::new(),
}
}
/// Get the total number of instructions created in this function, whether they are currently
/// inserted in the layout or not.
///
/// This is intended for use with `EntityMap::with_capacity`.
pub fn num_insts(&self) -> usize {
self.insts.len()
}
/// Get the total number of extended basic blocks created in this function, whether they are
/// currently inserted in the layout or not.
///
/// This is intended for use with `EntityMap::with_capacity`.
pub fn num_ebbs(&self) -> usize {
self.ebbs.len()
}
}
/// Handling values.