Skip to content

Commit 55a1a52

Browse files
committed
Reduce grow_and_alloc_raw to a single call site.
The current structure is clumsy, calling `alloc_raw_without_grow` in one function, and then if that fails, calling another function that calls `alloc_raw_without_grow` again.
1 parent 25407bc commit 55a1a52

File tree

1 file changed

+12
-12
lines changed
  • compiler/rustc_arena/src

1 file changed

+12
-12
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ impl Default for DroplessArena {
412412
}
413413

414414
impl DroplessArena {
415+
#[inline(never)]
416+
#[cold]
415417
fn grow(&self, layout: Layout) {
416418
// Add some padding so we can align `self.end` while
417419
// still fitting in a `layout` allocation.
@@ -451,13 +453,6 @@ impl DroplessArena {
451453
}
452454
}
453455

454-
#[inline(never)]
455-
#[cold]
456-
fn grow_and_alloc_raw(&self, layout: Layout) -> *mut u8 {
457-
self.grow(layout);
458-
self.alloc_raw_without_grow(layout).unwrap()
459-
}
460-
461456
/// Allocates a byte slice with specified layout from the current memory
462457
/// chunk. Returns `None` if there is no free space left to satisfy the
463458
/// request.
@@ -488,12 +483,17 @@ impl DroplessArena {
488483
#[inline]
489484
pub fn alloc_raw(&self, layout: Layout) -> *mut u8 {
490485
assert!(layout.size() != 0);
491-
if let Some(a) = self.alloc_raw_without_grow(layout) {
492-
return a;
486+
487+
// This loop executes once or twice: if allocation fails the first
488+
// time, the `grow` ensures it will succeed the second time.
489+
loop {
490+
if let Some(a) = self.alloc_raw_without_grow(layout) {
491+
return a;
492+
}
493+
// No free space left. Allocate a new chunk to satisfy the request.
494+
// On failure the grow will panic or abort.
495+
self.grow(layout);
493496
}
494-
// No free space left. Allocate a new chunk to satisfy the request.
495-
// On failure the grow will panic or abort.
496-
self.grow_and_alloc_raw(layout)
497497
}
498498

499499
#[inline]

0 commit comments

Comments
 (0)