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.

(cherry picked from commit a6bf253602702c052453d5ccb45a40d8fbb368ba)
This commit is contained in:
Andreas Kling authored and Nico Weber committed 2024-11-09 07:29:52 -05:00
1 parent eb7b733325
commit 0e13e00d6a
3 files changed
+5 -6

No files matched your search

@@ -23,9 +23,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) {
+3 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -18,6 +18,8 @@
namespace JS {
size_t HeapBlockBase::block_size = PAGE_SIZE;
NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, CellAllocator& cell_allocator, size_t cell_size, [[maybe_unused]] char const* class_name)
{
#ifdef AK_OS_SERENITY
+2 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
* 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<HeapBlockBase*>(bit_cast<FlatPtr>(cell) & ~(HeapBlockBase::block_size - 1));