LibJS+LibWeb: Use GC::Weak instead of AK::WeakPtr for GC-allocated types

This makes some common types like JS::Object smaller (by 8 bytes) and
yields a minor speed improvement on many benchmarks.
This commit is contained in:
Andreas Kling 2025-10-16 11:13:54 +02:00 committed by Andreas Kling
parent 25a5ed94d6
commit dfa796a4e4
Notes: github-actions[bot] 2025-10-17 15:25:08 +00:00
36 changed files with 111 additions and 115 deletions

View file

@ -8,9 +8,9 @@
#include <AK/Function.h>
#include <AK/Noncopyable.h>
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibGC/Ptr.h>
#include <LibGC/Weak.h>
#include <LibJS/Forward.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/EventLoop/TaskQueue.h>
@ -119,7 +119,7 @@ private:
// https://html.spec.whatwg.org/multipage/webappapis.html#performing-a-microtask-checkpoint
bool m_performing_a_microtask_checkpoint { false };
Vector<WeakPtr<DOM::Document>> m_documents;
Vector<GC::Weak<DOM::Document>> m_documents;
// Used to implement step 4 of "perform a microtask checkpoint".
// NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.

View file

@ -8,7 +8,7 @@
#pragma once
#include <AK/String.h>
#include <AK/WeakPtr.h>
#include <LibGC/Weak.h>
#include <LibWeb/Bindings/HTMLFormElementPrototype.h>
#include <LibWeb/DOM/InputEventsTarget.h>
#include <LibWeb/Export.h>
@ -178,7 +178,7 @@ protected:
private:
void reset_form_owner();
WeakPtr<HTMLFormElement> m_form;
GC::Weak<HTMLFormElement> m_form;
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
bool m_parser_inserted { false };

View file

@ -380,7 +380,7 @@ static void show_the_picker_if_applicable(HTMLInputElement& element)
// 2. Wait for the user to have made their selection.
auto accepted_file_types = element.parse_accept_attribute();
auto allow_multiple_files = element.has_attribute(HTML::AttributeNames::multiple) ? AllowMultipleFiles::Yes : AllowMultipleFiles::No;
auto weak_element = element.make_weak_ptr<HTMLInputElement>();
auto weak_element = GC::Weak<HTMLInputElement> { element };
element.set_is_open(true);
element.document().browsing_context()->top_level_browsing_context()->page().did_request_file_picker(weak_element, accepted_file_types, allow_multiple_files);
@ -404,7 +404,7 @@ static void show_the_picker_if_applicable(HTMLInputElement& element)
// events, or a cancel event.)
else {
if (element.type_state() == HTMLInputElement::TypeAttributeState::Color) {
auto weak_element = element.make_weak_ptr<HTMLInputElement>();
auto weak_element = GC::Weak<HTMLInputElement> { element };
element.set_is_open(true);
element.document().browsing_context()->top_level_browsing_context()->page().did_request_color_picker(weak_element, Color::from_utf16_string(element.value()).value_or(Color(0, 0, 0)));
}

View file

@ -570,7 +570,7 @@ bool HTMLLinkElement::stylesheet_linked_resource_fetch_setup_steps(Fetch::Infras
void HTMLLinkElement::set_parser_document(Badge<HTMLParser>, GC::Ref<DOM::Document> document)
{
m_parser_document = document->make_weak_ptr<DOM::Document>();
m_parser_document = document;
}
bool HTMLLinkElement::is_implicitly_potentially_render_blocking() const

View file

@ -167,7 +167,7 @@ private:
Optional<String> m_mime_type;
WeakPtr<DOM::Document> m_parser_document;
GC::Weak<DOM::Document> m_parser_document;
};
}

View file

@ -480,7 +480,7 @@ void HTMLSelectElement::show_the_picker_if_applicable()
}
// Request select dropdown
auto weak_element = make_weak_ptr<HTMLSelectElement>();
auto weak_element = GC::Weak<HTMLSelectElement> { *this };
auto rect = get_bounding_client_rect();
auto position = document().navigable()->to_top_level_position(Web::CSSPixelPoint { rect.x(), rect.bottom() });
document().page().did_request_select_dropdown(weak_element, position, rect.width(), m_select_items);

View file

@ -31,10 +31,10 @@ GC_DEFINE_ALLOCATOR(HTMLTextAreaElement);
HTMLTextAreaElement::HTMLTextAreaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_input_event_timer(Core::Timer::create_single_shot(0, [weak_this = make_weak_ptr()]() {
, m_input_event_timer(Core::Timer::create_single_shot(0, [weak_this = GC::Weak<HTMLTextAreaElement> { *this }]() {
if (!weak_this)
return;
static_cast<HTMLTextAreaElement*>(weak_this.ptr())->queue_firing_input_event();
weak_this->queue_firing_input_event();
}))
{
}

View file

@ -51,8 +51,7 @@ struct PaintConfig {
};
// https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
class WEB_API Navigable : public JS::Cell
, public Weakable<Navigable> {
class WEB_API Navigable : public JS::Cell {
GC_CELL(Navigable, JS::Cell);
GC_DECLARE_ALLOCATOR(Navigable);