LibWeb: Avoid GC allocation in ResizeObservation::is_active()

Extract the box size computation into a new compute_box_size() that
returns a plain struct with two doubles, and use it in is_active()
instead of calculate_box_size() which allocates a GC-managed
ResizeObserverSize object.

Since is_active() only needs to compare sizes and immediately discards
the result, there's no reason to involve the GC. The GC-allocating
calculate_box_size() now delegates to compute_box_size() internally.

This was 2.6% of CPU time while playing a YouTube video.
This commit is contained in:
Andreas Kling
2026-02-21 01:33:41 +01:00
committed by Alexander Kalenik
parent 935f76f88e
commit 5fdcf207f8
Notes: github-actions[bot] 2026-02-21 02:52:27 +00:00
3 changed files with 32 additions and 19 deletions

View File

@@ -22,34 +22,27 @@ void ResizeObserverSize::initialize(JS::Realm& realm)
}
// https://drafts.csswg.org/resize-observer-1/#calculate-box-size
GC::Ref<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
ResizeObserverSize::RawSize ResizeObserverSize::compute_box_size(DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
{
// 1. Let computedSize be a new ResizeObserverSize object.
auto computed_size = realm.create<ResizeObserverSize>(realm);
RawSize size;
// FIXME: 2. If target is an SVGGraphicsElement that does not have an associated CSS layout box:
// FIXME: If target is an SVGGraphicsElement that does not have an associated CSS layout box:
// Otherwise:
if (target.paintable_box()) {
auto const& paintable_box = *target.paintable_box();
switch (observed_box) {
case Bindings::ResizeObserverBoxOptions::BorderBox:
// 1. Set computedSizes inlineSize attribute to targets border area inline length.
computed_size->set_inline_size(paintable_box.border_box_width().to_double());
// 2. Set computedSizes blockSize attribute to targets border area block length.
computed_size->set_block_size(paintable_box.border_box_height().to_double());
size.inline_size = paintable_box.border_box_width().to_double();
size.block_size = paintable_box.border_box_height().to_double();
break;
case Bindings::ResizeObserverBoxOptions::ContentBox:
// 1. Set computedSizes inlineSize attribute to targets content area inline length.
computed_size->set_inline_size(paintable_box.content_width().to_double());
// 2. Set computedSizes blockSize attribute to targets content area block length.
computed_size->set_block_size(paintable_box.content_height().to_double());
size.inline_size = paintable_box.content_width().to_double();
size.block_size = paintable_box.content_height().to_double();
break;
case Bindings::ResizeObserverBoxOptions::DevicePixelContentBox: {
auto device_pixel_ratio = target.document().window()->device_pixel_ratio();
// 1. Set computedSizes inlineSize attribute to targets content area inline length, in integral device pixels.
computed_size->set_inline_size(paintable_box.border_box_width().to_double() * device_pixel_ratio);
// 2. Set computedSizes blockSize attribute to targets content area block length, in integral device pixels.
computed_size->set_block_size(paintable_box.border_box_height().to_double() * device_pixel_ratio);
size.inline_size = paintable_box.border_box_width().to_double() * device_pixel_ratio;
size.block_size = paintable_box.border_box_height().to_double() * device_pixel_ratio;
break;
}
default:
@@ -57,10 +50,23 @@ GC::Ref<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& re
}
}
// 3. Return computedSize.s
return size;
}
GC::Ref<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
{
auto raw = compute_box_size(target, observed_box);
auto computed_size = realm.create<ResizeObserverSize>(realm);
computed_size->set_inline_size(raw.inline_size);
computed_size->set_block_size(raw.block_size);
return computed_size;
}
bool ResizeObserverSize::equals(RawSize const& other) const
{
return m_inline_size == other.inline_size && m_block_size == other.block_size;
}
bool ResizeObserverSize::equals(ResizeObserverSize const& other) const
{
return m_inline_size == other.m_inline_size && m_block_size == other.m_block_size;