Files
ladybird/Libraries/LibJS/Runtime/Realm.h
Andreas Kling c0e520463e LibJS: Invalidate prototype chains via per-shape child lists
invalidate_all_prototype_chains_leading_to_this used to scan every
prototype shape in the realm and walk each one's chain looking for
the mutated shape. That was O(N_prototype_shapes x chain_depth) per
mutation and showed up hot in real profiles when a page churned a
lot of prototype state during startup.

Each prototype shape now keeps a weak list of the prototype shapes
whose immediate [[Prototype]] points at the object that owns this
shape. The list is registered on prototype-shape creation
(clone_for_prototype, set_prototype_shape) and migrated to the new
prototype shape when the owning prototype object transitions to a
new shape. Invalidation is then a recursive walk over this direct-
child registry, costing O(transitive descendants).

Saves ~300 ms of main thread time when loading https://youtube.com/
on my Linux machine. :^)
2026-04-24 18:59:01 +02:00

87 lines
3.0 KiB
C++

/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/HashTable.h>
#include <AK/OwnPtr.h>
#include <AK/StringView.h>
#include <AK/Weakable.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Heap.h>
#include <LibJS/Bytecode/Builtins.h>
#include <LibJS/Export.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Intrinsics.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
// 9.3 Realms, https://tc39.es/ecma262/#realm-record
class JS_API Realm final : public Cell {
GC_CELL(Realm, Cell);
GC_DECLARE_ALLOCATOR(Realm);
public:
struct HostDefined {
virtual ~HostDefined() = default;
virtual void visit_edges(Cell::Visitor&) { }
template<typename T>
bool fast_is() const = delete;
virtual bool is_principal_host_defined() const { return false; }
virtual bool is_synthetic_host_defined() const { return false; }
};
template<typename T, typename... Args>
GC::Ref<T> create(Args&&... args)
{
auto object = heap().allocate<T>(forward<Args>(args)...);
static_cast<Cell*>(object)->initialize(*this);
return *object;
}
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value);
[[nodiscard]] Object& global_object() const { return *m_global_object; }
void set_global_object(GC::Ref<Object> global) { m_global_object = global; }
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
void set_global_environment(GC::Ref<GlobalEnvironment> environment);
[[nodiscard]] DeclarativeEnvironment& global_declarative_environment() const { return *m_global_declarative_environment; }
[[nodiscard]] Intrinsics const& intrinsics() const { return *m_intrinsics; }
[[nodiscard]] Intrinsics& intrinsics() { return *m_intrinsics; }
void set_intrinsics(Badge<Intrinsics>, Intrinsics& intrinsics)
{
VERIFY(!m_intrinsics);
m_intrinsics = &intrinsics;
}
HostDefined* host_defined() { return m_host_defined; }
HostDefined const* host_defined() const { return m_host_defined; }
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
private:
Realm() = default;
virtual void visit_edges(Visitor&) override;
GC::Ptr<Intrinsics> m_intrinsics; // [[Intrinsics]]
GC::Ptr<Object> m_global_object; // [[GlobalObject]]
GC::Ptr<DeclarativeEnvironment> m_global_declarative_environment; // Cached from GlobalEnv
GC::Ptr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]]
OwnPtr<HostDefined> m_host_defined; // [[HostDefined]]
};
}