Skip to content

Commit 98e1688

Browse files
committed
Auto merge of #78058 - bugadani:arena2, r=lcnr
Make sure arenas don't allocate bigger than HUGE_PAGE Right now, arenas allocate based on the size of the last chunk. It is possible for a `grow` call to allocate a chunk that is not a multiple of `PAGE`, and this size is doubled for each subsequent allocation. This means, instead of `HUGE_PAGE`, the biggest page possible is actually unknown. This change fixes this, and also removes an unnecessary checked multiplication. It is still possible to allocate bigger than `HUGE_PAGE` pages, but this will only happen as many times as absolutely necessary.
2 parents ad268bd + 396561b commit 98e1688

File tree

1 file changed

+4
-8
lines changed
  • compiler/rustc_arena/src

1 file changed

+4
-8
lines changed

compiler/rustc_arena/src/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,8 @@ impl<T> TypedArena<T> {
223223
// If the previous chunk's len is less than HUGE_PAGE
224224
// bytes, then this chunk will be least double the previous
225225
// chunk's size.
226-
new_cap = last_chunk.storage.len();
227-
if new_cap < HUGE_PAGE / elem_size {
228-
new_cap = new_cap.checked_mul(2).unwrap();
229-
}
226+
new_cap = last_chunk.storage.len().min(HUGE_PAGE / elem_size / 2);
227+
new_cap = new_cap * 2;
230228
} else {
231229
new_cap = PAGE / elem_size;
232230
}
@@ -343,10 +341,8 @@ impl DroplessArena {
343341
// If the previous chunk's len is less than HUGE_PAGE
344342
// bytes, then this chunk will be least double the previous
345343
// chunk's size.
346-
new_cap = last_chunk.storage.len();
347-
if new_cap < HUGE_PAGE {
348-
new_cap = new_cap.checked_mul(2).unwrap();
349-
}
344+
new_cap = last_chunk.storage.len().min(HUGE_PAGE / 2);
345+
new_cap = new_cap * 2;
350346
} else {
351347
new_cap = PAGE;
352348
}

0 commit comments

Comments
 (0)