From a6bf253602702c052453d5ccb45a40d8fbb368ba Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 29 Aug 2024 11:55:20 +0200 Subject: [PATCH] LibJS: Use the system native page size as the HeapBlock::block_size Before this change, we were hard-coding 4 KiB. This meant that systems with a 16 KiB native page size were wasting 12 KiB per HeapBlock on nothing, leading to worse locality and more mmap/madvise churn. We now query the system page size on startup and use that as the HeapBlock size. The only downside here is that some of the pointer math for finding the base of a HeapBlock now has to use a runtime computed value instead of a compile time constant. But that's a small price to pay for what we get. --- Userland/Libraries/LibJS/Heap/BlockAllocator.cpp | 3 --- Userland/Libraries/LibJS/Heap/HeapBlock.cpp | 4 +++- Userland/Libraries/LibJS/Heap/Internals.h | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp b/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp index 634e3f457bf..3fcaf9c7cc9 100644 --- a/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp +++ b/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp @@ -22,9 +22,6 @@ namespace JS { -// NOTE: If this changes, we need to update the mmap() code to ensure correct alignment. -static_assert(HeapBlock::block_size == 4096); - BlockAllocator::~BlockAllocator() { for (auto* block : m_blocks) { diff --git a/Userland/Libraries/LibJS/Heap/HeapBlock.cpp b/Userland/Libraries/LibJS/Heap/HeapBlock.cpp index b1a65440790..ccbe0bbbdae 100644 --- a/Userland/Libraries/LibJS/Heap/HeapBlock.cpp +++ b/Userland/Libraries/LibJS/Heap/HeapBlock.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2024, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -18,6 +18,8 @@ namespace JS { +size_t HeapBlockBase::block_size = PAGE_SIZE; + NonnullOwnPtr HeapBlock::create_with_cell_size(Heap& heap, CellAllocator& cell_allocator, size_t cell_size, [[maybe_unused]] char const* class_name) { char const* name = nullptr; diff --git a/Userland/Libraries/LibJS/Heap/Internals.h b/Userland/Libraries/LibJS/Heap/Internals.h index 08d20537d01..2bb809bb908 100644 --- a/Userland/Libraries/LibJS/Heap/Internals.h +++ b/Userland/Libraries/LibJS/Heap/Internals.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2024, Andreas Kling * Copyright (c) 2020-2023, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -33,7 +33,7 @@ class HeapBlockBase { AK_MAKE_NONCOPYABLE(HeapBlockBase); public: - static constexpr auto block_size = 4 * KiB; + static size_t block_size; static HeapBlockBase* from_cell(Cell const* cell) { return reinterpret_cast(bit_cast(cell) & ~(HeapBlockBase::block_size - 1));