Skip to content

Truly independent web browser

LibGC+LibWasm: Keep each heap's cell allocators in a table of its own

Andreas Kling1901d73df7e9
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index 8de3c5e502a..01d6cd22938 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -70,6 +70,8 @@ public:
: CellAllocatorDescriptorBase(type_info, class_name, foreign_type)
{
}
+
+ ~CAPICellAllocator() { release_index(); }
};
class RootGatheringVisitor final : public Cell::Visitor {
@@ -156,7 +158,7 @@ struct CAPI {
static_assert(sizeof(RawPtr<Cell>) == sizeof(Cell*));
static_assert(offsetof(CellAllocator::LocalFreeCells, free_list) == 0);
static_assert(sizeof(FlatPtr) == sizeof(void*));
- static CellAllocator::LocalFreeCells* local_free_cells(Heap& heap, CellAllocatorDescriptorBase& descriptor) { return descriptor.for_heap(heap).local_free_cells({}); }
+ static CellAllocator::LocalFreeCells* local_free_cells(Heap& heap, CellAllocatorDescriptorBase& descriptor) { return heap.cell_allocator_for(descriptor).local_free_cells({}); }
static void set_foreign_context(Heap& heap, void* context) { heap.m_foreign_context = context; }
static void* foreign_context(Heap& heap) { return heap.m_foreign_context; }
static void defer_gc(Heap& heap) { heap.defer_gc(); }
diff --git a/Libraries/LibGC/CellAllocator.cpp b/Libraries/LibGC/CellAllocator.cpp
index 92fbd7a716e..8a6033183fa 100644
--- a/Libraries/LibGC/CellAllocator.cpp
+++ b/Libraries/LibGC/CellAllocator.cpp
@@ -5,7 +5,9 @@
*/
#include <AK/Badge.h>
+#include <AK/Mutex.h>
#include <AK/NeverDestroyed.h>
+#include <AK/Vector.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Heap.h>
@@ -13,6 +15,37 @@
namespace GC {
+// Descriptors are shared by the heaps of all threads, so they keep no per-heap state. Each has an index instead, which
+// keeps the tables of the heaps dense: the indices of descriptors that went away are used again.
+struct DescriptorIndices {
+ Mutex mutex;
+ size_t next_index { 0 };
+ Vector<size_t> free_indices;
+};
+
+static DescriptorIndices& descriptor_indices()
+{
+ static AK::NeverDestroyed<DescriptorIndices> indices;
+ return *indices;
+}
+
+CellAllocatorDescriptorBase::CellAllocatorDescriptorBase(CellTypeInfo const& type_info, StringView class_name, void const* foreign_type)
+ : m_type_info(type_info)
+ , m_class_name(class_name)
+ , m_foreign_type(foreign_type)
+{
+ auto& indices = descriptor_indices();
+ MutexLocker locker { indices.mutex };
+ m_index = indices.free_indices.is_empty() ? indices.next_index++ : indices.free_indices.take_last();
+}
+
+void CellAllocatorDescriptorBase::release_index()
+{
+ auto& indices = descriptor_indices();
+ MutexLocker locker { indices.mutex };
+ indices.free_indices.append(m_index);
+}
+
BlockAllocator& CellAllocator::shared_block_allocator()
{
static AK::NeverDestroyed<BlockAllocator> allocator;
@@ -40,16 +73,6 @@ CellAllocator::~CellAllocator()
reclaim_all(m_usable_blocks);
}
-CellAllocator& CellAllocatorDescriptorBase::for_heap(Heap& heap)
-{
- if (m_last_heap == &heap) [[likely]]
- return *m_last_allocator;
- auto& allocator = heap.cell_allocator_for({}, *this);
- m_last_heap = &heap;
- m_last_allocator = &allocator;
- return allocator;
-}
-
Cell* CellAllocator::allocate_cell_slow(Heap& heap)
{
// NB: The local block has no free cells left, and take_free_cells() left it full.
diff --git a/Libraries/LibGC/CellAllocator.h b/Libraries/LibGC/CellAllocator.h
index 05195a7eafd..b5e9177c303 100644
--- a/Libraries/LibGC/CellAllocator.h
+++ b/Libraries/LibGC/CellAllocator.h
@@ -36,31 +36,21 @@ public:
size_t cell_size() const { return m_type_info.cell_size; }
void const* foreign_type() const { return m_foreign_type; }
- CellAllocator& for_heap(Heap&);
-
- void forget_heap(Badge<Heap>, Heap& heap)
- {
- if (m_last_heap == &heap) {
- m_last_heap = nullptr;
- m_last_allocator = nullptr;
- }
- }
+ // Where each heap keeps its allocator for this descriptor, see Heap::cell_allocator_for().
+ size_t index() const { return m_index; }
protected:
- CellAllocatorDescriptorBase(CellTypeInfo const& type_info, StringView class_name, void const* foreign_type = nullptr)
- : m_type_info(type_info)
- , m_class_name(class_name)
- , m_foreign_type(foreign_type)
- {
- }
+ CellAllocatorDescriptorBase(CellTypeInfo const& type_info, StringView class_name, void const* foreign_type = nullptr);
+
+ // A descriptor that goes away while the process lives must call this, so that its index does not go to waste. No
+ // heap may have an allocator for it any more.
+ void release_index();
private:
CellTypeInfo const& m_type_info;
Optional<StringView> m_class_name;
void const* m_foreign_type { nullptr };
-
- Heap* m_last_heap { nullptr };
- CellAllocator* m_last_allocator { nullptr };
+ size_t m_index { 0 };
};
class GC_API CellAllocator {
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cpp
index 313c6a20434..9a403f8ab7a 100644
--- a/Libraries/LibGC/Heap.cpp
+++ b/Libraries/LibGC/Heap.cpp
@@ -304,11 +304,15 @@ void Heap::set_default_heap_for_testing(Heap& heap)
s_the = &heap;
}
-CellAllocator& Heap::cell_allocator_for(Badge<CellAllocatorDescriptorBase>, CellAllocatorDescriptorBase& descriptor)
+CellAllocator& Heap::create_cell_allocator(CellAllocatorDescriptorBase& descriptor)
{
- return *m_cell_allocators_by_type.ensure(&descriptor, [&] {
- return make<CellAllocator>(descriptor);
- });
+ auto index = descriptor.index();
+ if (index >= m_cell_allocators_by_descriptor_index.size())
+ m_cell_allocators_by_descriptor_index.resize(index + 1);
+ auto& allocator = m_cell_allocators_by_descriptor_index[index];
+ VERIFY(!allocator);
+ allocator = make<CellAllocator>(descriptor);
+ return *allocator;
}
Heap::Heap(AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> gather_embedder_roots, BecomeProcessDefault become_process_default)
@@ -325,8 +329,6 @@ Heap::~Heap()
{
collect_garbage(CollectionType::CollectEverything);
- for (auto& entry : m_cell_allocators_by_type)
- entry.key->forget_heap({}, *this);
while (auto* allocator = m_all_cell_allocators.first())
m_all_cell_allocators.remove(*allocator);
@@ -354,7 +356,7 @@ void Heap::add_stats(AK::JsonObject& values) const
values.set("gc.allocated_bytes_since_gc"sv, m_allocated_bytes_since_last_gc);
values.set("gc.heap_blocks"sv, m_live_heap_blocks.size());
values.set("gc.heap_block_bytes"sv, m_live_heap_blocks.size() * HeapBlock::BLOCK_SIZE);
- values.set("gc.cell_allocators"sv, m_cell_allocators_by_type.size());
+ values.set("gc.cell_allocators"sv, m_all_cell_allocators.size_slow());
values.set("gc.threshold_bytes"sv, m_gc_bytes_threshold);
values.set("gc.live_cell_bytes"sv, m_stats->live_cell_bytes);
values.set("gc.live_external_bytes"sv, m_stats->live_external_bytes);
diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.h
index 258db7c67d0..6f62b6f9122 100644
--- a/Libraries/LibGC/Heap.h
+++ b/Libraries/LibGC/Heap.h
@@ -150,7 +150,6 @@ public:
void register_sweep_callback(AK::Function<void()>);
void register_cell_allocator(Badge<CellAllocator>, CellAllocator&);
- CellAllocator& cell_allocator_for(Badge<CellAllocatorDescriptorBase>, CellAllocatorDescriptorBase&);
void uproot_cell(Cell* cell);
void uproot_cell(ForeignCell* cell) { uproot_cell(as_cell(cell)); }
@@ -196,10 +195,21 @@ private:
return *cell;
}
+ CellAllocator& cell_allocator_for(CellAllocatorDescriptorBase& descriptor)
+ {
+ auto index = descriptor.index();
+ if (index < m_cell_allocators_by_descriptor_index.size()) [[likely]] {
+ if (auto* allocator = m_cell_allocators_by_descriptor_index[index].ptr()) [[likely]]
+ return *allocator;
+ }
+ return create_cell_allocator(descriptor);
+ }
+ CellAllocator& create_cell_allocator(CellAllocatorDescriptorBase&);
+
template<typename T>
Cell* allocate_cell()
{
- static_assert(requires { T::cell_allocator.for_heap(*this).allocate_cell(*this); }, "GC cell type must declare its own allocator using GC_DECLARE_ALLOCATOR(ClassName)");
+ static_assert(requires { cell_allocator_for(T::cell_allocator).allocate_cell(*this); }, "GC cell type must declare its own allocator using GC_DECLARE_ALLOCATOR(ClassName)");
static_assert(IsSame<T, typename decltype(T::cell_allocator)::CellType>,
"GC cell allocator type mismatch");
@@ -217,7 +227,7 @@ private:
{
VERIFY(!m_collecting_garbage);
will_allocate(descriptor.cell_size(), triggers_collection);
- return descriptor.for_heap(*this).allocate_cell(*this);
+ return cell_allocator_for(descriptor).allocate_cell(*this);
}
// Cells allocated during incremental sweep in a block that the sweep has
@@ -323,7 +333,7 @@ private:
bool m_should_collect_on_every_allocation { false };
CellAllocator::List m_all_cell_allocators;
- HashMap<CellAllocatorDescriptorBase*, NonnullOwnPtr<CellAllocator>> m_cell_allocators_by_type;
+ Vector<OwnPtr<CellAllocator>> m_cell_allocators_by_descriptor_index;
RootImpl::List m_roots;
RootVectorBase::List m_root_vectors;
diff --git a/Libraries/LibWasm/Engine/src/runtime/heap.rs b/Libraries/LibWasm/Engine/src/runtime/heap.rs
index 7a0824b9f94..d33e45f8959 100644
--- a/Libraries/LibWasm/Engine/src/runtime/heap.rs
+++ b/Libraries/LibWasm/Engine/src/runtime/heap.rs
@@ -203,7 +203,7 @@ struct Allocators {
entities: Vec<*mut c_void>,
}
-// The allocators are only handles, which LibGC only uses from the thread of the heap it allocates from.
+// The allocators are only handles: LibGC never changes them, and keeps what it allocates with in each heap.
unsafe impl Send for Allocators {}
unsafe impl Sync for Allocators {}