Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibJS: Add fast ExecutionContext allocator #24421

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Userland/Libraries/LibJS/Runtime/ExecutionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,34 @@

namespace JS {

class ExecutionContextAllocator {
public:
NonnullOwnPtr<ExecutionContext> allocate(Heap& heap)
{
if (m_execution_contexts.is_empty())
return adopt_own(*new ExecutionContext(heap));
void* slot = m_execution_contexts.take_last();
return adopt_own(*new (slot) ExecutionContext(heap));
}
void deallocate(void* ptr)
{
m_execution_contexts.append(ptr);
}

private:
Vector<void*> m_execution_contexts;
};

static NeverDestroyed<ExecutionContextAllocator> s_execution_context_allocator;

NonnullOwnPtr<ExecutionContext> ExecutionContext::create(Heap& heap)
{
return adopt_own(*new ExecutionContext(heap));
return s_execution_context_allocator->allocate(heap);
}

void ExecutionContext::operator delete(void* ptr)
{
s_execution_context_allocator->deallocate(ptr);
}

ExecutionContext::ExecutionContext(Heap& heap)
Expand Down
4 changes: 4 additions & 0 deletions Userland/Libraries/LibJS/Runtime/ExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ struct ExecutionContext {
void visit_edges(Cell::Visitor&);

private:
friend class ExecutionContextAllocator;

ExecutionContext(Heap&);

IntrusiveListNode<ExecutionContext> m_list_node;

public:
void operator delete(void* ptr);

Heap& m_heap;

using List = IntrusiveList<&ExecutionContext::m_list_node>;
Expand Down