Skip to content

Commit 91cf105

Browse files
committed
perf(allocator): increase initial chunk size from 512B to 16KB (#20968)
Repeat of #18234. #20963 wiped all changes to `bump.rs`, going back to a fresh copy of `bumpalo`. Re-apply changes from #18234 which were lost in the process - increasing default arena chunk size to 16 KiB, and altering `try_with_min_align_and_capacity` to respect the requested capacity in `Bump::with_capacity`. Add a comment to `try_with_min_align_and_capacity` explaining why that change is required.
1 parent cbc0c21 commit 91cf105

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

crates/oxc_allocator/src/bump.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,10 @@ const OVERHEAD: usize = match round_up_to(MALLOC_OVERHEAD + FOOTER_SIZE, CHUNK_A
518518
None => panic!(),
519519
};
520520

521-
// The target size of our first allocation, including our overhead. The
522-
// available bump capacity will be smaller.
523-
const FIRST_ALLOCATION_GOAL: usize = 1 << 9;
521+
// The target size of our first allocation, including our overhead.
522+
// The available bump capacity will be slightly smaller.
523+
// 16 KiB covers the majority of real-world JS/TS files.
524+
const FIRST_ALLOCATION_GOAL: usize = 16 * 1024;
524525

525526
// The actual size of the first allocation is going to be a bit smaller than the
526527
// goal. We need to make room for the footer, and we also need take the
@@ -752,7 +753,10 @@ impl<const MIN_ALIGN: usize> Bump<MIN_ALIGN> {
752753

753754
let chunk_footer = unsafe {
754755
Self::new_chunk(
755-
Self::new_chunk_memory_details(None, layout).ok_or(AllocErr)?,
756+
// `new_size_without_footer` here was `None` in original `bumpalo` code.
757+
// Changed to `Some(capacity)` when we increased `FIRST_ALLOCATION_GOAL` to 16 KiB,
758+
// to avoid `Bump::with_capacity` allocating 16 KiB even when requested `capacity` is much smaller.
759+
Self::new_chunk_memory_details(Some(capacity), layout).ok_or(AllocErr)?,
756760
layout,
757761
EMPTY_CHUNK.get(),
758762
)
@@ -2610,7 +2614,12 @@ mod tests {
26102614
// Uses private `DEFAULT_CHUNK_SIZE_WITHOUT_FOOTER` and `FOOTER_SIZE`.
26112615
#[test]
26122616
fn allocated_bytes() {
2613-
let mut b = Bump::with_capacity(1);
2617+
let mut b = Bump::new();
2618+
2619+
assert_eq!(b.allocated_bytes(), 0);
2620+
assert_eq!(b.allocated_bytes_including_metadata(), 0);
2621+
2622+
b.alloc(0u8);
26142623

26152624
assert_eq!(b.allocated_bytes(), DEFAULT_CHUNK_SIZE_WITHOUT_FOOTER);
26162625
assert_eq!(
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
File | File size || Sys allocs | Sys reallocs || Arena allocs | Arena reallocs | Arena bytes
22
-------------------------------------------------------------------------------------------------------------------------------------------
3-
checker.ts | 2.92 MB || 334 | 37 || 152659 | 28244
3+
checker.ts | 2.92 MB || 333 | 37 || 152659 | 28244
44

5-
cal.com.tsx | 1.06 MB || 20244 | 46 || 37147 | 4570
5+
cal.com.tsx | 1.06 MB || 20242 | 46 || 37147 | 4570
66

7-
RadixUIAdoptionSection.jsx | 2.52 kB || 41 | 6 || 30 | 6
7+
RadixUIAdoptionSection.jsx | 2.52 kB || 34 | 6 || 30 | 6
88

99
pdf.mjs | 567.30 kB || 3994 | 417 || 47464 | 7734
1010

11-
antd.js | 6.69 MB || 7755 | 1657 || 331673 | 69344
11+
antd.js | 6.69 MB || 7751 | 1657 || 331673 | 69344
1212

13-
binder.ts | 193.08 kB || 79 | 23 || 7075 | 824
13+
binder.ts | 193.08 kB || 78 | 23 || 7075 | 824
1414

tasks/track_memory_allocations/allocs_semantic.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ checker.ts | 2.92 MB || 2281 | 18 |
44

55
cal.com.tsx | 1.06 MB || 14966 | 22 || 0 | 0
66

7-
RadixUIAdoptionSection.jsx | 2.52 kB || 12 | 3 || 0 | 0
7+
RadixUIAdoptionSection.jsx | 2.52 kB || 10 | 3 || 0 | 0
88

99
pdf.mjs | 567.30 kB || 1401 | 208 || 0 | 0
1010

0 commit comments

Comments
 (0)