From 0533f6d1a6fb12f428f2a44d9b4877ece37aa98e Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Thu, 7 May 2026 22:16:39 +0100 Subject: [PATCH] AK+LibWeb: Add a `fast_as()` method for quickly casting mixin types `as_if()` previously had a fast path that used `fast_is()` and a static cast. Mixin types couldn't use this fast path, since a static cast from a sibling base is not valid, so would fall back to using a slow dynamic cast instead. This change adds a `fast_as()` mechanism for mixin types to opt in to and teaches `as_if()` to use it. This means `as()` and `as_if()` can be used with mixin types without incurring the overhead of a dynamic cast --- AK/TypeCasts.h | 4 +++- Libraries/LibWeb/DOM/Node.h | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/AK/TypeCasts.h b/AK/TypeCasts.h index d9c924bca0..7141d3b8a3 100644 --- a/AK/TypeCasts.h +++ b/AK/TypeCasts.h @@ -38,7 +38,9 @@ ALWAYS_INLINE bool is(NonnullRefPtr const& input) template ALWAYS_INLINE CopyConst* as_if(InputType& input) { - if constexpr (requires { input.template fast_is>(); static_cast*>(&input); }) { + if constexpr (requires { input.template fast_as>(); }) { + return input.template fast_as>(); + } else if constexpr (requires { input.template fast_is>(); static_cast*>(&input); }) { if (!is(input)) return nullptr; return static_cast*>(&input); diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index 7bc07851fa..f6573fa005 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -379,6 +379,11 @@ public: template bool fast_is() const = delete; + template + T* fast_as() = delete; + template + T const* fast_as() const = delete; + WebIDL::ExceptionOr ensure_pre_insertion_validity(JS::Realm&, GC::Ref node, GC::Ptr child) const; bool is_host_including_inclusive_ancestor_of(Node const&) const;