MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / try_ensure_capacity

Method try_ensure_capacity

cranelift/bitset/src/compound.rs:271–303  ·  view source on GitHub ↗
(&mut self, n: usize)

Source from the content-addressed store, hash-verified

269 /// allocation failure.
270 #[inline]
271 pub fn try_ensure_capacity(&mut self, n: usize) -> Result<(), OutOfMemory> {
272 // Subtract one from the capacity to get the maximum bit that we might
273 // set. If `n` is 0 then nothing need be done as no capacity needs to be
274 // allocated.
275 let (word, _bit) = Self::word_and_bit(match n.checked_sub(1) {
276 None => return Ok(()),
277 Some(n) => n,
278 });
279
280 if word < self.elems.len() {
281 // Already have capacity.
282 return Ok(());
283 }
284
285 // Need to allocate additional capacity.
286
287 assert!(word < usize::try_from(isize::MAX).unwrap());
288
289 let delta = word - self.elems.len();
290 let to_grow = delta + 1;
291
292 // Amortize the cost of growing by at least growing another
293 // `self.elems.len()`, so the new length is double the old length.
294 let to_grow = cmp::max(to_grow, self.elems.len());
295 // Don't make ridiculously small allocations.
296 let to_grow = cmp::max(to_grow, 4);
297
298 let mut new_elems = TryVec::from(mem::take(&mut self.elems));
299 new_elems.reserve_exact(to_grow)?;
300 new_elems.try_extend(iter::repeat(ScalarBitSet::new()).take(to_grow))?;
301 self.elems = new_elems.into_boxed_slice()?;
302 Ok(())
303 }
304
305 /// Insert `i` into this bitset.
306 ///

Callers 2

try_with_capacityMethod · 0.45
ensure_capacityMethod · 0.45

Calls 11

OkFunction · 0.85
maxFunction · 0.85
fromFunction · 0.85
checked_subMethod · 0.80
repeatFunction · 0.50
newFunction · 0.50
lenMethod · 0.45
reserve_exactMethod · 0.45
try_extendMethod · 0.45
takeMethod · 0.45
into_boxed_sliceMethod · 0.45

Tested by

no test coverage detected